title: System.DmlException: Update failed. first error: INVALID_CROSS_REFERENCE_KEY,
body: <p>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</p>
  
  <p>my code segment is </p>
  
  <pre class="lang-apex prettyprint-override"><code> PermissionSet set1 = [SELECT Id From PermissionSet 
                           WHERE profileId = : SourceProfileId LIMIT 1] ;
   PermissionSet set2 = [SELECT Id FROM PermissionSet 
                           WHERE profileId = : TargetProfileId LIMIT 1];
   List&lt;ObjectPermissions&gt; oo = [SELECT Id,
                                        SObjectType,
                                        ParentId,
                                        PermissionsCreate,
                                        PermissionsDelete,
                                        PermissionsEdit,
                                        PermissionsModifyAllRecords,
                                        PermissionsRead,
                                        PermissionsViewAllRecords 
                                   FROM ObjectPermissions 
                                       WHERE ParentId = : set1.id];
  
  
   List&lt;ObjectPermissions&gt; oo1 = [SELECT ParentId,
                                        Id,
                                        SObjectType,
                                        PermissionsCreate,
                                        PermissionsDelete,
                                        PermissionsEdit,
                                        PermissionsModifyAllRecords,
                                        PermissionsRead,
                                        PermissionsViewAllRecords 
                                   FROM ObjectPermissions 
                                       WHERE ParentId = : set2.Id];
   Map&lt;String , ObjectPermissions&gt; source_obj = new Map&lt;String, ObjectPermissions&gt;();
   Map&lt;String , ObjectPermissions&gt; target_obj = new Map&lt;String, ObjectPermissions&gt;();
   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;
       }
   }
  </code></pre>
  
  <p>when the control come to this line </p>
  
  <p>update target;
  it is giving error </p>
  
  <p>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: []</p>
  
  <p>i am unable to figure it out why i am facing this error please some one help to resolve this error</p>