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
Manish S 8Manish S 8 

Apex coding to combine 2 SOQL queries in a map collection.

Hai i m a beginner in apex. I want to add 2 SOQL queries onto a map. 1st query returns a list of profile names:[SELECT name FROM Profile]   2nd query returns a list of fieldpermissions i.e fieldname,permissionedit,permissionread&sobjectType:[SELECT field,PermissionsEdit,PermissionsRead,sObjectType FROM FieldPermissions where parentid in(select id from permissionset where profileid in profile)
I want to retrieve all the fieldpermissions list with respect to each profile name. Can anyone help me with the code ?
 
logontokartiklogontokartik
Basically you want to build a Map of Profile to FieldPermissions?

Something like below?
Map<Id,List<FieldPermissions> profileFieldPermissionMap = new Map<Id,List<FieldPermissions>();

Map<Id,Id> permissionSetProfileMap = new Map<Id,Id>();

for (PermissionSet pm : [Select Id, ProfileId from PermissionSet]) {
    permissionSetProfileMap.put(pm.Id,pm.ProfileId); // Build map between permissionSet ID and profile Id.
}

for (FieldPermissons fp : [SELECT field,PermissionsEdit,PermissionsRead,sObjectType,parentid FROM FieldPermissions]) {
   List<FieldPermissions> fPermissionsPerProfile;
   // Check to see if the profile already has list of fieldpermissions 
   if (profileFieldPermissionMap.get(permissionSetProfileMap.get(fp.parentId)) != null) {   
        // If yes, retrieve it 
        fPermissionsPerProfile = profileFieldPermissionMap.get(permissionSetProfileMap.get(fp.parentId));
   } else {
      // else create a new list
      fPermissionsPerProfile = new List<FieldPermissions>();
   }
    // add the fieldPermission to list
    fPermissionsPerProfile.add(fp);
    // add list to the map.
    profileFieldPermissionMap.put(permissionSetProfileMap.get(fp.parentId),fPermissionsPerProfile);
}

Hope this helps.

PS: i wrote it in the forum , so if any typos please excuse