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 object

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

Can this be achievable ? If so, can i receive some help on building the trigger ?

Thank you in advance.
Best Answer chosen by Saloni Khedkar
Maharajan CMaharajan C
Hi Saloni,

We can write trigger to handle this scenario:

Please refer the below code:

am considering Product__c  as Custom Object in below code
 
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(Product__c prod : [Select Id,Inactive__c,Product__c,Country__c from Product__c]){
        String key = '';
        if(!String.isEmpty(prod.Product__c) && !String.isEmpty(prod.Country__c)){
            key = prod.Product__c + prod.Country__c;
            prodcheckMap.put(key, prod.Inactive__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 Inactive for ' + acccountry;
                oli.addError(errormsg);
            }
        }
    }
}

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi Saloni,

We can write trigger to handle this scenario:

Please refer the below code:

am considering Product__c  as Custom Object in below code
 
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(Product__c prod : [Select Id,Inactive__c,Product__c,Country__c from Product__c]){
        String key = '';
        if(!String.isEmpty(prod.Product__c) && !String.isEmpty(prod.Country__c)){
            key = prod.Product__c + prod.Country__c;
            prodcheckMap.put(key, prod.Inactive__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 Inactive for ' + acccountry;
                oli.addError(errormsg);
            }
        }
    }
}

Thanks,
Maharajan.C
This was selected as the best answer
Saloni KhedkarSaloni Khedkar
Hi Maharajan,
Thank you for helping me with the code. I did try this however, the error message did not apprear.
I do have a question on one of the lines:
for(Product__c prod : [Select Id,Inactive__c,Product__c,Country__c from Product__c])

here on this line, you have considered the object Product__c which being the custom object. However, you have used Product__c (same field in the select statement). I am little confused here.

Also, can you give a short description on flow ? I am new to coding.

Thanks a lot for all your help. 

Looking forward. 
Fender JanisonFender Janison
Thanks for addressing this topic. I was looking for the information regarding the same. Keep sharing the info walgreenslistens (https://www.walgreenslistens.biz/), kudos.
Saloni KhedkarSaloni Khedkar
Thank you @Maharajan,

I was able to execute the code.

If the Inactive value is FALSE, what would be the changes in the code?

Thank you so much for helping. I really appreciate the efforts.
Saloni KhedkarSaloni Khedkar

Hi @Maharajan
The code workks great

I am trying to add another condition where it should fire the same error. 
The condition now compares the Custome table with the product table.
I am trying to add another condition where if the product is not in the custom table but billing country is 'United states of America'

can this condition be accomodated in the same code?