+ Start a Discussion
matt_bmatt_b 

Cannot create multiple custom fields using Metadata API

Hi all,

 

I can successfully connect to the Metadata API and create a custom field, but what I would like to do is create three custom fields all at the same time. From reading the Metadata specifications it appears I can provide the .create() method with up to 10 metadata components, so technically I should be able to pass it an array of 3 custom fields instead of a single custom field, and it should process them all with only one .create() call.

 

My code to create one custom field is as follows:

 

SFMetadataWSDL.CustomField customField = new SFMetadataWSDL.CustomField();

 

customField.fullName = "Account.TestField__c";

customField.description = "This is a custom field created by the API";

customField.label = "Test Field;

customField.formula = "HYPERLINK(\"http://www.cnet.com\", \"Open Cnet\")";

customField.type = SFMetadataWSDL.FieldType.Text;

 

SFMetadataWSDL.AsyncResult asyncResult = sfMetadata.create(new SFMetadataWSDL.Metadata[] { customField })[0];

 

This works just fine, but if I modify it to pass the .create() method an array of 3 custom fields, I cannot compile.

 

SFMetadataWSDL.CustomField[] customField = new SFMetadataWSDL.CustomField[3];

 

customField[0].fullName = "Account.Testfield1__c";

customField[0].description = "This is a custom field number 1 created by the API";

customField[0].label = "Test Field 1";

customField[0].formula = "HYPERLINK(\"http://www.cnet.com\", \"Open Cnet\")";

customField[0].type = SFMetadataWSDL.FieldType.Text;

 

customField[1].fullName = "Account.Testfield2__c";

customField[1].description = "This is a custom field number 2 created by the API";

customField[1].label = "Test Field 2";

customField[1].formula = "HYPERLINK(\"http://www.cnet.com\", \"Open Cnet\")";

customField[1].type = SFMetadataWSDL.FieldType.Text;

 

customField[2].fullName = "Account.Testfield3__c";

customField[2].description = "This is a custom field number 3 created by the API";

customField[2].label = "Test Field 3";

customField[2].formula = "HYPERLINK(\"http://www.cnet.com\", \"Open Cnet\")";

customField[2].type = SFMetadataWSDL.FieldType.Text;

 

SFMetadataWSDL.AsyncResult[] asyncResult = sfMetadata.create(new SFMetadataWSDL.Metadata[] { customField });

 

The compilation error I receive is "Cannot implicitly convert type 'SFMetadataWSDL.CustomField[]' to 'SFMetadataWSDL.Metadata'". I think I'm being an idiot here and have just missed something out - can someone point it out?

 

Thanks

Matt 

MukundMukund

Hi ,

 

could you please post the complete code .I am trying by this way  i created one method in side the generated appex class

 

publicvoidcreatecustomfield (){

soapSforceCom200604Metadata.CustomField customField =

newsoapSforceCom200604Metadata.CustomField(); 

//customField.fullName = 'BES_VRM__Relationship__c.TestField__c';

customField.description ='This is a custom field created by the API';

customField.label =''Test Field'

 

//customField.formula = "HYPERLINK(\"http://www.cnet.com\", \"Open Cnet\")";// customField.type = soapSforceCom200604Metadata.FieldType.Text;

soapSforceCom200604Metadata sfMetadata =newsoapSforceCom200604Metadata();

 

soapSforceCom200604Metadata.AsyncResult asyncResult = sfMetadata.create(

newsoapSforceCom200604Metadata.Metadata[] { customField })[0]; 

}

 

Here i am not able to call //customField.fullName = 'BES_VRM__Relationship__c.TestField__c';  as fullName is not part of the appex generatd class .

 

Also i am getting  error in

soapSforceCom200604Metadata.AsyncResult asyncResult = sfMetadata.create(

newsoapSforceCom200604Metadata.Metadata[] { customField })[0]; 

}

the error is Save error: Initial expression is of incorrect type, expected: soapSforceCom200604Metadata.Metadata soapSforceCom200604Metadata.cls /DevOrg/src/classes line 3754 Force.com save problem

 

Please let me know your  comment on this .

 

Regards

Mukund

TechnossusTechnossus

Hi Matt

 

Try this:

 

SFMetadataWSDL.AsyncResult[] asyncResult = sfMetadata.create(customField);

 

Regards

Raman