You need to sign in to do that
Don't have an account?
Ab
Update Lookup field from Apex code
Hello,
I am inserting a record to a custom object like below
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 ?
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 ?
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. Let me know, if it helps you. Don't forget to mark best answer.
Thanks,
Sumit Kumar Singh
All Answers
rtList.Parent_Account__c = 'a008E000001Vzxq';
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. Let me know, if it helps you. Don't forget to mark best answer.
Thanks,
Sumit Kumar Singh