0

Assume that on update of an sObject record, I want to execute some code only if a particular condition is satisfied. Now the requirements are very dynamic, and conditions will change often. Infact I need the ability to switch off/on a condition without the need of code deployment.

To solve this issue, I have created a custom metadata with 2 fields: Object and Condition. Below can be sample records of this custom metadata.

Name            Object      Active      Condition           
--------        -------     --------    -----------------------
Rule1           Account     N           Type='Active' AND Revenue > 10000
Rule2           Account     Y           Source='Check' AND Revenue > 10000
Rule3           Contact     Y           Name='System' OR Salary >= 80000

Is it possible that in a trigger, I can check the conditions by just fetching the information provided in the custom metadata? Or is there any existing framework which I can reuse for my needs?

trigger AccountTrigger on Account (after update) {
    if (Trigger.isUpdate) {
        // Call custom metadata where Object = Account
        ...
        ...
        switch on Name {
           when 'Rule1' {
               if (Condition) {...}
           }
           when 'Rule2' {
               if (Condition) {...}
           }
        }
    }
}
1
  • Looks like you are looking for a rule engine to auromate tasks. I have sent a couple a couple of paid options but not a full fledged open source tool yet. – manjit5190 Sep 27 '20 at 4:58
0

In apex, you can access a custom metadata using SOQL (similar to how you would access a sobject, but the custom metadata object suffix would be __mdt). Check out the examples given here.

Or, you can make use of Metadata.Operations.retrieve method. Sample code shown here.

Few points you should focus while accessing the metadata and using it in your code are given below:

  • How are you planning to break down the conditions and utilize them in the code? Your question shows simple AND condition rule at the moment, but eventually as you try to scale up the application you might have to incorporate nested (sometimes, deeply) AND & OR conditions. So, splitting them up using String methods will not really be a good practice in the long run.
  • When you have similar (but slightly different) rules, you would want to apply some logic to prioritize and process only the metadata that is relevant. In other words, you should try and avoid redundant check on the rules, but write code that captures the most apt one at the earliest. Consider, having another field like priority that would aid this. Most critical and comprehensive rule should be picked up first.

Consider, capturing the metadata information in a static variable (Map or List) which would be a good practice for bulk processing (esp., in chunks of data processed by triggers).

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.