+ Start a Discussion

Is it possible to write a PHP script which will change a SalesForce object field's type?


My client would not only like to be able to backup his SalesForce environment, but also be able to restore from that backup.


So far as I know, while many options make it possible to replicate SalesForce data elsewhere, there are no easy options for restoring data, so I've been trying to figure out how to do this.


Among the obstacles,  it is my understanding that Autonumber fields can not be overridden.  So, to properly restore sObjects with Autonumber fields, it will be necessary to first convert Autonumber fields to Text, and then restore them to Autonumber after the data has been restored (inserted).


Would it be possible to do this through the SalesForce API using PHP to develop the restoration solution?


If so,  how?





The metadata API can be used to change field type, however not being a PHP person, i have no idea how easy or hard it may be to call from PHP.

SimonF wrote:
The metadata API can be used to change field type, however not being a PHP person, i have no idea how easy or hard it may be to call from PHP.
Cheers for the confirmation; this has in fact been my own suspicion since it can clearly be done from the Force.com IDE for eclipse.
Would you happen to know the way this behaviour might be scripted in any language?
 While, for the most part, it would be easiest for me to build a solution in PHP, PHP itself isn't a requirement.
 Also, perhaps if I know how it can be done in some other language, perhaps I can figure out how to adapt this to PHP.
 Best regards,



Here's some c# code that changes an existing field called Foo on a custom object called Foo to be a text(120) field. Its basically setting up the new definition of the custom field, embeding that in a UpdateMetadata structure, than calling the update call on the metadata API, and then polling for the outcome.



            sf.SforceService s = new sf.SforceService();

            sf.LoginResult lr = s.login("un", "pwd");


            md.MetadataService d = new md.MetadataService();

            d.Url = lr.metadataServerUrl;

            d.SessionHeaderValue = new md.SessionHeader();

            d.SessionHeaderValue.sessionId = lr.sessionId;


            md.CustomField f = new md.CustomField();

            f.fullName = "Foo__c.FooId__c";

            f.label = "Foo";

            f.length = 120;

            f.lengthSpecified = true;

            f.type = md.FieldType.Text;


            md.UpdateMetadata u = new md.UpdateMetadata();

            u.currentName = "Foo__c.FooId__c";

            u.metadata = f;


            md.AsyncResult r = d.update(new md.UpdateMetadata[] { u })[0];

            while (!r.done) {


                r = d.checkStatus(new string[] { r.id })[0];


            if (r.state == md.AsyncRequestState.Completed)

                Console.WriteLine("Field type change complete");  

            else if (r.state == md.AsyncRequestState.Error)


I don't know if the PHP toolkits support the metadata api, but if not, and this is all you need to do, it ought to be reasonability straightforward to construct the xml requests manually, there's only 2 needed, one for the update call,and one for the checkStatus call.

Cheers for the responses.


Let's say, hypothetically that I opt for the second option which may be easier for me than developing the entire solution in C#.


What would an example of these XML requests look like?


What are my options for submitting the requests?


Best regards,




The PHP Toolkit seems to have limited support for the Metadata API, check out SforceMetadataClient.php and SforceMetaObject.php. 


They haven't implemented all the methods defined in the Metadata WSDL (unfortunately, update() is missing), *BUT* I suspect that if you just copy the create() method in SforceMetadataClient.php and rename it to update, e.g.


public function update($obj) {

$encodedObj = new stdClass();

$encodedObj->metadata = new SoapVar($obj, SOAP_ENC_OBJECT, 'CustomObject', $this->namespace);
return $this->sforce->update($encodedObj);


everything will go swimmingly.   The SOAP XML encoding should be the same (it's the same for delete() as well).  Let me know if you have trouble and I'll figure out something in PHP and post it.


Edit:  Added $encodedObj = new stdClass() in the first line of the method, better to declare explicitly rather than to rely on PHP to create the object for you like the Toolkit is doing.

Message Edited by phpandsfdc on 11-20-2009 11:35 AM