Skip to main content Rejoignez le hackathon virtuel Agentforce pour créer des solutions innovantes et concourir pour un grand prix de 50 000 $. Inscrivez-vous sans attendre. Offre soumise à conditions.
Lukesh Karmore (Softsensor) a posé une question dans #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 réponses
  1. 28 juin 2021, 17: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');

        }

      }

    }
Chargement
0/9000