0

Trigger:

if (Trigger.isUpdate) { 
    String jsonOldMap = JSON.serialize(Trigger.oldMap);
    String jsonNewMap = JSON.serialize(Trigger.newMap);

    AssignPermissionSetToUserHelper.futureCreateRecord(jsonOldMap,jsonNewMap); 
} 

This My Helper Class Method:

// FUTURE METHOD INSERT RECORD IN 'Permission-set-Assignment-History' OBJECT=== AFTER UPDATE
@future
public static void futureCreateRecord(String stroldMap, String strnewMap) {

    map<id, User> oldMap = (Map<id, User>) JSON.deserialize(stroldMap, map<id, User>.class);
    map<id, User> newMap = (Map<id, User>) JSON.deserialize(strnewMap, map<id, User>.class);

    List<Permission_set_Assignment_History__c> insertPsah = new List<Permission_set_Assignment_History__c> ();


    for(User obj : newMap.values()) {
        System.debug('OBj...'+obj);
        User oldObj = oldMap.get(obj.Id);               
        Permission_set_Assignment_History__c p = new Permission_set_Assignment_History__c ();
        p.Name ='User Permission ' ;//Show AssignePermission Name In History Object
        p.OldRole__c = oldObj.UserRoleid;// how to show role Name 
        P.NewRole__c  =obj.UserRoleid;
        p.UserImpacted__c = obj.Id;// how to show role Name 
        p.Type__c = 'Assigned';
        System.debug('p...'+p);
        insertPsah.add(p);
         System.debug('p..After.'+p);
    }


    if (insertPsah.size() > 0) { 
        Database.insert(insertPsah,false);
    }

}

enter image description here

  • Hi - if you could please add more of a description of what you're trying to accomplish, it would be easier to help you out – Brian Miller Oct 3 '19 at 9:25
  • let take a exmple . after update Trigger i want show Old Values And New Values In Custom Object. Like User OldName And User New Name ,UserRole Name And Assigned Permission Name – Teja Mani Oct 3 '19 at 9:29
  • Iam Calling this Method in @future – Teja Mani Oct 3 '19 at 9:30
0

You essentially would add queries to get the information. You have all the roles in old and new map. You can loop through them to add them into a list to use the following query to build a map that you can reference to get the name.

Map<Id,UserRole> mapRoles = [SELECT Id FROM UserRole WHERE Id in :listOfRoles];
//later on
p.OldRole__c = mapRoles.get(oldObj.UserRoleid).Name; 
P.NewRole__c  =mapRoles.get(obj.UserRoleid).Name;

For permission sets, you'd have another query to get that information based on putting all the user record Ids into a list from the trigger maps. The big question is will there be two permission set assignments on the same user at the same time?

List<PermissionSetAssignmetn> permNames = [SELECT Id, PermissionSet.Name, ASsigneeId FROM PermissionSetAssignment WHERE AssigneeId in :listOfUsers AND SystemModStamp = lastHour //use some sort of time variable, but remember this is future so it's not guaranteed to happen immediately after. could do this outside the query

Map<Id,List<String>> userPermissionMap = new Map<Id,List<String>>();
for(PermissionSetAssignment psa : permNames){
    for(Id userId :listOfUser){
        if(psa.AssigneeId == userId){
            if(!userPermissionName.containsKey(userId){
                List<String> listNames = new List<String>();
                listNames.add(psa.PermissionSet.Name);
                userPermissionMap.put(userId, listNames);
            } else {
                List<String> existingNames = userPermissionMap.get(userId);
                existingNames.add(psa.PermissionSet.Name);
                userPermissionMap.put(userId, existingNames);
            }
        }          
    } 
}

//appends all the permission set names in one
p.Name = String.join(userPermissionMap.get(obj.Id));


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.