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
Saloni KhedkarSaloni Khedkar 

set check box value as false

Hi,
I have written a trigger which considers value of the checkbox as true.
can you help me in the code to take Approved__c values as false so that the error message works when Approved__c= False

trigger OpportunityProductCheck on OpportunityLineItem (before insert) {
    set<Id> oppIds = new set<id>();
    set<Id> prodIds = new set<Id>();
    map<String,Boolean> prodcheckMap = new map<String,Boolean>();
    for(OpportunityLineItem oli : Trigger.New){
        oppIds.add(oli.OpportunityId);
        prodIds.add(oli.Product2Id);
    }
    Map<Id,Opportunity> oppMap = new Map<Id,Opportunity>([Select Id,Account.BillingCountry from Opportunity where ID IN: oppIds]);
    Map<Id,Product2> prodMap = new Map<Id,Product2>([Select Id,Name from Product2 where Id IN: prodIds]);
    for(Country_Registration__c prod : [Select Id,Approved__c,Part_number__c,Country__c from Country_Registration__c]){
        String key = '';
        if(!String.isEmpty(prod.Part_number__c) && !String.isEmpty(prod.Country__c)){
            key = prod.Part_number__c + prod.Country__c;
            prodcheckMap.put(key, prod.Approved__c );
        }
    }
    
    for(OpportunityLineItem oli : Trigger.New){
        string prodName = prodMap.containsKey(oli.Product2Id) ? prodMap.get(oli.Product2Id).Name : '';
        string acccountry = oppMap.containsKey(oli.OpportunityId) ? oppMap.get(oli.OpportunityId).Account.BillingCountry : '';
        //string olikey = prodMap.get(oli.Product2Id).Name + oppMap.get(oli.OpportunityId).Account.BillingCountry;
        string olikey = prodName + acccountry;
        if(prodcheckMap.containsKey(olikey)  ){
            boolean bool = prodcheckMap.get(olikey);
           
            if(bool){
                String errormsg = prodName + ' is NOT registered for ' + acccountry;
                oli.addError(errormsg);
            }
        }
    }
}
Maharajan CMaharajan C
Hi Saloni,

Just change the below if condition - add exclamatory to check the value as false:

 if(!bool){
         String errormsg = prodName + ' is NOT registered for ' + acccountry;
         oli.addError(errormsg);
  }

Thanks,
Maharajan.C