Hi Friends,
Imagine a scenario where you need to check complex conditions on an SObject record and don't want to or can't create a formula field on the object? No more complex apex code, Dynamic formula evaluation solves your problem. Let us understand it with an example.
Problems solved by dynamic formula evaluation
- Create a formula field on the fly in the apex code.
- Create a formula for the Wrapper class object in apex.
- Calculate formula values at run time in apex.
Dynamic formula evaluation in Apex
Dynamic formula evaluation: an example with Standard Object
FormulaEval.FormulaInstance isPrimeCustomer = Formula.builder() .withType(Account.SObjectType) .withReturnType(FormulaEval.FormulaReturnType.BOOLEAN) .withFormula('AnnualRevenue > 10000 AND ISPICKVAL(Industry, "Information Technology") AND Prime_Customer__c = TRUE'); Account acc1 = new Account( Name = 'Test Dynamic Formula Account', AnnualRevenue = 20000, Industry = 'Information Technology', Prime_Customer__c = true ); isPrimeCustomer.evaluate(acc1); // returns TRUE Account acc2 = new Account( Name = 'Test Dynamic Formula Account 2', AnnualRevenue = 20000, Industry = 'Information Technology', Prime_Customer__c = false ); isPrimeCustomer.evaluate(acc2); // returns FALSE Account acc3 = new Account( Name = 'Test Dynamic Formula Account 3', AnnualRevenue = 10001, Industry = 'Chemical', Prime_Customer__c = true ); isPrimeCustomer.evaluate(acc2); // returns FALSE
Dynamic formula evaluation: an example with Wrapper Class
global class Car { global Double lengthInMeters; global Integer numOfSeats; global String name; } Car aBoat = new Car(); aBoat.lengthInMeters = 5.2; aBoat.numOfSeats = 4; aBoat.name = 'My Super car'; FormulaEval.FormulaInstance isLongCar = FormulaEval.FormulaBuilder.builder() .withReturnType(FormulaEval.FormulaReturnType.STRING) .withType(Car.class) .withFormula('IF(lengthInMeters > 5, "Long", "Short")') .build(); isLongCar.evaluate(aBoat); // Returns "Long"
What happens if the field referenced in dynamic formula evaluation is deleted?
more content coming soon.
No comments :
Post a Comment
Hi there, comments on this site are moderated, you might need to wait until your comment is published. Spam and promotions will be deleted. Sorry for the inconvenience but we have moderated the comments for the safety of this website users. If you have any concern, or if you are not able to comment for some reason, email us at rahul@forcetrails.com