我正在使用Tooling来更新选择列表值。它犯了下列错误:
无法更改Apex类或触发器中引用的自定义字段的字段类型:
代码:
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);发布于 2015-01-24 10:49:07
好的,在这个阶段,虽然我还不能百分之百确定,但这似乎是不可能的,至少在Tooling 中是这样的,我已经在下面解释了我尝试过的内容。在此期间,我可以100%确认这确实与元数据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错误的理论。Tooling的数据结构的各个方面,很像元数据API,一开始似乎是可选的,但有些则不会出现。我的错误消息背后的理论是,您需要在元数据部分中设置更多信息,例如字段类型,因为Salesforce似乎认为您想要更改它,因为您没有传递它。
尝试了Tooling方法。为此,我通常建议首先检索自定义字段,然后修改数据结构并执行更新。但是,在这种情况下,不管我尝试了什么,我似乎都从CustomField上的Tooling查询中获得了一组大部分为空白的元数据信息。我将看看是否可以让Tooling开发人员在这里发表评论。
Select Id, Metadata, DeveloperName, NamespacePrefix, TableEnumOrId
From CustomField
Where DeveloperName = 'YesNo'


发布于 2020-05-08 11:10:31
上面的gievn解决方案使用(https://andyinthecloud.com/2013/10/27/introduction-to-calling-the-metadata-api-from-apex/)工作;但是它需要为一个小需求导入所有代码。在Tooling发布之后,我发现它在没有导入和管理过多代码的情况下,为这样的实现提供了生命保护。我们只需要使用Tooling发出一个标注。
详细的文章在这里: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
https://stackoverflow.com/questions/28032972
复制相似问题