+ Start a Discussion
salesforce@14salesforce@14 

System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, Assigned To ID: owner cannot be blank: [OwnerId

Hi All,

     I am trying to get owner id of parent object. but i am not able to get it.
I did like this .

[select id,ParentObject__r.OwnerId from Childobject__c];

can any one tell me how to resolve it..

Thanks in advance.
 
Best Answer chosen by salesforce@14
Jayant JadhavJayant Jadhav
@Sai,

I guess this method is in class, can you try using without sharing key word for your class.

All Answers

Jayant JadhavJayant Jadhav
Hi Sai,

Can you plz post your code?
salesforce@14salesforce@14
Hi Jayant Jadhav,

My code:


public static string pagesubmit(string recordId){
        
        try{
            Task newTask = new Task();
              if(recordId!=null)
              {
                   Child_Object__c child= [Select Id, Parent_Object__c,Parent_Object__r.OwnerId From Child_Object__c  where Id=:recordId];                               system.debug('@@@@@'+child);
                   newTask.WhatId =child.Parent_Object__c;
                   newTask.TargetMemberId__c = recordId;
                   newTask.OwnerId=Parent_Object__c.OwnerId;                                      
                   insert newTask;
              }
        }
}

Thanks.
surasura

make sure your parent_Object__c is not  a child of a master detail realtionship  . becuase child object of master detail relationship doesnt have a ownerId. in that case you have to get the owner id of your parent object records master record
salesforce@14salesforce@14
Hi sura,

 There is no master detail relationship for Parent_Object__c.

Thanks.
surasura
please check your child record has a Parent_Object__c field populated 
surasura
 if it is also populated check whether user who is running the code has  access to parent record of your child 
Le NguyenLe Nguyen
Hi Sai,​
Pls change this:
newTask.OwnerId=Parent_Object__c.OwnerId;                                     

to

​newTask.OwnerId = child.Parent_Object__r.OwnerId ;

Le
Jayant JadhavJayant Jadhav
Sai,

Plz try below code. We will receive this kind of exception if we are trying to assign null value to the lookup or master field.
 
public static string pagesubmit(string recordId){
        
     
            Task newTask = new Task();
              if(recordId!=null)
              {
                   Child_Object__c child= [Select Id, Parent_Object__c,Parent_Object__r.OwnerId From Child_Object__c  where Id=:recordId];                             
                   system.debug('@@@@@'+child);
                   newTask.WhatId =child.Parent_Object__c;

                   //newTask.TargetMemberId__c = recordId;

                  if(child.Parent_Object__r.OwnerId!=null)
                   newTask.OwnerId=child.Parent_Object__r.OwnerId;                                      
                   insert newTask;
              }
        
}

 
salesforce@14salesforce@14
Hi All,

          Thanks for your response.

First of all i am not querying the ownerid of parent object.It showing null and i dont know is there any other way.

Please help me.
Jayant JadhavJayant Jadhav
@Sai, 

Can  you plz let us know your use case for this issue and also let me know after using above code what kind of exception you are receiving?
salesforce@14salesforce@14
Hi Jayant Jadhav, 

I am getting exception as 
System.DmlException: Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: []


Thanks.
Jayant JadhavJayant Jadhav
@Sai,

I guess this method is in class, can you try using without sharing key word for your class.
This was selected as the best answer
salesforce@14salesforce@14
Hi Jayant Jadhav,

                    I solved my issue. Thank you very much.