Retrieve the formulas from the Salesforce Formula fields

0 votes

I am looking for a way to get the formulas from Salesforce Formula fields. We are using the CDATA Drivers to connect to Salesforce. But I am not seeing any option to retrieve the Salesforce formulas.

Mar 2, 2022 in SalesForce by surbhi
• 3,820 points
2,289 views

1 answer to this question.

0 votes

In Apex you can use "describe" calls. If all other options fail - you could build a custom service that returns this data to you.

Schema.DescribeFieldResult dfr = Account.Address__c.getDescribe();
System.debug(dfr.getCalculatedFormula());
// BillingStreet & BR() & BillingCity & BR() &  BillingPostalCode & BR() &  BillingCountry & BR() &  Street2__c & BR() & Street3__c

You've tagged metadata API so if you really use that - similar info should be available in it.

<CustomField>
    <fullName>Address__c</fullName>
    <externalId>false</externalId>
    <formula>BillingStreet &amp; BR() &amp; BillingCity &amp; BR() &amp;  BillingPostalCode &amp; BR() &amp;  BillingCountry &amp; BR() &amp;  Street2__c &amp; BR() &amp; Street3__c</formula>
    <formulaTreatBlanksAs>BlankAsZero</formulaTreatBlanksAs>
    <label>Address</label>
    <required>false</required>
    <trackHistory>false</trackHistory>
    <type>Text</type>
    <unique>false</unique>
</CustomField>

In REST API it's similar, a call to /services/data/v48.0/sobjects/Account/describe will return (among others)

{
    "aggregatable" : true,
    "aiPredictionField" : false,
    "autoNumber" : false,
    "byteLength" : 3900,
    "calculated" : true,
    "calculatedFormula" : "BillingStreet & BR() & BillingCity & BR() &  BillingPostalCode & BR() &  BillingCountry & BR() &  Street2__c & BR() & Street3__c",
    "cascadeDelete" : false,
    (...)
    "formulaTreatNullNumberAsZero" : true,
    "groupable" : false,
    "highScaleNumber" : false,
    "htmlFormatted" : true,
    "idLookup" : false,
    "inlineHelpText" : null,
    "label" : "Address",
    "length" : 1300,
    "mask" : null,
    "maskType" : null,
    "name" : "Address__c",
    (...)
    "type" : "string",
    "unique" : false,
    "updateable" : false,
    "writeRequiresMasterRead" : false
  }

And finally there's Tooling API where you can query the metadata like you'd query normal database tables. But the core of what you need will be hidden in a JSON field you'd have to parse. If your tool is some ETL - check if it can query FieldDefinition table.

/services/data/v48.0/tooling/query?q=SELECT+Metadata+FROM+FieldDefinition+WHERE+EntityDefinitionId+=+'Account'+AND+QualifiedApiName+=+'Address__c'

Hope this helps!

Check out this Salesforce Course to learn more.

Thanks!

answered Mar 2, 2022 by surbhi
• 3,820 points

edited Jun 19, 2023 by Khan Sarfaraz

Related Questions In SalesForce

+1 vote
1 answer

Apex (Salesforce) read json from response and store it in a string variable

String jsonStr = '{ "oktaToken":"eyJraWQiOiIyNlN1NHFMNnVVZTVJX2M5X2Z3WmZvX09ON0dNUHRtQzlEeHFsTGplLS00IiwiYWxnIjoiUlMyNTYifQ.eyJ2ZXIiOjEsImp0aSI6IkFULlVINmdIOF9BcWJ" }'; Map<String, String> ...READ MORE

answered Mar 3, 2022 in SalesForce by CoolCoder
• 4,420 points

edited Jun 27, 2023 by Khan Sarfaraz 9,693 views
0 votes
0 answers

Apex (Salesforce) read json from response and store it in a string variable

I am trying to hit a API ...READ MORE

Mar 4, 2022 in SalesForce by surbhi
• 3,820 points
1,234 views
0 votes
1 answer

In Salesforce what is the difference between Page Layouts and Visual force Pages?

A built-in standard mechanism for displaying information ...READ MORE

answered Mar 9, 2022 in SalesForce by CoolCoder
• 4,420 points
799 views
0 votes
1 answer

In Salesforce Whats the difference between Page Layouts and Visual force Pages?

A built-in standard mechanism for displaying information ...READ MORE

answered Mar 9, 2022 in SalesForce by CoolCoder
• 4,420 points
435 views
+2 votes
2 answers

Salesforce Interview questions

Here are some questions very important for ...READ MORE

answered Jan 11, 2019 in Career Counselling by Suresh
• 720 points
3,461 views
0 votes
1 answer

How to connect to salesforce from tableau?

Hi, follow these steps to connect to Salesforce: 1. ...READ MORE

answered Mar 25, 2019 in Tableau by Cherukuri
• 33,030 points
1,070 views
0 votes
1 answer

Power BI - Salesforce

Hi, Follow below steps: 1. Go to Data source. 2. ...READ MORE

answered Mar 25, 2019 in Power BI by Cherukuri
• 33,030 points
787 views
0 votes
2 answers

What is the best training for Salesforce ADM-201 Exam?

Hi @Vardhan, I took Edureka's Salesforce Online Training, ...READ MORE

answered Jun 3, 2021 in Others by Jaya
• 140 points

edited Dec 22, 2021 by Soumya 749 views
0 votes
1 answer

Salesforce Formula Text Value Based on Custom Field Picklist Value

You should update the fields using the ...READ MORE

answered Mar 2, 2022 in SalesForce by surbhi
• 3,820 points
6,139 views
0 votes
1 answer

Salesforce Test Class - System.NullPointerException: Attempt to de-reference a null object

Because testAccount.account is never set, it is ...READ MORE

answered Mar 2, 2022 in SalesForce by surbhi
• 3,820 points
2,918 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP