0

I am a new APEX learner. I created two custom objects : Book & Author, where Author being Parent and Book being child object. I created Child : book record 'Harry Potter' with Parent : 'J K Rowling'.Book Record With Author

Now I Wanted to Change the author to myself so I performed the following operation:

Author__c aut=[SELECT Id FROM Author__c WHERE Name LIKE 'Ravi%'];
system.debug('Author '+aut.Id);
Book__c bc=[SELECT Id, Name, Author__r.Id, Author__r.Name FROM Book__c WHERE Name LIKE 'harry     potter%'];
system.debug(bc.Name+', Author: '+bc.Author__r.Name+', author Id: '+bc.Author__r.Id);
bc.Author__r.Id=aut.Id;
system.debug(bc.Author__r.Id);
update bc;
bc=[SELECT Id, Name, Author__r.Id, Author__r.Name FROM Book__c WHERE Name LIKE 'harry potter%'];
system.debug(bc.Name+', Author: '+bc.Author__r.Name+', author Id: '+bc.Author__r.Id);

This Executed successfully, but author wasn't changed :

enter image description here

1 Answer 1

2

Try this instead

bc.Author__c =aut.Id;
2
  • 1
    This is telling it to change the actual Id which is not possible. bc.Author__r.Id=aut.Id;
    – Chris
    Commented Apr 14, 2022 at 17:56
  • Not Working. A non foreign key field cannot be referenced in a path expression: Author__c Commented Apr 15, 2022 at 5:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .