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   
    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
    //stroing of price study ids in variable pricestudyIds

  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;
  //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.

  • 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
    Commented Feb 21, 2017 at 15:10
  • Ensure trigger should not fire recursively.
    – Reshma
    Commented Feb 22, 2017 at 5:36

2 Answers 2


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


Not convinced it is happening on Update


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...

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .