2

I am trying to create a custom field for an existing entity like account with v48 of the Metadata api My code is:

MetadataService.CustomField customField = new MetadataService.CustomField();
customField.fullName = "Contact.TestField__c";
customField.label = "Test Field";
customField.type = FieldType.Text;
Metadata[] metadata = new Metadata[] { customField };
createMetadataResponse response = client.createMetadataAsync(sessionHeader, null, null, metadata).Result;

in my response I get a FIELD_INTEGRITY_EXCEPTION with the message "Could not resolve standard field's name." What have I done wrong here? Do I have to add a reference to the entity the field belongs to somewhere?

1
  • 1
    try adding typeSpecified = true in your code
    – User6670
    Commented Feb 24, 2020 at 10:16

1 Answer 1

2

Here is a working example I used to add a lookup custom field from Account to Opportunity.

MetadataService metadataService = salesforceSession.GetMetadataService();

string fieldName = "testLookup";
string SFType = "Account";

CustomField cf = new CustomField();
cf.fullName = SFType + "." + fieldName + "__c";
cf.type = FieldType.Lookup;
//type specified is required to avoid FIELD_INTEGRITY_EXCEPTION: https://developer.salesforce.com/forums/ForumsMain?id=906F0000000Ad5LIAS
cf.typeSpecified = true;
cf.label = fieldName + " label";
cf.referenceTo = "Opportunity";
cf.relationshipLabel = "Test Accounts";
cf.relationshipName = "test";
cf.relationshipOrderSpecified = false;
cf.lengthSpecified = false;

SaveResult[] saveResults = metadataService.createMetadata(new CustomField[] { cf });

Assert.IsTrue(saveResults[0].success);

As User6670 commented, you need to set typeSpecified to true in addition to setting the type. I see I'd originally found that detail in [Metadata API] Adding Custom field to object.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .