function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
harish reddy 50harish reddy 50 

updating checkboxs on case object based on account multipicklist values

my scenario: i have multipicklist field multivaue__c (first,second,third,fourth,fifth) on account when ever i select some values in picklist field then corresponding checkbox field on case with same names first__c,second__c...,should be checked,As account and case have lookup relation i have written trigger on case
 
problem:how should i check each multi picklist value in if condition
Here is my code:

trigger boxTrigger on case (before insert) {

     LIST<id> ids = new LIST<id>();
     
     LIST<case> cse=new lIST<case>();
     for(case cse1:trigger.new)
     {
          ids.add(cse1.accountid);
     }
     
 List<account> acc = [select multivalue__c,(select id,first__c,second__c,third__c,fourth__c,fifth__c from cases)from account where id in:ids];
     
for(account acc1:acc)
{
   for( case cse1:acc1.cases)
   {
   
       if(acc1.multivalue__c.contains('first'))
       {
          cse1.first__c= true;
       }
        cse.add(cse1);
   }
}
update cse;
}



 
Maharajan CMaharajan C
Hi Harish,

Try the below one:

trigger UpdateChildbasedonParent1 on case (before insert)
{
set<id> AccIds=new set<id>();
for(case con:trigger.new)
{
AccIds.add(con.AccountId);
}
map<Id,Account> RelatedAccounts = new map<Id,Account>([SELECT Id, Name,multivalue__c FROM Account WHERE Id IN :AccIds]);
for(case con:trigger.new)
{
Account Related=RelatedAccounts.get(con.AccountId);
If(con.first__c==false && Related.multivalue__c.contains('first'))
con.first__c=true;
}
}

​Let me know if it works or not!!!

If it works mark this as a best answer!!!

Thanks,
​Raj
harish reddy 50harish reddy 50
thanks for responding maharaja its working ...cant we this with out map..? i am new to apex ...
Maharajan CMaharajan C
Hi Harish,

Try the below which is modified from your code Its works to me:

trigger boxTrigger on case (before insert) {

     LIST<id> ids = new LIST<id>();
     
     LIST<case> cse=new lIST<case>();
     for(case cse1:trigger.new)
     {
          ids.add(cse1.accountid);
     }
     
 account acc = [select Id,multivalue__c from account where id in:ids];
     

   for( case cse1:trigger.new)
   {
   
       if(acc.multivalue__c.contains('first'))
       {
          cse1.first__c= true;
       }
        
   }
}


Can you please Let me know if it works or not and also If you face any problems!!!

If it works don't forget to mark this as a best answer!!!

Thanks,
​Raj
Maharajan CMaharajan C
please choose the best answer which one helps to you!!! it will help others!!!

Thanks,
​Raj