You need to sign in to do that
Don't have an account?
help me with below trigger
i have custom field product type on Opportunity , when oppolineItem is created i have to checck productFamily equal to product type if not then show error my code is
trigger CheckProductTypeonOppLineItem_whenItIsCreated on OpportunityLineItem (after insert) {
set<id> setIds=new set<id>();
for(OpportunityLineItem opp:Trigger.new){
setIds.add(opp.Id);
}
list<OpportunityLineItem> opplist=[select id,opportunity.Product_type__c, Product2.Family from OpportunityLineItem where id In:setIds];
for(OpportunityLineItem o:opplist){
if(o.Product2.Family != o.opportunity.Product_type__c){
o.addError('Product family must be same ');
}
}
}
any other way ot writing above trigger .
Thank you
trigger CheckProductTypeonOppLineItem_whenItIsCreated on OpportunityLineItem (after insert) {
set<id> setIds=new set<id>();
for(OpportunityLineItem opp:Trigger.new){
setIds.add(opp.Id);
}
list<OpportunityLineItem> opplist=[select id,opportunity.Product_type__c, Product2.Family from OpportunityLineItem where id In:setIds];
for(OpportunityLineItem o:opplist){
if(o.Product2.Family != o.opportunity.Product_type__c){
o.addError('Product family must be same ');
}
}
}
any other way ot writing above trigger .
Thank you
Are you getting any issue.Please specify.
Thanks!
Please use below code:
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh
Hi,
Lokesh with the help of trigger you cant get the error message as in case of after insert
and 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
trigger 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');
}
}
}