+ Start a Discussion
vish hvish h 

How to update lookup field in the parent object.

Hi ,

I'm attempting to update the lookup field in the object and unable to do so.. Please anyone guide me the correct way of updating the field in apex.

PO_requisition__c poreq = new PO_requisition__c();
            poreq = [select SecondApprover__r.FirstName from PO_requisition__c where ID ='a0J9000000tinJl'];
            poreq.SecondApprover__r.Id = UserInfo.getUserId();
            update poreq;
            System.debug('----------------------This is approved now-----'+poreq.SecondApprover__r.Id);


getting below error..

System.NullPointerException: Attempt to de-reference a null object

Thanks
VIshwa
Best Answer chosen by vish h
Vishal_GuptaVishal_Gupta
Hi,

Please give a try with below code :

PO_requisition__c poreq = new PO_requisition__c();
            poreq = [select SecondApprover__r.FirstName from PO_requisition__c where ID ='a0J9000000tinJl'];
            poreq.SecondApprover__c = UserInfo.getUserId();
            update poreq;
            System.debug('----------------------This is approved now-----'+poreq.SecondApprover__c);

Thanks,
Vishal

All Answers

Vishal_GuptaVishal_Gupta
Hi,

Please give a try with below code :

PO_requisition__c poreq = new PO_requisition__c();
            poreq = [select SecondApprover__r.FirstName from PO_requisition__c where ID ='a0J9000000tinJl'];
            poreq.SecondApprover__c = UserInfo.getUserId();
            update poreq;
            System.debug('----------------------This is approved now-----'+poreq.SecondApprover__c);

Thanks,
Vishal
This was selected as the best answer
vish hvish h
Thank you !! It worked...