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
KunlunKunlun 

Api for adding,deleting field in Object

I need to add and delete some fields in Account, I also need to set them value.
For example:
I want to do a loop to add field which name is in " filed1, field2,......fieldN "
I also need to do a loop to set value on " filed1, field2,......fieldN "
I can't make sure how many fields I should operate, so I don't know which number is "N".
Can I use api to do it?
SuperfellSuperfell
Yes, you can use the metadata API to add new field definitions, then you can use the regular API to set their values.
KunlunKunlun
        Thanks SimonF

I had checked metadata help, and it likes as below.
        
        CustomObject co = new CustomObject();
        string name = "MyCustomObject";
        co.fullName = name + "__c";
        co.label = name + " Object";

        
        CustomField nf = new CustomField();
        nf.type = FieldType.Text;
        nf.label = co.fullName + " Name";
        
        co.nameField = nf;
AsyncResult[] ars = stub.create(new Metadata[] { co} );

what is stub? is it sfdc? sfdc only can create sObject[].
How can I add this customField into Account?

Thanks again.
SuperfellSuperfell
stub is an instance of the metadata API stub (download & import the metadata WSDL). Once you've created the custom object, you can add fields in a similar way by passing an CUstomField to the metadta create call, and specifying its fullname as customObject__c.fieldName__c
KunlunKunlun
I had downloaded metadata.wsdl and added web reference, my code is as below:
        CustomObject co = new CustomObject();
        string name = "MyCustomObject";
        co.fullName = name + "__c";
        co.deploymentStatus = DeploymentStatus.Deployed;

        co.description = "Created by the Metadata API";
        co.enableActivities = true;
        co.label = name + " Object";
        co.pluralLabel = co.label + "s";
        co.sharingModel = SharingModel.ReadWrite;

       
        CustomField nf = new CustomField();
        nf.type = FieldType.Text;
        nf.label = co.fullName + " Name";
       
        co.nameField = nf;

        MetadataService stub = new MetadataService();

        AsyncResult[] ars = stub.create(new Metadata[]{co});

but it throwed an error:INVALID_SESSION_ID: Invalid Session ID found in SessionHeader: Illegal Session

And I should add this field into Account, Account is not a custom filed, how can I add them into Account?

SuperfellSuperfell
You have to set the sessionHeader to be a valid sessionId (just like the regular enterprise/partner API)
KunlunKunlun
I had set the session info. but it throwed new error
null: Must specify a deployment status for the Custom Object

行 264:        [return: System.Xml.Serialization.XmlElementAttribute("result")]
行 265: public AsyncResult[] create([System.Xml.Serialization.XmlElementAttribute("metadata")] Metadata[] metadata) {
行 266: object[] results = this.Invoke("create", new object[] {
行 267: metadata});
行 268: return ((AsyncResult[])(results[0]));


But I had set the deplyment status in my code.
       
        CustomObject co = new CustomObject();
        string name = "MyCustomObject1";
        co.fullName = name + "__c";
        co.deploymentStatus = DeploymentStatus.Deployed;

        co.description = "Created by the Metadata API";
        co.enableActivities = true;
        co.label = name + " Object";
        co.pluralLabel = co.label + "s";
        co.sharingModel = SharingModel.ReadWrite;

       
        CustomField nf = new CustomField();
        nf.type = FieldType.Text;
        nf.label = co.fullName + " Name";

        co.nameField = nf;
        AsyncResult[] ars = metaBinding.create(new Metadata[] { co });
rajanrajan
Hi I am having the same problem with creating a object.Have you get any solution?
KunlunKunlun
I have the same problem, I don't know how to solve it.
SuperfellSuperfell
I just wrote up a complete example, see http://www.pocketsoap.com/weblog/2008/01/1803.html
KunlunKunlun
yes, it works, but how to add a custom field in Account.
SuperfellSuperfell
As it says on that page "Creating customFields is just as easy, just remember to specify the fully qualified fieldname as its fullName, e.g. if you wanted a new field on your custom object co__c, you set the name to be co__c.newField__c, or if you wanted a new custom field on Account, it'd be Account.newField__c. Share and Enjoy."
SquallSquall

I attempted to run that sample, and I received the following exception:

Unable to generate a temporary class (result=1).
error CS0030: Cannot convert type 'SalesForceMetaDataAPI.metaforce.LayoutItem[]' to 'SalesForceMetaDataAPI.metaforce.LayoutItem'

The exception is thrown upon instantiating the MetadataService class:

Code:
ms = new MetadataService();


 

SuperfellSuperfell
There's some issue with the new v13 metadata WSDL and .NET, we're currently investigating.
SquallSquall
I found a workaround to the problem.  After the MetaData web reference is added, open the corresponding Reference.cs file.  Replace all occurrences of LayoutItem[][] with LayoutItem[].