Created
August 9, 2017 03:38
-
-
Save JitendraZaa/f4389d092ec43d4ad17e2ba8aa45697d to your computer and use it in GitHub Desktop.
Example of SFDCRules - https://github.com/JitendraZaa/SFDCRules
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
| //Define set of all allowed operators | |
| //Mostly we don't need to change this, it can be added in setup method | |
| Operations opObj = Operations.getInstance(); | |
| opObj.registerOperation(OperationFactory.getInstance('&&')); | |
| opObj.registerOperation(OperationFactory.getInstance('==')); | |
| opObj.registerOperation(OperationFactory.getInstance('!=')); | |
| opObj.registerOperation(OperationFactory.getInstance('||')); | |
| opObj.registerOperation(OperationFactory.getInstance('(')); | |
| opObj.registerOperation(OperationFactory.getInstance(')')); | |
| opObj.registerOperation(OperationFactory.getInstance('<')); | |
| opObj.registerOperation(OperationFactory.getInstance('<=')); | |
| opObj.registerOperation(OperationFactory.getInstance('>')); | |
| opObj.registerOperation(OperationFactory.getInstance('>=')); | |
| //Define bindings, which will replace variables while | |
| //evaluating rules | |
| Map<String, String>bindings = new Map<String, String>(); | |
| bindings.put('Case.OwnerName__c'.toLowerCase(), 'Jitendra'); | |
| bindings.put('Case.IsEscalated'.toLowerCase(), 'false'); | |
| bindings.put('Case.age_mins__c'.toLowerCase(), '62'); | |
| //Define rule | |
| String expr = 'Case.OwnerName__c == Minal || ( Case.age_mins__c < 75 && Case.IsEscalated == false )' ; | |
| //Initialize Rule Engine | |
| Rule r = new Rule().setExpression(expr); | |
| //Evaluate rule with Binding values | |
| Boolean retVal = r.eval(bindings) ; | |
| //Check if expected result is correct | |
| System.assertEquals(true, retVal); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment