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
Jesus GJesus 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:
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
Best Answer chosen by Jesus G

All Answers

madhan bondalapatimadhan bondalapati
In the trigger part for the Update replace 
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 
Jesus GJesus G
Hello Madhan,

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
Bhaswanthnaga vivek vutukuriBhaswanthnaga vivek vutukuri
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(), Trigger.oldMap);
    }
}
 
public class AllUserTriggersHandler {
    @future
    public static void handleAfterUpdate(Set<Id> UserIds, Map<Id, User> usersOld) { 
        List <Contact> contactsToUpdate = new List <Contact>();
        List<User> userList = [SELECT Id, ContactId, Profile.Name, Username, IsActive FROM User WHERE Id IN :UserIds];

        //to compare with old values

        for (User currentUser : userList) {

        	User oldUser = usersOld.get(currentUser.Id);

        	if(currentUser.Username == oldUser.Username)
        	{
        		//do something;
        	}
        }
        update contactsToUpdate;
    }
}

don't know why you are getting error try this code once
Jesus GJesus G
Hello Bhaswanthnaga,

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
Bhaswanthnaga vivek vutukuriBhaswanthnaga vivek vutukuri
Bro, why do you need future annotation in this case ? just remove that if there is not particular reason for using that
madhan bondalapatimadhan bondalapati
Just remove future method..because it has to fire instantly,it should not wait for future resources
madhan bondalapatimadhan bondalapati
I had a little doubt whether we can use Trigger.oldmap for after update...
Jesus GJesus G
Please, notice that the future annotation is needed since I am performing a DML operation on a setup object (user) and non-setup object (contact) in the same context... I will get a 'MIXED_DML_OPERATION' error if I do not use it.
Jesus GJesus G
This was selected as the best answer