0

I am trying to create fields dynamically on a custom object. Field's metadata info taking from csv file. When I am uploading the file then I am getting callout exception from MetadataService.

EXCEPTION_THROWN|[13432]|System.CalloutException: Web service callout failed: WebService returned a SOAP Fault: 'Text ' is not a valid value for the enum 'FieldType' faultcode=soapenv:Client faultactor=

I have checked in my csv file all the values are fine. Text is also supported metadata type for customFields.

Here's my method:

public static void createCustomFields(string fieldName, string 
    System.debug('******fieldName**inside service*'+fieldName); 
    System.debug('******fieldLength**inside service*'+fieldLength);
    MetadataService.MetadataPort service = createService();     
    MetadataService.CustomField customField = new MetadataService.CustomField();
    customField.fullName = fieldName;
    customField.label = fieldLabel;
    customField.type_x = fieldType;
    customField.length = fieldLength;
    System.debug('*******Custom Field values******');
    System.debug('*************'+customField);
    List<MetadataService.SaveResult> results = service.createMetadata(new MetadataService.Metadata[] { customField });              
    handleSaveResults(results[0]);
    system.debug('******results*****'+results);      
}

string fieldName, string fieldLabel,integer fieldLength, string fieldType

all these values are coming correctly in this method from csv file.

Issue in this line: List<MetadataService.SaveResult> results = service.createMetadata(new MetadataService.Metadata[] { customField });

Strange thing is when I run the same code from anonymous by passing hardcoded values it works fine and custom field get created.

Not sure what's wrong with csv file.

Attaching the snapshot of csv file.

Any suggestion would be much appreciated.enter image description here

3

as I can see from error message, you have trailing space in Text(space goes here).

you need to remove that space before passing to API by using normalizeSpace() or trim() String methods .

customField.type_x = fieldType;

change to

customField.type_x = fieldType.normalizeSpace();

it is better to sanitise string before passing to createCustomFields method:

YourClass.createCustomFields(fieldName, fieldLabel, fieldLength, fieldType.normalizeSpace());

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.