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
Nirmal9114Nirmal9114 

updating multi picklist value on insertion of another multi picklist

I have to write a trigger on before insert. i have 2 fields Country__c and GU__c.
my req is to update GU__C accordingly when user inserts Country. The system should automatically assign GU value(s) based on the country value input within a record. An object is created with records containing country__c with its GU__C.
the fields are multi picklist.
Please suggest where i went wrong.

Apex class:

Public class GuUpdate{
   public static void GuUpdation(List<Opportunity> OppList){
        
               //List<Opportunity> opplist1 = [Select id,Project_Location_s__c from Opportunity where Opportunity in :oppList];
        List<GU_Lookup__c> GuList = new List<GU_Lookup__c>();
        
        Set<id> oppid= new Set<id>();
        for(Opportunity opty : OppList){
            if(opty.Impact_Location_GU__c == null){
             oppid.add(opty.Impact_Location_GU__c)
            }
        }
        
        map<id,GU_Lookup__c> mapGuList=new map<id,GU_Lookup__c>([Select Id, Country__c, GU__c from GU_Lookup__c where Id IN: Gulist]);
                              
        for(Opportunity opp: oppid){
            opp = [Select id,Project_Location_s__c, Impact_Location_GU__c from Opportunity where id IN: oppid];
                if(mapGuList.containsKey(opp.Project_Location_s__c)) {
                    GU_Lookup__c Gu = mapGuList.get(opp.Project_Location_s__c); 
                      mapGuList.put(opp.id);
                           
                    if(mapGuList.Country__c == opp.Project_Location_s__c ){
                        opp.Impact_Location_GU__c ==mapGuList.GU__c;
                    }
                } 
          system.debug('opplist'+opplist);      
          update opplist;
         system.debug('opplist'+opplist);
  
        }
    }
}      
        
SalesFORCE_enFORCErSalesFORCE_enFORCEr
There are lot of issues with this code.
1. oppid will always be blank. Not sure why are you adding Impact_Location_GU__c in oppid and using it to fetch opportunities. I think you should add opty.Id in oppid set.
2. GuList is also blank so mapGuList will always be blank.
3. I dont understand this: GU_Lookup__c Gu = mapGuList.get(opp.Project_Location_s__c); 
4. You can;t add opp.id in mapGuList.
5. You can't access values from a map like this mapGuList.GU__c;
6. You should not run a query inside for loop, it will fail in bullk operations.