2

i am trying to update a ObjectPermissions Object in slaesforce such that a profile will get the permissions to access a object similar to other profile. i write a code

my code segment is

 PermissionSet set1 = [SELECT Id From PermissionSet 
                         WHERE profileId = : SourceProfileId LIMIT 1] ;
 PermissionSet set2 = [SELECT Id FROM PermissionSet 
                         WHERE profileId = : TargetProfileId LIMIT 1];
 List<ObjectPermissions> oo = [SELECT Id,
                                      SObjectType,
                                      ParentId,
                                      PermissionsCreate,
                                      PermissionsDelete,
                                      PermissionsEdit,
                                      PermissionsModifyAllRecords,
                                      PermissionsRead,
                                      PermissionsViewAllRecords 
                                 FROM ObjectPermissions 
                                     WHERE ParentId = : set1.id];


 List<ObjectPermissions> oo1 = [SELECT ParentId,
                                      Id,
                                      SObjectType,
                                      PermissionsCreate,
                                      PermissionsDelete,
                                      PermissionsEdit,
                                      PermissionsModifyAllRecords,
                                      PermissionsRead,
                                      PermissionsViewAllRecords 
                                 FROM ObjectPermissions 
                                     WHERE ParentId = : set2.Id];
 Map<String , ObjectPermissions> source_obj = new Map<String, ObjectPermissions>();
 Map<String , ObjectPermissions> target_obj = new Map<String, ObjectPermissions>();
 for (ObjectPermissions o : oo) {
     source_obj.put(o.SObjectType, o);
 }
 for (ObjectPermissions o : oo1) {
     target_obj.put(o.SObjectType, o);
 }
 ObjectPermissions target, source;
 for (String s : source_obj.keySet() ) {
     if (target_obj.containsKey(s)) {
         target = target_obj.get(s);
         source = source_obj.get(s);
         System.debug('Source is:' + source);
         System.debug('Target is : ' + target);
         target.PermissionsCreate = source.PermissionsCreate;
         target.PermissionsDelete = source.PermissionsDelete;
         target.PermissionsEdit = source.PermissionsEdit;
         target.PermissionsModifyAllRecords = source.PermissionsModifyAllRecords;
         target.PermissionsRead = source.PermissionsRead;
         target.PermissionsViewAllRecords = source.PermissionsViewAllRecords;
         update target;
     } else {
         target = new ObjectPermissions(SObjectType = s );
         source = source_obj.get(s);
         target.PermissionsCreate = source.PermissionsCreate;
         target.PermissionsDelete = source.PermissionsDelete;
         target.PermissionsEdit = source.PermissionsEdit;
         target.PermissionsModifyAllRecords = source.PermissionsModifyAllRecords;
         target.PermissionsRead = source.PermissionsRead;
         target.PermissionsViewAllRecords = source.PermissionsViewAllRecords;
         insert target;
     }
 }

when the control come to this line

update target; it is giving error

Update failed. First exception on row 0 with id 110i0000007KNEvAAO; first error: INVALID_CROSS_REFERENCE_KEY, You can't create, edit, or delete records for this permission set parent because it's associated with a profile. Parent ID: 0PSi00000009BE9: []

i am unable to figure it out why i am facing this error please some one help to resolve this error

Pavel Slepiankou
  • 3,516
  • 2
  • 25
  • 30
mathlearner
  • 7,509
  • 31
  • 126
  • 189

1 Answers1

2

the persmission set you are trying to update is already associated with some of your profile with id=0PSi00000009BE9 go to

https://ap1.salesforce.com/0PSi00000009BE9 and check there.

Sachin Kadian
  • 201
  • 3
  • 12