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 

Trigger for comparing two field values from different objects

I have a custom object which has a list of products, country field and checkbox- inactive.

Use case: 
When I create an opportunity and add OpportunityLineItem, The line item should compare with the products field from the custom object.
Also, the Opportunity should look at Bill to country from the account attached and compare with the country field from the custom object.
And if the record matches and the "Inactive" checkbox from the custom object is true, then it should throw an error and not allow to save the opportunity.

For example,
Custom object:
Product: Pencil
Country: Mexico
Inavtive: True

Opportunity:
OpportunityLineItem: Pencil
Account - Bill to Country: Mexico
ERROR FLAG
 
Saloni KhedkarSaloni Khedkar
I also have a code for this:

Considering Country_Restriction__c as custom object



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_Restriction__c prod : [Select Id,    Approved__c,    Part_Number__c,    Country__c from Country_Restriction__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 + ' needs approval for ' + acccountry;
                oli.addError(errormsg);
            }
        }
    }
}
Saloni KhedkarSaloni Khedkar
I am looking to add another condition in this code. I am a begineer in Apex and would need help.

The condition is:
If the product is not in the custom object product list (product field) and is attached on an opportunity having Account with BillingCountry in United states or Puerto Rico or Phiilippines, then also throw the same error.

is this possible in the same code?