Last active
February 23, 2021 19:53
-
-
Save robertStrunk/8c321d05c1db76ca4750ed81e802d9ab to your computer and use it in GitHub Desktop.
Unconventional approach to getting a formula field to update in apex. The tradeoff is it consumes extra limits for the queries and DML operations for update and rollback etc. Example is non-bulkified but could easily be made to be so.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// get existing record | |
SBQQ__Quote__c existingQuote = [ | |
SELECT formula_Field__c | |
FROM SBQQ__Quote__c | |
WHERE Id = 'xxxxxxxxxxxxxxxxxx' | |
]; | |
// capture the current value of the formula field | |
Decimal oldFormulaValue = existingQuote.formula_Field__c, | |
newFormulaValue, | |
reQueriedOldFormulaValue; | |
// generate rollback point | |
Savepoint sp = Database.setSavepoint(); | |
// update fields that impact the formula | |
existingQuote.Formula_input_1__c = 5000; | |
existingQuote.Formula_input_2__c = 10000; | |
update existingQuote; | |
// query for the new formula value | |
newFormulaValue = [ | |
SELECT formula_Field__c | |
FROM SBQQ__Quote__c | |
WHERE Id = :existingQuote.Id | |
].formula_Field__c; | |
//now that we have the updated formula value we can revert the changes in the DB | |
Database.rollback(sp); | |
// requery the formula field to help prove the rollback happened | |
reQueriedOldFormulaValue = [ | |
SELECT formula_Field__c | |
FROM SBQQ__Quote__c | |
WHERE Id = :existingQuote.Id | |
].formula_Field__c; | |
// assert that the rollback happened | |
System.assertEquals( oldFormulaValue, reQueriedOldFormulaValue ); | |
// assert that we have the updated formula value | |
System.assertNotEquals( oldFormulaValue, newFormulaValue ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment