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
dfpittdfpitt 

Help inserting child record of custom object - BRAND NEW TO FORCE.COM and APEX

Hello,

 

I'm brand new to APEX, I need some help.

 

I have a custom object called Patient__c and a custom object called EMR__c, I want to automatically create an EMR record when i create a patient, with just the patient field filled, the rest should be blank (and none other EMR fields are setup as required fields).

 

I created the trigger below, but I get the following error when saving a patient (the new patient's name is John Smith):

 

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger insert_EMR caused an unexpected exception, contact your administrator: insert_EMR: execution of AfterInsert caused by: System.StringException: Invalid id: John Smith: Trigger.insert_EMR: line 7, column 82

 

trigger insert_EMR on Patient__c (after insert) {

List<EMR__c> ListA = new List<EMR__c>();

for (Patient__c pat : trigger.new)
   {
    EMR__c emr1= new EMR__c(Patient__c=pat.name);
    ListA.add(ante);
    insert ListA;
   }
}

 

any ideas would be appreciated,

 

thanks

Best Answer chosen by Admin (Salesforce Developers) 
Baktash H.Baktash H.

Line 7:

try instead of EMR__c emr1= new EMR__c(Patient__c=pat.name);

this:

EMR__c emr1= new EMR__c(Patient__c=pat.Id);

All Answers

Baktash H.Baktash H.

Line 7:

try instead of EMR__c emr1= new EMR__c(Patient__c=pat.name);

this:

EMR__c emr1= new EMR__c(Patient__c=pat.Id);

This was selected as the best answer
dfpittdfpitt

That worked great, thank!