Skip to main content Join the Agentforce Virtual Hackathon to build innovative solutions and compete for a $50k Grand Prize. Sign up now. Terms apply.
Lukesh Karmore (Softsensor) 님이 #Apex에 질문했습니다
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
답변 4개
  1. 2021년 6월 28일 오후 5:56
    Thank you so much for your kind reply Abhinav,mukesh and suraj , i solved it by myself

    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');

        }

      }

    }
로딩 중
0/9000