使用工具 API 更新多 Select 选择列表 - Salesforce

Updating Multi Select Picklist Using Tooling API - Salesforce

我正在使用工具 API 更新选项列表值。它给出以下错误:

Cannot change field type of a custom field referenced in Apex class or trigger:

代码:

        String json = '{ "Metadata" : { "externalId" : false, "label" : "Meeting Type", "picklist" : { "controllingField" : null, "picklistValues" : [';
        Schema.DescribeFieldResult fieldResult = Scope_Of_Services_Tasks__c.Meeting_Type__c.getDescribe();
        List<Schema.PicklistEntry>listpickVal = fieldResult.getPicklistValues();
        /*Set<string>setPickValue = new set<string>{'Open Items Review','Implementation','Q1','Q2','Q3','Q4','Benefits Review','Vendor Partner Review'};
        for(string plEntry : setPickValue){
            json = json+'{"default" : false, "description" : null, "fullName" : "'+plEntry+'"},';
        }*/

            for(Schema.PicklistEntry plEntry : listpickVal){
                json = json+'{"default" : false, "description" : null, "fullName" : "'+plEntry.getLabel()+'"},';
            }

        json = json+'{"default" : false, "description" : null, "fullName" : "'+picklistval+'"}],"restrictedPicklist" : null,"sorted" : false},"type" : "Picklist","unique" : null,"urls" : null,"visibleLines" : null,"writeRequiresMasterRead" : null },"FullName" : "Scope_Of_Services_Tasks__c.Meeting_Type__c"}';

        System.debug('@@@___'+json);
        Httprequest req = new HttpRequest();
        req.setEndpoint('https://cs7.salesforce.com/services/data/v32.0/tooling/sobjects/customField/00NE0000004t18P?_HttpMethod=PATCH');
        req.setMethod('POST');
        req.setHeader('Content-Type', 'application/json');
        req.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionId());
        req.setBody(json);

        Http httpReq = new Http();
        HttpResponse res = httpReq.send(req);

好的,所以在这个阶段,虽然我不是 100% 确定,但这似乎不可能,至少使用 工具 API,我'我在下面对我尝试过的内容进行了更多解释。与此同时,我可以 100% 确认这确实与 Metadata API 完美配合!这是一个例子...

    MetadataService.MetadataPort service = createService();             

    // Read Custom Field
    MetadataService.CustomField customField = 
        (MetadataService.CustomField) service.readMetadata('CustomField', 
            new String[] { 'Lead.picklist__c' }).getRecords()[0];       

    // Add pick list values
    metadataservice.PicklistValue two = new metadataservice.PicklistValue();
    two.fullName= 'second';
    two.default_x=false;
    metadataservice.PicklistValue three = new metadataservice.PicklistValue();
    three.fullName= 'third';
    three.default_x=false;
    customField.picklist.picklistValues.add(two);
    customField.picklist.picklistValues.add(three);     

    // Update Custom Field
    handleSaveResults(
        service.updateMetadata(
            new MetadataService.Metadata[] { customField })[0]);        

关于工具的理论API错误。工具 API 的数据结构的某些方面,很像元数据 API,乍一看似乎是可选的,但有些方面似乎不是。我的错误消息背后的理论是,您需要在元数据部分设置更多信息,例如字段类型,因为 Salesforce 似乎认为您想要更改它,因为您没有传递它。

尝试使用工具API方法。为此,我通常建议先检索自定义字段,然后修改数据结构并执行更新。但是在这种情况下,无论我尝试什么,我似乎都从我的工具 API SOQL 查询中通过 CustomField 返回了一组几乎空白的元数据信息。我看看能否让工具 API 开发人员在这里发表评论。

Select Id, Metadata, DeveloperName, NamespacePrefix, TableEnumOrId 
 From CustomField 
 Where DeveloperName = 'YesNo'

上述 gievn 解决方案使用 Apex 元数据服务 (https://andyinthecloud.com/2013/10/27/introduction-to-calling-the-metadata-api-from-apex/);但即使是很小的需求,它也需要导入所有代码。在发布工具 API 之后,我发现它是这种实现的救命稻草,无需导入和管理太多代码。我们只需要使用工具 API.

进行标注

完整的详细文章在这里:https://sfdcian.com/update-picklist-value-and-add-it-in-record-type-using-apex-salesforce/

PATCH
/services/data/v41.0/tooling/sobjects/CustomField/00N540000071QJG
Body: <as given at below URL>

这里是 JSON 必须在此标注中作为请求正文发送的正文:https://github.com/ayub-ansari/Create-Picklist-Field-Using-Apex/blob/master/FirstHTTPRequestToUpdatePicklistField