Skip to main content Nehmen Sie am Agentforce Virtual Hackathon teil! Hier werden innovative Lösungen erstellt, von denen die beste mit einem Hauptpreis von 50.000 USD prämiert wird. Jetzt registrieren. Es gelten allgemeine Teilnahmebedingungen.
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 Antworten
  1. 28. Juni 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');

        }

      }

    }
  2. 28. Juni 2021, 16:45

    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

  3. 28. Juni 2021, 15:25
    Hi Lokesh,

    Please use below code:

     

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

    }

    }

    }

    if you need any assistanse, Please let me know!!

    Kindly mark my solution as the best answer if it helps you.

    Thanks

    Mukesh 
0/9000