0

I have two objects opportunity and price study.Both are related using lookup relationship.

In Opportunity I have picklist called Rule__c with value "rule" and others.when I select a value "rule" I need to insert price study record.for that i wrote a trigger:

trigger InsertEPRecord on Opportunity (after insert,after update){
 List<Price_Study__c> Pricestudy = new List<Price_Study__c>(); //Initialize Price Study Object
 RecordType rt = [SELECT Id, Name FROM RecordType WHERE Name = 'Price Study']; // Quering Record type Name 

 //Insert trigger to check condition Rule field value is "rule"

  for(opportunity opp : Trigger.new){
            //Adding Price study record to price study list
           if((Trigger.oldMap == null || 'Rule' != Trigger.oldMap.get(opp.Id).Rule__c) &&  opp.Rule__c=='rule'){
            Pricestudy.add(new Price_Study__c(Opportunity__c = opp.id));
        }    
    }  

 //Checking Null and Insering Price study list   
    if(Pricestudy.size()>0){
    insert pricestudy;
    }
}

When price study record is inserted it should update parent field value called EP_insert__C.

trigger UpdateAmount on Price_Study__c (after insert){ //Trigger to Update Opportunity EP Field value
  list<Price_Study__c> pslist=new list<Price_Study__c>();  //Intilaize Pricestudy list
  set<Id> listIds = new set<Id>(); //Set of opportunity Ids
  set<Id> pricestudyIds = new set<Id>(); //Set of price study Ids

   //Perfroming Tirgger Insert Operation
  for (Price_Study__c childObj : Trigger.new){
    //Storing of Opportunity ids in variable listids
    listIds.add(childObj.Opportunity__c);
    //stroing of price study ids in variable pricestudyIds
    pricestudyIds.add(childObj.id);
  }

  list<opportunity> opplist = new list<opportunity>();
  //set of pricelist to update opportunity field value 
  for(opportunity opp: [SELECT id, EP_Insert__c,(SELECT ID,Opportunity__c,Auto_Number__c,Current_Year__c FROM Price_Study__r) FROM Opportunity WHERE ID IN :listIds]){
    for(Price_Study__c p:opp.Price_Study__r){
     opp.EP_Insert__c = 'EP/'+P.Current_Year__c+'/'+P.Auto_Number__c;
     }
     opplist.add(opp);
   }
  //DML Operation
  update opplist;
}

Every time I am testing my first code it is working fine .it is insert one record on price study object.

When I am testing together with both triggers they are inserting two price study records.Seriously I cant able to find where the issue lies.

2
  • Going to need a bit more detail, how are you testing, insert, update, what is the status of the Rule__c field when you do so. Any debug logs? Any errors? – Eric Feb 21 '17 at 15:10
  • Ensure trigger should not fire recursively. – Reshma Feb 22 '17 at 5:36
1

I am not 100% cause I would think you would be getting an error but maybe not due to the triggers only firing twice maximum

On Insert

The Opp trigger fires - oldMap is NULL Price Study record is created and fires Price Study Trigger Price Study trigger updates Opp Opp trigger creates another Price Study record

In the above the old map is null the first time and the pick list is not set to "Rule" so it creates the Price Study Records

Update

Not convinced it is happening on Update

0

A couple of issues I see here:

  1. InsertEPRecord Trigger will be creating Pricestudy record on Opportunity insert, and also when Rule__c set to 'Rule'
  2. EP_Insert__c will be updated only with the latest related Price_Study__c record...
  3. Opportunity Trigger will create Pricestudy record, and then after insert Trigger on this record will update related Opportunity record, and this will run Opportunity after update Trigger again...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.