I am having following requirement:
To get list of all the users for whom profile has been changed.
Then query on FRUP (It is a custom object) to retrieve all the records which are associated with the user whose profile is changed. (FRUP object will contain the list of all the records created by all the users on all the objects say Account, Opportunity)
Update FRUP.
For achieving this I wrote one trigger through which i am able to fetch list of all the users whose profile has changed which is as follows:
Trigger UserProfileTrigger on User (before update) {
List<User> usr = new List<User>();
Map<String,String> userMap = new Map<String,String>();
for(User u: Trigger.new){
//Create an old and new map so that we can compare values
User oldOpp = Trigger.oldMap.get(u.ID);
User newOpp = Trigger.newMap.get(u.ID);
//Retrieve the old and new profile
string oldProfileId = oldOpp.profileId;
string newProfileId = newOpp.profileId;
//If the fields are different, the profile has changed
if(oldProfileId != newProfileId){
System.debug('Old:'+oldProfileId);
System.debug('New :'+newProfileId);
usr.add(u);
System.debug('User :'+usr);
}
}
}
Also Following are the fields on custom object FRUP:
- Owner
- Name
- Record ID
- Folder ID
- Created By
- Last Modified By
UPDATE: I tried the following code:
Trigger UserProfileTrigger on User (before update) {
set<Id> userIds = new set<Id>();
for(User u: Trigger.new){
//If the fields are different, the profile has changed
if(u.ProfileId != Trigger.oldMap.get(u.ID).ProfileId){
userIds.add(u.Id);
}
From the above code I am getting 18 digit user id while FRUP stores 15 digit user id, so what I did I applied string function and able to fetch 15 digit id code is as follows:
set<Id> userIds = new set<Id>();
for(User u: Trigger.new){
//If the fields are different, the profile has changed
if(u.ProfileId != Trigger.oldMap.get(u.ID).ProfileId){
string u1 = u.id;
string u2 = u1.left(15);
system.debug('user id------------------:'+u2);
//userIds.add(u.Id);
userIds.add(u2);
}
system.debug('$$$$$$$$$$$$$$$$$$$$$'+userIds);
But in debug log when I am printing u2
I am getting 15 digit id but userIds
variable is printing 18 digit id.