function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Andy.xuAndy.xu 

Metadata api CRUD-based

i want to add CustomField to standard object (Account),code(Metadata api CRUD-based) :

 

  CustomObject co = new CustomObject();
  co.setFullName("Account");
  CustomField nf = new CustomField();
  nf.setType(FieldType.Text);
  nf.setLabel("yy");
  co.setNameField(nf);
  
  UpdateMetadata updateMetadata = new UpdateMetadata();
  updateMetadata.setMetadata(co);

 

Like thi,after run ,Exception come out:

    Exception in thread "main" AxisFault
 faultCode: {http://soap.sforce.com/2006/04/metadata}UNKNOWN_EXCEPTION
 faultSubcode:
 faultString: null: Invalid fullName, must end in __c
 

can anyone help me....?and thanks!

shailesh_patil.ax1006shailesh_patil.ax1006

If you want to create a custom object, you have to do something like :

 

    CustomObject co = new CustomObject();
    co.setFullName(objectName+"__c");
    co.setDeploymentStatus(DeploymentStatus.Deployed);
    co.setDescription("Created by the WSC using the Metadata API");
    co.setLabel(displayName);
    co.setPluralLabel(displayName+"s");
    co.setSharingModel(SharingModel.ReadWrite);
    co.setEnableActivities(true);

 

If you want to add a custom field to the object defined above, you have to do something like :

 

// create the text id field
    CustomField field = new CustomField();
    field.setType(FieldType.Text);
    field.setDescription("The custom object identifier field");
    field.setLabel(displayName);
    field.setFullName(objectName+"__c");
    // add the field to the custom object
    co.setNameField(field);

 

Here you are creating a custom object and then adding a custom field to it. If you wanna add a custom field to a standard object, you have to access that object. I don't know whether this would work or not but you have to do something like,

 

CustomObject co = new CustomObject(FullName='Account');

 

And then use above defined custom field definition.

 

Please let me know too. I am just curious !!

 

Daniel BallingerDaniel Ballinger

I just had the same issue with adding a new field and resolved it by just creating the CustomField rather than updating the Account standard object to have a custom field.

 

 

CustomField cf = new CustomField();
cf.fullName = "Account.testField__c";
cf.type = FieldType.Text;
cf.label = "Test Field";
cf.length = 50;
cf.lengthSpecified = true;

AsyncResult[] ars = metadataService.create (new CustomField[]{ cf } );

 The syntax here will be a bit different as I'm working in C# rather than Java, but the idea should be the same.

 

Notice how the fullName includes both the new fields name and the parent objects name.