function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SFDC-DMGSFDC-DMG 

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;
     }
    }

}
}
}

Jake GmerekJake Gmerek

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.

SFDC-DMGSFDC-DMG

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

 

Jake GmerekJake Gmerek
Next I would use the debug logs:

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.