You need to sign in to do that
Don't have an account?
Robert 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
Screen Shot of Excution Log
For your first check, skip the assignment before the compare, so have :
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:
Regards
Andrew