2
votes

I am having following requirement:

1) To get list of all the users for whome 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: 1)Owner 2)Name
3)Record ID
4)Folder ID
5)Created By
6)Last Modified By

any help/suggestions??

1
Hi @Rudra, there is a new stackexchange site specific to Salesforce at salesforce.stackexchange.com. Come join the community over there! :) - Ralph Callaway

1 Answers

0
votes

I'm not sure what field on the FRUP references the user Id it relates to, but you can loop through the FRUP object with something like this:

List<FRUP> frupToUpdate = new List<FRUP>();
for (FRUP f : [
    Select
        OwnerId,
        Name, //etc.
        UserId //the field that references the user Id
    from FRUP
    where userId = : usr]) {
//update field on FRUP, e.g. f.Name = 'New Name';
frupToUpdate.add(f);
}

This selects the FRUP records that relate to the users with changed profiles. It then updates a field on the record and adds the record to a list for updating. Note that this should be after your loop for(User u: Trigger.new). Then update the FRUP records that have changed: update frupToUpdate;