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
VisionaryVisionary 

need help bulkifying

i have a pricebook setup where price book has a multiselect picklist called Customer_Grouping_Codes

example:
Pricebook1 has Customer_Grouping_Codes: a1,a2,a3,a5,a9,a10
Pricebook2 has Customer_Grouping_Codes: a4,a6,a7,b1,b2
etc....

- on my account object i have a field called Customer_Group_1_Code__c(this is uploaded from source data warehouse)
- account: abc has Customer_Group_1_Code__c = a3
- I now have a field called Default_Price_Book__c on Account that is Lookup(Price Book)
- what i need to do is set this Account.Default_Price_Book__c on before insert, before update to the Pricebook2.id corresponding to Account.Customer_Group_1_Code__c 




here is what i have so far: 

trigger AccountPriceBookSelector on Account (before insert, before update) {
	
    for(Account acc : Trigger.new)
    {
        if(acc.Customer_Group_1_Code__c!=null)
        {
            
            List<Pricebook2> pb = [select id, name from PriceBook2 where Customer_Grouping_Codes__c includes (:acc.Customer_Group_1_Code__c) ];
             if(!pb.isEmpty())
             {
            	acc.Default_Price_Book__c=pb[0].id;
             }
        
        }
        else
        {
            List<Pricebook2> stdPBL =  [select id from Pricebook2 where IsStandard = TRUE];
            acc.Default_Price_Book__c = stdPBL[0].id;
        }
    }
}
 

I'm just not sure where to start with this - it looks so easy (pseudo code) but i'm having trouble

ManojjenaManojjena
Hi Visionary,

Try with belwo code ,
 
trigger AccountPriceBookSelector on Account (before insert, before update) {
	Pricebook stdPBL =  [select id from Pricebook2 where IsStandard = TRUE LIMIT 1];
	Set<String> customerGrpCodeSet=new Set<String>();
	Map<String,Id> customerGroupCodeWithProductId=new Map<String,Id>();
    for(Account acc : Trigger.new){
        if(acc.Customer_Group_1_Code__c!=null){
           customerGrpCodeSet.add(acc.Customer_Group_1_Code__c);			
		}
	}
	if(!customerGrpCodeSet.isEmpty()){
	    for(Pricebook2 prcBok: SELECT id, Name FROM PriceBook2 WHERE  Customer_Grouping_Codes__c IN : customerGrpCodeSet){
			customerGroupCodeWithProductId.put(prcBok.Customer_Grouping_Codes__c,prcBok.Id);
	    }
	}
	for(Account ac : Trigger.new){
		if(ac.Customer_Group_1_Code__c!=null){
			if(customerGroupCodeWithProductId != null  && customerGroupCodeWithProductId.get(ac.Customer_Group_1_Code__c) != null){
				ac.Default_Price_Book__c=customerGroupCodeWithProductId.get(ac.Customer_Group_1_Code__c); 
			}
        }else{
			ac.Default_Price_Book__c=stdPBL.id;
	    }
	
	}
}

Let me know if it helps !!
Thanks
Manoj
VisionaryVisionary
so here is what i modified it to to work, but soql on line 11 is not returning anything
trigger AccountPriceBookSelector on Account (before insert, before update) {
    Pricebook2 stdPBL =  [select id from Pricebook2 where IsStandard = TRUE LIMIT 1];
    Set<String> customerGrpCodeSet=new Set<String>();
    Map<String,Id> customerGroupCodeWithProductId=new Map<String,Id>();
    for(Account acc : Trigger.new){
        if(acc.Customer_Group_1_Code__c!=null){
           customerGrpCodeSet.add(acc.Customer_Group_1_Code__c); 
            system.debug('***************************** acc.CustomerGC1: '+acc.Customer_Group_1_Code__c);
        }
    }
    if(!customerGrpCodeSet.isEmpty()){
        for(Pricebook2 prcBok: [SELECT id, Name FROM PriceBook2 WHERE  Customer_Grouping_Codes__c in :customerGrpCodeSet]){
            customerGroupCodeWithProductId.put(prcBok.Customer_Grouping_Codes__c,prcBok.Id);
            system.debug('***************************** PrcBokID: '+prcBok.Id+'*****************Codes: '+prcBok.Customer_Grouping_Codes__c);
        }
    }
    for(Account ac : Trigger.new){
        if(ac.Customer_Group_1_Code__c!=null){
            if(customerGroupCodeWithProductId != null  && customerGroupCodeWithProductId.get(ac.Customer_Group_1_Code__c) != null){
                ac.Default_Price_Book__c=customerGroupCodeWithProductId.get(ac.Customer_Group_1_Code__c);
                system.debug('****************************if* Set: '+customerGroupCodeWithProductId.get(ac.Customer_Group_1_Code__c));
            }
       }else{
            ac.Default_Price_Book__c=stdPBL.id;
           system.debug('****************************else* Set: '+stdPBL.id);
        }
     
    }
}

i tried "...Where Customer_Grouping_codes__c includes (:customerGrpCodeSet)"
but that is giving an error in dev console: Invalid bind expression type of set<string> for column of type string

BUT - it works in the Query Editor when i type it like "...Where Customer_Grouping_Codes__c includes ('A1')