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
Abhishek Singh 88Abhishek Singh 88 

need help to populate lookup field using apex class

Hi developers,
Here i have written a class, which is inserting an incident and after insertion i am trying put id of that incident in another child object
BMCServiceDesk__Incident__c newincident= new BMCServiceDesk__Incident__c();
newincident.Name='Wellspan';
newincident.description__c='Test cases.'
insert newincident;
// second object related as lookup, incident__c is field related to it.
email_to_incident_history__c emailincident=new email_to_incident_history__c();
emailincident.incident__c=newincident.Name;
insert emailincident;
But it is throwing an error, saying invalid id.
 
Best Answer chosen by Abhishek Singh 88
Amit Singh 1Amit Singh 1
Abhishek,

incident__c is a look up field into email_to_incident_history__c object right? Use below code.
BMCServiceDesk__Incident__c newincident= new BMCServiceDesk__Incident__c();
newincident.Name='Wellspan';
newincident.description__c='Test cases.'
insert newincident;
// second object related as lookup, incident__c is field related to it.
email_to_incident_history__c emailincident=new email_to_incident_history__c();
emailincident.incident__c=newincident.Id;
insert emailincident;
Let me know if this works :)
Thanks!
Amit Singh
 

All Answers

Amit Singh 1Amit Singh 1
Abhishek,

incident__c is a look up field into email_to_incident_history__c object right? Use below code.
BMCServiceDesk__Incident__c newincident= new BMCServiceDesk__Incident__c();
newincident.Name='Wellspan';
newincident.description__c='Test cases.'
insert newincident;
// second object related as lookup, incident__c is field related to it.
email_to_incident_history__c emailincident=new email_to_incident_history__c();
emailincident.incident__c=newincident.Id;
insert emailincident;
Let me know if this works :)
Thanks!
Amit Singh
 
This was selected as the best answer
NGD_CLOUDNGD_CLOUD
Hi Abhishek,

You shoud try below updated code :- 
 
BMCServiceDesk__Incident__c newincident= new BMCServiceDesk__Incident__c();
newincident.Name='Wellspan';
newincident.description__c='Test cases.'
insert newincident;
// second object related as lookup, incident__c is field related to it.
email_to_incident_history__c emailincident=new email_to_incident_history__c();
emailincident.incident__c=newincident.Id;
insert emailincident;

I have updated line of code  : emailincident.incident__c=newincident.Id;

If you feel my answer is work for you then choose as a best answer.

Best Regards - Nagendra
Abhishek Singh 88Abhishek Singh 88
Thanks All,
Since all answer are correct so i marked first one as best answer.