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
AbAb 

Update Lookup field from Apex code

Hello,

I am inserting a record to a custom object like below
 
List<Account> accountList = [SELECT  Id, Name, Column1__c, Column2__c, Column3__c, ParentId
                             FROM Account 
                             WHERE Id = '001b000000r6WE3' LIMIT 1];
CustomObject1__c rtList = new CustomObject1__c();

for(Account a : accountList){
	rtList.Name = a.Name;
	rtList.Column1__c = a.Column1__c;
	rtList.Column2__c = a.Column2__c;
	rtList.Column3__c = a.Column3__c;   
    //rtList.id = a.ParentId;
    rtList.Parent_Account__r.Id  = 'a008E000001Vzxq';  //LOOKUP
}

System.debug('AccountList  # '+accountList);
System.debug('rtList # '+rtList);

Update rtList;

The lookup field always gives me error when uploading.
Lookup field name is "Parent account" I tried using  "rtList.Parent_Account__r.Id" and also "Parent_Account__r.Parent_Account__c " but both of them gives error,

What can be solution, if want also want to update the lookup field through Apex code ?
Best Answer chosen by Ab
Sumit Kumar Singh 9Sumit Kumar Singh 9
I can't see any "insert" statement.
You are trying to "update" a record which doesn't exists.
If you are trying to insert only 1 custom Object record, then for loop is not needed.
List<Account> accountList = [SELECT  Id, Name, Column1__c, Column2__c, Column3__c, ParentId
                             FROM Account 
                             WHERE Id = '001b000000r6WE3' LIMIT 1];


if(accountList  !=  null && accountList .size() > 0){
    CustomObject1__c cObjRec = new CustomObject1__c();
	cObjRec.Name = accountList[0].Name;
	cObjRec.Column1__c = accountList[0].Column1__c;
	cObjRec.Column2__c = accountList[0].Column2__c;
	cObjRec.Column3__c = accountList[0].Column3__c;  
        
        //This will not work
        //rtList.Parent_Account__r.Id  = 'a008E000001Vzxq';  //LOOKUP
       
       // try this one instead
       cObjRec.Parent_Account__c = 'a008E000001Vzxq';
        
       // Insert the custom Object Record
       insert cObjRec;
}
Let me know, if it helps you. Don't forget to mark best answer.


Thanks,
Sumit Kumar Singh



 

All Answers

povery.sf@gmail.compovery.sf@gmail.com
Did you try?

rtList.Parent_Account__c = 'a008E000001Vzxq';
Sumit Kumar Singh 9Sumit Kumar Singh 9
I can't see any "insert" statement.
You are trying to "update" a record which doesn't exists.
If you are trying to insert only 1 custom Object record, then for loop is not needed.
List<Account> accountList = [SELECT  Id, Name, Column1__c, Column2__c, Column3__c, ParentId
                             FROM Account 
                             WHERE Id = '001b000000r6WE3' LIMIT 1];


if(accountList  !=  null && accountList .size() > 0){
    CustomObject1__c cObjRec = new CustomObject1__c();
	cObjRec.Name = accountList[0].Name;
	cObjRec.Column1__c = accountList[0].Column1__c;
	cObjRec.Column2__c = accountList[0].Column2__c;
	cObjRec.Column3__c = accountList[0].Column3__c;  
        
        //This will not work
        //rtList.Parent_Account__r.Id  = 'a008E000001Vzxq';  //LOOKUP
       
       // try this one instead
       cObjRec.Parent_Account__c = 'a008E000001Vzxq';
        
       // Insert the custom Object Record
       insert cObjRec;
}
Let me know, if it helps you. Don't forget to mark best answer.


Thanks,
Sumit Kumar Singh



 
This was selected as the best answer