When I try to add a new field to a custom object, I get an error. Apparently I missed one of the recommended fields or have some typo? I also tried to add a field of the "Text" type - everything was successful with it.

public class MetadataUtility {

    public static void generateField(String objectAPIName, String fieldAPIName, String fieldLabel){
        String metadata = '{"Metadata" : {"type" : "Currency","Precision":18,"Scale":1,"Label":"'+fieldLabel+'","DeveloperName":"'+fieldLabel+'","Required":false},"FullName":"'+objectAPIName+'.'+fieldAPIName+'"}';
        String responseBody = createField(metadata);
    public static String createField(String metadata) {
        HttpRequest request = new HttpRequest();
        request.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID());
        request.setHeader('Content-Type', 'application/json');    
        Http http = new Http();
        HTTPResponse res = http.send(request);
        return res.getBody();

//I'm trying to make a recording

MetadataUtility.generateField('Exchange_Rate__c', 'BYN__c', 'BYN');

//I get this error

System.HttpResponse[Status=Bad Request, StatusCode=400]

[{"message":"Cannot deserialize instance of complexvalue from VALUE_NUMBER_INT value 18 or request may be missing a required field at [line:1, column:50]","errorCode":"JSON_PARSER_ERROR"}]
  • the apex metadata api wrapper makes coding this more reliable
    – cropredy
    Jun 11 at 19:49
  • Thanks for the information. I'll take it into consideration. But I first need to solve my write error problem... Jun 13 at 15:52

During the time since the publication of the question, I conducted a number of experiments and solving the problem on adding it in this form led to success. Maybe it will be useful to someone:

String metadata = '{"Metadata" : {"type" : "Currency","precision" : 18,"scale" : 1,"label" : "' + fieldLabel + '","required" : false},"fullName" : "'+objectAPIName+'.'+fieldAPIName+'"}';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.