You need to sign in to do that
Don't have an account?
Trigger not updating Opportunity field - please help
Hello,
I wrote the below trigger. It updates a Lookup field (for a user) on the Opportunity from another related lookup field (user) on a custom child object.
I am not getting errors, but the field on the opportunity is not being updated.
Can anyone take a look at the code below and let me know what might be happening with it or what I can fix to have the field from the custom child object update the field on the opportunity upon edit (of the child object)
Thanks!
trigger Update_NHC_SE_Assignment on Network_Health_Check__c (before insert, after update) {
// Update the SE NHC Assignment on Opp based on the SE who delivered the Demo
// Declare the variables
public set<Id> OppIDs = new Set<Id>();
public list<Opportunity> OppsToUpdate = new List<Opportunity>();
// Build the list of Opps to update
if(Trigger.isInsert || Trigger.isUnDelete || Trigger.isUpdate){
for(Network_Health_Check__c n: Trigger.new){
if(n.Assigned_SE__r.id != null && n.Name != null){
Opportunity Opp = new Opportunity(Id=n.Related_Opportunity__r.Id, SE_NHC_Assignment__c = n.Assigned_SE__r.Id);update Opp;
}
}
if(Trigger.isUpdate){
for(Network_Health_Check__c n: Trigger.old){
if(n.Assigned_SE__r.id != null && n.Name != null){
Opportunity Opp = new Opportunity(Id=n.Related_Opportunity__r.Id, SE_NHC_Assignment__c=n.Assigned_SE__r.Id); update Opp;
}
}
}
}
}
So I am pretty sure that if you use the id= shortcut to save a query then you can not set other fields in the constructor. Essentially you are using a get method not a set method. I would try replacing this:
Opportunity Opp = new Opportunity(Id=n.Related_Opportunity__r.Id, SE_NHC_Assignment__c = n.Assigned_SE__r.Id);update Opp;
with this:
Opportunity Opp = new Opportunity(Id=n.Related_Opportunity__r.Id);
Opp.SE_NHC_Assignment__c = n.Assigned_SE__r.Id;
update Opp;
See if that works and let me know how it goes.
Hi,
Thank you for your reponse. Unfortunately, this is still not working.
I added an additional field as well that updates something else on the opp to see whether that was working and to try and narrow down where the issue was - however, that f additional field did not update either, which told me that the trigger wasn't working (versus that I thought it would have been an issue with the SE_Demo_Assignment__ field).
Any other ideas about how to get this working?
Thanks,
Danielle
First Insert this:
Opportunity Opp = new Opportunity(Id=n.Related_Opportunity__r.Id);
system.debug(json.serializepretty(Opp));
Opp.SE_NHC_Assignment__c = n.Assigned_SE__r.Id;
update Opp;
Then check the log and see if you are even getting to that point.