2

Trying to compare 2 collections, list and list and for all the cases inserted that match any rule, create a record in other object "caseRulesMatched". Case has 3 custom fields f1, f2, f3 and while inserting if these fields match to the fields in pre-defined rules, would like to retrieve the matching values on both case and rule matched, insert a record in caserulesMatched.

list<case> m = [select id, casenumber, accountId, f1, f2, f3 from case limit 10];
list<rules__c> r = [select f1, f2, f3, status__c, due_date__c from rules__C];
list<caserulesMatched> l = new list<caserulesMatched>();

map<string, rules__C> ruleMap = new map<string, rules__C>();
for(rules__c r1 : r){
   string s = r1.f1+r1.f2+r1.f3;
   ruleMap.put(s, r1);
}

for(case c : m){
   string s1 = c.f1+c.f2+c.f3;
   if(ruleMap.containsKey(s1){
     caserulesMatched cR = new caserulesMatched();
     cR.casenumber__c = c.casenumber__c;
     cR.status__C = ruleMap.get(s1).status__c;
     l.add(cR);
   }
}
insert l;
}

I am sure the above is not best approach but it is working perfectly for me. The problem is if I have 2 rules with same values for f1, f2, f3. I am unable to build proper ruleMap as the keys are unique. Please suggest a better way to compare maps.

Thanks

  • if two rules has same values for f1, f2, f3. Then how you differeniancte between them – Tushar Sharma Sep 15 '15 at 5:06
  • There are values other than f1, f2, f3 but these 3 are primary for me – sf_user Sep 15 '15 at 5:10
2

You can try this. Change this

map<string, rules__C> ruleMap = new map<string, rules__C>();
for(rules__c r1 : r){
string s = r1.f1+r1.f2+r1.f3;
ruleMap.put(s, r1);
}

into

map<string, List<rules__C>> ruleMap = new map<string, List<rules__C>>();
    for(rules__c r1 : r){
    string s = r1.f1+r1.f2+r1.f3;
   if(ruleMap.containsKey(s)) {
    List<rules__C> rlList = ruleMap.get(s);
    rllist.add(r1);
    rulemap.put(s, rllist);
}  else {
     List<rules__C> rlList = List<rules__C>();
    rllist.add(r1);
   rulemap.put(s, rllist);
}

    }

now if you have multiple rule with same value then map will handle them and in your next for loop you can differentiate based on your other fields value. Hopesit helps you.

| improve this answer | |
  • Thanks Tushar. Working on it. will get back shortly. Thanks – sf_user Sep 15 '15 at 5:32
  • 2
    @Tushar, removed one line of your code, I believe that line will cause issue. Other than that, it looks good to me. – Lance Shi Sep 15 '15 at 5:42
  • @LanceShi you removed rulemap.put(s, rllist); this line from if condition now new record is not added in the list. I think we need that line. So i am adding that back. Please let me know what you found wrong here. – Tushar Sharma Sep 15 '15 at 5:47
  • The reason I removed that is because it is under the condition: if(ruleMap.containsKey(s)). So rulemap already contains the key s. And if you put another pair with the key s, it will throw exception I believe. And it is not needed because the list of rules you get is a reference. So when you add r1 into it, you actually already added it into ruleMap.get(s). – Lance Shi Sep 15 '15 at 5:50
  • @LanceShi i don't think it will give an exception . it will simply overwrite the old value with new one with new value added. I used this already it many places. – Tushar Sharma Sep 15 '15 at 5:56

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.