You need to sign in to do that
Don't have an account?

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?
-Brian.
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) {
System.Threading.Thread.Sleep(1000);
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)
Console.WriteLine(r.message);
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,
-Brian.
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.