You need to sign in to do that
Don't have an account?

Duplicate ID in After Update Trigger
I am writing a trigger to copy childs field value into parent field value. Let say operational_metrics__c is a parent object and contacts is its child object with lookup relation ship. Contact object has fields like lastname,driver_name__c,vehicle_type__C and operational_metrics__c has driver_name__c field. I need to copy driver_name__c in contact to driver_name__c in operational_metrics__c. I am trying like this
trigger opMetricsTrigger on Operational_Metrics__c (after insert,after update)
{
Operational_Metrics__c[] op=null;
Set<string> opIds = new Set<string>();
Date d1,d2,cDate,pMont,cRegDate;
String dat;
Contact con;
decimal avg,nDays;
Decimal tw;
String convertedDate;
String mTest,actionTaken,oAction;
List<Operational_Metrics__c> c= new LIST<Operational_Metrics__c>();
List<Operational_Metrics__c> cUpdate= new LIST<Operational_Metrics__c>();
List<Operational_Metrics__c> cU= new LIST<Operational_Metrics__c>();
private static boolean run = true;
if(TriggerContextUtility.isFirstRun())
{
run=false;
for (Operational_Metrics__c e : Trigger.new)
{
if(e.u_id__c != null)
opIds.add(e.u_id__c );
}
List<Operational_Metrics__c> opm = [select Unique_Pile_ID__c,Surveyor__c,driver_name__C,u_id__C,Uid_number__c, Composting_Method__c,Pile_Monitoring_Date__c,Average_Temperature__c,Moisture_Test__c,Pile_Monitoring_Action_Taken__c,Other_Action__c,Pile_Input_Register_Date__c,Total_Waste_Input_on_the_Pile__c,Pile_Completion_Stage__c,Date_Pile_COmpleted__c,Curing_Batch_Register_Date__c,No_of_Days__c,No_of_Days_for_piling__c from operational_metrics__c where u_id__c in :opIds];
List<Operational_Metrics__c> uOpm = [select Unique_Pile_ID__c,Surveyor__c,u_id__C,Uid_number__c, Composting_Method__c,Pile_Monitoring_Date__c,Average_Temperature__c,Moisture_Test__c,Pile_Monitoring_Action_Taken__c,Other_Action__c,Pile_Input_Register_Date__c,Total_Waste_Input_on_the_Pile__c,Pile_Completion_Stage__c,Date_Pile_COmpleted__c,Curing_Batch_Register_Date__c,No_of_Days__c,No_of_Days_for_piling__c from operational_metrics__c where u_id__c in :opIds and Pile_Completion_Stage__c='yes'];
List<Operational_Metrics__c> dName=[select Project__c,driver_name__c,Surveyor__c,vehicle_type__c,vehicle_number__c, (SELECT contact.LastName FROM Contacts__r) from operational_metrics__c];
for(Operational_Metrics__c d : dName)
{
if(d.Project__c=='Nalgonda')
{
for(contact cont:d.contacts__r)
{
if(d.Vehicle_Type__c=='Tractor')
{
d.Driver_Name__c=cont.lastname;
// c.add(d);
}
}
}
else if(d.Project__c=='Miryalaguda')
{
System.debug('prj2 '+d);
}
}
for(Operational_Metrics__c ops : opm)
{
if(ops.Surveyor__c=='NLDA Supervisor')
{
ops.Project__c='Nalgonda';
}
else if(ops.Surveyor__c=='Araveli Nagaswara')
{
ops.project__C = 'Miryalaguda';
}
if(ops.Pile_Completion_Stage__c=='yes')
{
d1=ops.Date_Pile_COmpleted__c;
convertedDate=String.valueOf(d1.day())+String.valueOf(d1.month());
tw=ops.Total_Waste_Input_on_the_Pile__c;
cDate=ops.Date_Pile_COmpleted__c;
}
else if(ops.Curing_Batch_Register_Date__c!=null)
{
d2=ops.Curing_Batch_Register_Date__c;
System.debug('records2 are '+ops);
}
else
{
avg=ops.Average_Temperature__c;
mTest=ops.Moisture_Test__c;
actionTaken=ops.Pile_Monitoring_Action_Taken__c;
oAction=ops.Other_Action__c;
pMont=ops.Pile_Monitoring_Date__c;
cRegDate=ops.Curing_Batch_Register_Date__c;
}
if(d1!=null && d2!=null)
{
ops.No_of_Days_for_piling__c=d1.daysBetween(d2);
nDays=ops.No_of_Days_for_piling__c;
c.add(ops);
}
}
for(Operational_Metrics__c u:uOpm)
{
u.Total_Waste_Input_on_the_Pile__c=tw;
u.Average_Temperature__c=avg;
u.Moisture_Test__c=mTest;
u.Pile_Monitoring_Action_Taken__c=actionTaken;
u.Other_Action__c=oAction;
u.Pile_Monitoring_Date__c=pMont;
u.Curing_Batch_Register_Date__c=cRegDate;
u.No_of_Days_for_piling__c=nDays;
u.Curing_Batch_Register_Date__c=d2;
cUpdate.add(u);
}
TriggerContextUtility.setFirstRunFalse();
update c;
update cUpdate;
}
}
But the child record value is not updating into parents record. could anyone help me out
trigger opMetricsTrigger on Operational_Metrics__c (after insert,after update)
{
Operational_Metrics__c[] op=null;
Set<string> opIds = new Set<string>();
Date d1,d2,cDate,pMont,cRegDate;
String dat;
Contact con;
decimal avg,nDays;
Decimal tw;
String convertedDate;
String mTest,actionTaken,oAction;
List<Operational_Metrics__c> c= new LIST<Operational_Metrics__c>();
List<Operational_Metrics__c> cUpdate= new LIST<Operational_Metrics__c>();
List<Operational_Metrics__c> cU= new LIST<Operational_Metrics__c>();
private static boolean run = true;
if(TriggerContextUtility.isFirstRun())
{
run=false;
for (Operational_Metrics__c e : Trigger.new)
{
if(e.u_id__c != null)
opIds.add(e.u_id__c );
}
List<Operational_Metrics__c> opm = [select Unique_Pile_ID__c,Surveyor__c,driver_name__C,u_id__C,Uid_number__c, Composting_Method__c,Pile_Monitoring_Date__c,Average_Temperature__c,Moisture_Test__c,Pile_Monitoring_Action_Taken__c,Other_Action__c,Pile_Input_Register_Date__c,Total_Waste_Input_on_the_Pile__c,Pile_Completion_Stage__c,Date_Pile_COmpleted__c,Curing_Batch_Register_Date__c,No_of_Days__c,No_of_Days_for_piling__c from operational_metrics__c where u_id__c in :opIds];
List<Operational_Metrics__c> uOpm = [select Unique_Pile_ID__c,Surveyor__c,u_id__C,Uid_number__c, Composting_Method__c,Pile_Monitoring_Date__c,Average_Temperature__c,Moisture_Test__c,Pile_Monitoring_Action_Taken__c,Other_Action__c,Pile_Input_Register_Date__c,Total_Waste_Input_on_the_Pile__c,Pile_Completion_Stage__c,Date_Pile_COmpleted__c,Curing_Batch_Register_Date__c,No_of_Days__c,No_of_Days_for_piling__c from operational_metrics__c where u_id__c in :opIds and Pile_Completion_Stage__c='yes'];
List<Operational_Metrics__c> dName=[select Project__c,driver_name__c,Surveyor__c,vehicle_type__c,vehicle_number__c, (SELECT contact.LastName FROM Contacts__r) from operational_metrics__c];
for(Operational_Metrics__c d : dName)
{
if(d.Project__c=='Nalgonda')
{
for(contact cont:d.contacts__r)
{
if(d.Vehicle_Type__c=='Tractor')
{
d.Driver_Name__c=cont.lastname;
// c.add(d);
}
}
}
else if(d.Project__c=='Miryalaguda')
{
System.debug('prj2 '+d);
}
}
for(Operational_Metrics__c ops : opm)
{
if(ops.Surveyor__c=='NLDA Supervisor')
{
ops.Project__c='Nalgonda';
}
else if(ops.Surveyor__c=='Araveli Nagaswara')
{
ops.project__C = 'Miryalaguda';
}
if(ops.Pile_Completion_Stage__c=='yes')
{
d1=ops.Date_Pile_COmpleted__c;
convertedDate=String.valueOf(d1.day())+String.valueOf(d1.month());
tw=ops.Total_Waste_Input_on_the_Pile__c;
cDate=ops.Date_Pile_COmpleted__c;
}
else if(ops.Curing_Batch_Register_Date__c!=null)
{
d2=ops.Curing_Batch_Register_Date__c;
System.debug('records2 are '+ops);
}
else
{
avg=ops.Average_Temperature__c;
mTest=ops.Moisture_Test__c;
actionTaken=ops.Pile_Monitoring_Action_Taken__c;
oAction=ops.Other_Action__c;
pMont=ops.Pile_Monitoring_Date__c;
cRegDate=ops.Curing_Batch_Register_Date__c;
}
if(d1!=null && d2!=null)
{
ops.No_of_Days_for_piling__c=d1.daysBetween(d2);
nDays=ops.No_of_Days_for_piling__c;
c.add(ops);
}
}
for(Operational_Metrics__c u:uOpm)
{
u.Total_Waste_Input_on_the_Pile__c=tw;
u.Average_Temperature__c=avg;
u.Moisture_Test__c=mTest;
u.Pile_Monitoring_Action_Taken__c=actionTaken;
u.Other_Action__c=oAction;
u.Pile_Monitoring_Date__c=pMont;
u.Curing_Batch_Register_Date__c=cRegDate;
u.No_of_Days_for_piling__c=nDays;
u.Curing_Batch_Register_Date__c=d2;
cUpdate.add(u);
}
TriggerContextUtility.setFirstRunFalse();
update c;
update cUpdate;
}
}
But the child record value is not updating into parents record. could anyone help me out
Are u getting any error message?
I have created a sample code for updating records in ther parent object.I hope the below give code will help you .I have created one field on the contact as
well as parent object. If u look at my code.I have undelined some code ,you have to replace and put correct id and then it should be good.
I have not tested it but this will give u good idea
Just an advice,first try to run your code with one field and see if it updates your parent objetct,if it does then try complete code.
Trigger tested1 on operationalmetrics__c (after update) {
map<id,operationalmetrics__c> operationalmetricsId=new map<id,operationalmetrics__c>();
for(operationalmetrics__c opp:trigger.new)
{
operationalmetricsId.put(opp.id,opp);
}
List<operationalmetrics__c> l=[select id,driver_name__c,(select id,driver_name__c from contacts__r) from operationalmetrics__c where id in:operationalmetricsId.keyset()];
list<operationalmetricsId> ok;
for(operationalmetrics__c clap:l)
{
for(contact d:l.contacts__r)
{
operationalmetricsId__c h=operationalmetricsId.get(d.id).driver_name__c;
h.driver_name__c=d.driver_name__c;
ok.add(h);
}
}
update ok;
}