My organization has a number of formula fields that are used to link to services and sites outside of salesforce. In our production environment these need to point to the production sites and in our sandbox they need to point to our test sites. Whenever we do a refresh these are overwritten with the production values and it is very tedious to go through and modify all of these fields one by one. Is there anyway we could script the update of these? Does apex have any means of modify not an individual record but the metadata about a particular record type?

2 Answers 2


You could try using the Metadata or Tooling API to update the CustomField record.


The Metadata API has CustomField. If you can modify the formula then you can alter the value for each environment.

From Apex you may wish to use the Apex Wrapper Salesforce Metadata API. There is an example there of creating a CustomField in Apex that could be adapted to changing the formula of an existing field.

Here is an example using the SOAP version of the ToolingAPI from .NET to alter the formula of a formula field.

// Your code to get an instance of the Tooling Service for
// an active Salesforce Session will probably differ.
ToolingServiceWrapper toolingService = salesforceSession.GetToolingService();

sObject[] customFields = toolingService.retrieve("Id, Metadata", "CustomField",
                                                 new string[] { "00N70000003KVWfEAO" });
Assert.AreNotEqual(0, customFields.Length);

CustomField cf = (CustomField)customFields[0];
if (cf.Metadata.formula == "Name")
    cf.Metadata.formula = "$Organization.Name";
    cf.Metadata.formula = "Name";
SaveResult[] saveResults = toolingService.update(new sObject[] { cf });
Assert.AreNotEqual(0, saveResults.Length);
SaveResult sr = saveResults[0];

If the fields don't change very often (i.e. they don't change aside from switching between production and test sites), then you could just keep a single sandbox with the correct sandbox configuration, and deploy those fields only via a change set to recently refreshed sandboxes.

If the actual function of these fields changes often though then this would require some extra maintenance. But it is relatively easy to set up.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .