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
Robert Wambold 10Robert Wambold 10 

Another Trigger Question...How to get Id from Name?

Hello All,

Pretty simple question. My table amc_Project__c contains field Project_Manager__c (PM), If the User changes PM I want check if the NEW PM is different from the OLD PM. My problem is PM is Lookup field and when I compare in my code it is a Name vs  an Id. How do I make both either Names or Ids.

Thank you all.

Robert

 

My Code:

trigger Update_MC_Milestone_Action_Owners on amc__Project__c (after update) {

    Set<Id> ProjectId = new Set<Id>();
    String NEW_Project_Manager;
    String OLD_Project_Manager;
    
    for(amc__Project__c prj : Trigger.new)
    {
         if(prj.amc__Status__c !='All Products Offline' && prj.amc__Status__c !='Archived')
            System.debug('*** Project changed ***');
            NEW_Project_Manager=prj.Project_Manager__c;

            if(NEW_Project_Manager != trigger.oldMap.get(prj.Id).Project_Manager__c) {               
               System.debug('*** PM has changed ***');
               ProjectId.add(prj.Id);
               OLD_Project_Manager=trigger.oldMap.get(prj.Id).Project_Manager__c;
            }
    }

     List<amc__Milestone__c> mileToUpdate = new List<amc__Milestone__c>();

     for(amc__Milestone__c mil : [select id, amc__Milestone_Owner__c from amc__Milestone__c where amc__Project__c in: ProjectId])
     {
         if(System.Label.MC_Role_PM == mil.amc__Milestone_Owner__c || mil.amc__Milestone_Owner__c != OLD_Project_Manager){ 
             mil.amc__Milestone_Owner__c=NEW_Project_Manager;          
             mileToUpdate.add(mil);
         }

     }

     update mileToUpdate;
}


Screen Shot of Error

User-added image

Screen Shot of Excution Log

User-added image

 

 

Andrew GAndrew G
A couple of thoughts.

For your first check, skip the assignment before the compare, so have :
 
    for(amc__Project__c prj : Trigger.new)
    {
         if(prj.amc__Status__c !='All Products Offline' && prj.amc__Status__c !='Archived') 
         {
            System.debug('*** Project changed ***');
            if(prj.Project_Manager__c != trigger.oldMap.get(prj.Id).Project_Manager__c) 
            {           
               System.debug('*** PM has changed ***');
               ProjectId.add(prj.Id);
               OLD_Project_Manager=trigger.oldMap.get(prj.Id).Project_Manager__c;
            }
        }
    }
to my eye it makes it cleaner to read that you are check the new field value to the old field value. note that I also added the brackets for the project changed IF statement, just to be consistent in layout.
And to be honest, i would be surprised if the error was there any way.

And then I would check what sort of field is: amc__Milestone_Owner__c - i would suspect the issue is there 

I would run some debugs as follows to see what is recorded in each field , so then we can get to the base of the issue:
 
trigger Update_MC_Milestone_Action_Owners on amc__Project__c (after update) {

    Set<Id> ProjectId = new Set<Id>();
    String NEW_Project_Manager;
    String OLD_Project_Manager;
    
    for(amc__Project__c prj : Trigger.new)
    {
         if(prj.amc__Status__c !='All Products Offline' && prj.amc__Status__c !='Archived')  
         {
            System.debug('*** Project changed ***');
            if(prj.Project_Manager__c != trigger.oldMap.get(prj.Id).Project_Manager__c) 
            {           
               System.debug('*** PM has changed ***');
               ProjectId.add(prj.Id);
               OLD_Project_Manager=trigger.oldMap.get(prj.Id).Project_Manager__c;
               System.debug('*** OLD_Project_Manager ***' + OLD_Project_Manager);
            }
        }
    }

    List<amc__Milestone__c> mileToUpdate = new List<amc__Milestone__c>();

    for(amc__Milestone__c mil : [select id, amc__Milestone_Owner__c from amc__Milestone__c where amc__Project__c in: ProjectId])
    {
        System.debug('*** in amc__Milestone_Owner__c loop ***'); 
        System.debug('*** System.Label.MC_Role_PM ***' + System.Label.MC_Role_PM );
        System.debug('*** amc__Milestone_Owner__c ***' + mil.amc__Milestone_Owner__c );
        System.debug('*** OLD_Project_Manager***' + OLD_Project_Manager); 
        
        if(System.Label.MC_Role_PM == mil.amc__Milestone_Owner__c || mil.amc__Milestone_Owner__c != OLD_Project_Manager){ 
             mil.amc__Milestone_Owner__c=NEW_Project_Manager;          
             mileToUpdate.add(mil);
         }
     }
     update mileToUpdate;
}

Regards

Andrew