You need to sign in to do that
Don't have an account?
Jesus G
Compare old values in a User class
Hello!
Please, could you help me to edit the following class so I can use it to compare old and new values when using it in an After Update trigger? It is working as expected for an After Insert trigger, but I would need it to compare old and new values so only the records whose fields changes are updated. I understand I need to use Map instead of Set but I cannot see how for the User object.
Trigger:
Class:
Jesus
Please, could you help me to edit the following class so I can use it to compare old and new values when using it in an After Update trigger? It is working as expected for an After Insert trigger, but I would need it to compare old and new values so only the records whose fields changes are updated. I understand I need to use Map instead of Set but I cannot see how for the User object.
Trigger:
trigger AllUserTriggers on User (after insert, after update) { if(Trigger.isAfter && Trigger.isInsert) { AllUserTriggersHandler.handleAfterInsert(Trigger.newMap.keySet()); } if(Trigger.isAfter && Trigger.isUpdate) { AllUserTriggersHandler.handleAfterUpdate(Trigger.newMap.keySet()); } }
Class:
public class AllUserTriggersHandler { @future public static void handleAfterUpdate(Set<Id> UserIds) { List <Contact> contactsToUpdate = new List <Contact>(); List<User> userList = [SELECT Id, ContactId, Profile.Name, Username, IsActive FROM User WHERE Id IN :UserIds]; for (User currentUser : userList) { if (currentUser.ContactId != null){ Contact con = new Contact(); con.Id = currentUser.ContactId; con.User_profile__c = currentUser.Profile.Name; con.User_username__c = currentUser.Username; con.Active_User__c = currentUser.IsActive; contactsToUpdate.add(con); } } update contactsToUpdate; } }Thank you very much!
Jesus
All Answers
AllUserTriggersHandler.handleAfterUpdate(Trigger.newMap.keySet(),trigger.oldmap);
In the class replace the method with this
AllUserTriggersHandler.handleAfterUpdate(Trigger.newMap.keySet(),Map<id,user> u);
so you can acess the old values from the Map with instance u
Many thanks for your reply but I am afraid I already tried something similar and it does not work.
The errors are that the Map is an unsupported parameter type and, consequently, the method of the trigger is incorrect.
Any other ideas?
Thank you,
Jesus
don't know why you are getting error try this code once
Thank you for your reply but that is the code I have already tried. Please, find below the errors I get:
- AllUserTriggersHandler: Unsupported parameter type Map<Id,User>
- AllUserTriggers: Method does not exist or incorrect signature: AllUserTriggersHandler.handleAfterUpdate(Set<Id>, Map<Id,User>)
I understand that the issue is related to the fact that "Methods with the future annotation cannot take sObjects or objects as arguments":
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_invoking_future_methods.htm
Thank you!
J