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
RIteshMRIteshM 

System.DmlException: Update failed. first error: INVALID_CROSS_REFERENCE_KEY,

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

Ashish_SFDCAshish_SFDC

Hi Ritesh, 

 

This error basically means there is some problem in the Permission to Create or Edit  the Object records in the code. 

Check the settings in the profile and the objects in question and see if that helps. 

 

Regards,

Ashish