3

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

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

E.g.

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";
}
else
{
    cf.Metadata.formula = "Name";
}
SaveResult[] saveResults = toolingService.update(new sObject[] { cf });
Assert.AreNotEqual(0, saveResults.Length);
SaveResult sr = saveResults[0];
Assert.IsTrue(sr.success);
|improve this answer|||||
2

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.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.