You need to sign in to do that
Don't have an account?
Jesus G
Compare old / new values of 2 lists
Hello,
I am writing a class for the User object where I have 2 lists that collect the new and old values of the trigger. Would it be possible to compare the 2 values of the same record and perform an action only if it has changed? Please, notice I am not using Maps since the future annotation is needed:
Thank you very much,
J
I am writing a class for the User object where I have 2 lists that collect the new and old values of the trigger. Would it be possible to compare the 2 values of the same record and perform an action only if it has changed? Please, notice I am not using Maps since the future annotation is needed:
@future public static void handleAfterUpdate(Set<Id> newUserIds, Set<Id> oldUserIds) { List <Contact> contactsToUpdate = new List <Contact>(); List<User> newUsersList = [SELECT Id, ContactId, Profile.Name, Username, IsActive FROM User WHERE Id IN :newUserIds]; List<User> oldUsersList = [SELECT Id, ContactId, Profile.Name, Username, IsActive FROM User WHERE Id IN :oldUserIds]; for (User currentUser : /* list of the records where the previous fields have changed */) { 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,
J
Trigger:
Class:
All Answers
I wanted to have everything in the class instead of part of the code in the trigger and part in the class, but I guess this is a good workaround in this scenario. I have tested it and it looks correct :)
The only thing I am having some issues with is to uncheck the checkbox after the records are updated so they are not included the next time the trigger is executed. Should I have that code in the trigger or the future class? (I have tried in both without success)
Many thanks,
J
J
List <User> UsersList = [SELECT Id, ContactId, Profile.Name, Username, IsActive, Changed__cFROM User WHERE Changed__c = true AND Id IN :UserIds ];
above query will query only records that needs to be updated
Trigger:
Class: