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.
Rule__c
field when you do so. Any debug logs? Any errors? – Eric Feb 21 '17 at 15:10