1

I am having following requirement:

  1. To get list of all the users for whom profile has been changed.

  2. 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)

  3. 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.

1

Without knowing exactly what you want to do with the FRUP object something like this should work. Obviously I did this in a text editor so you wuill likely need to change custom field names to fit your object.

Also simplified your code a bit as you can just compare trigger.new profileId and trigger.old profileId directly in only one line.

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);
        }

    list<FRUP__c> frups = [Select Id, Name, RecordID__c, FolderID__c, CreatedById, LastModifiedById 
                          From FRUP__c 
                          Where Owner Id IN: userIds];

    for(FRUP__c f : frups){
        //Perform your logic and changes to the FRUP object inside this loop
    }

    update frups;
}
  • Hi SF_Ninja....Thanks for the response. But i am having one doubt which i am posting in your answer section due to limited space here. – SFDC Geek Nov 26 '13 at 10:24
  • If you just take the string returned from your FRUP object, and cast it as an Id, it gives you the 18 digits automatically. Id myID = yourFRUPstring; – Chris Duncombe Nov 26 '13 at 11:28
  • Also, if you are storing configuration mapping, like recordIds, you might want to look into using Custom Settings. They would be much more appropriate and easier to use. I dont really know your particular use case, but it sounds as though cusdtom settings might be a better options – Chris Duncombe Nov 26 '13 at 11:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.