I am using metadata api with c#. Below code is not giving me an error but still it is not creatng the field in salesforce. Please suggest.

                    CF = new MetadataAPI.CustomField();
                    CF.description = "invoices";
                    CF.fullName = "mynamespace__" + co.fullName + "." + Fields[i].Replace(" ", "_") + "__c"; 
                    CF.label = "invoices";
                    CF.type = MetadataAPI.FieldType.Lookup;
                    CF.referenceTo = "Opportunity";
                    CF.relationshipLabel = "invoices";
                    CF.relationshipName = "invoices";
                    CF.relationshipOrderSpecified = false;
                    ASyncResCFS = MetadataSvc.create(new CustomField [] {CF});
  • Have you checked the SaveResult that the create method returns for errors? Jun 11 '17 at 23:40
  • Yes I did but it says no error. I am able to create other type of fields but not lookup.
    – Mehul
    Jun 12 '17 at 7:29

Just tried to replicate with the following code:

MetadataService metadataService = salesforceSession.GetMetadataService();

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

CustomField cf = new CustomField();
cf.fullName = SFType + "." + fieldName + "__c";
cf.type = FieldType.Lookup;
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 });



The SaveResult was successful and returned the fullName. One thing to note, by org was a developer edition and it did have a namespace defined. Yet I didn't need to include that namespace in the code and it didn't come back in the fullName.

I was using v40.0 of the Metadata API.

Created Lookup field via Metadata API


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.