Thank you so much for your kind reply Abhinav,mukesh and suraj , i solved it by myselftrigger CheckProductTypeonOppLineItem_whenItIsCreated on OpportunityLineItem (before insert) { set<id> prdtIds=new set<id>(); set<id> oppIds=new set<id>(); for(OpportunityLineItem oli:Trigger.new ){ oppIds.add(oli.opportunityId); prdtIds.add(oli.product2Id); } opportunity opp=[select id ,product_type__c from opportunity where id in:oppIds]; product2 pd=[select id ,family from product2 where id in:prdtIds]; list<OpportunityLineItem> oliList=new list<OpportunityLineItem>(); for(OpportunityLineItem oli:Trigger.new ){ if(opp.product_type__c!=pd.family){ oli.addError('Product family does not match'); } }} Hi,
Lokesh with the help of trigger you cant get the error message as in case of after insertand also you cant get the error message in case of before insert because you cant get the id of Opportunity
so you should use Vf page to achieve this
Thank You
Hi Lokesh,Please use below code:
if you need any assistanse, Please let me know!!trigger CheckProductTypeonOppLineItem_whenItIsCreated on OpportunityLineItem (after insert) {
set<id> setOppIds=new set<id>();
for(OpportunityLineItem oli:Trigger.new){
setOppIds.add(oli.opportunityId);
}
Map<Id, Opportunity> oppMap = [Select Product_type__c from opportunity where id IN: setOppIds];
//list<OpportunityLineItem> opplist=[select id,opportunity.Product_type__c, Product2.Family from OpportunityLineItem where id In:setIds];
for(OpportunityLineItem o:Trigger.New){
if(o.Product2.Family != oppMap.get(o.opportunityId).Product_type__c){
o.addError('Product family must be same ');
}
}
}
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh
4 Antworten