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
Terell PigramTerell Pigram 

How to set a Master-Detail value with APEX

I have an object call "Applicant" with a field called "high_school__c"*(Master).  high_school_data__c has a master detail relationships with another object called "high_school_data__c"(detail).  In APEX, how would can I create a new instance of "Applicant" and also create it's "high_school_data__c" details.

Example:
Applicant a = new Applicant();
a.high_school__r.Name__c = "East High School".

I've tried doing something like this but it doesn't seem to work.  Could someone please assist me?
Shaijan ThomasShaijan Thomas
1. Create Applicant Record
2. Crate Detail Record (Hight School Data). Set relation ship Field to Applicant Id
3. Insert Apllicant Then insert High School Data
Thanks
Shaijan Thomas
Shaijan ThomasShaijan Thomas
Example:
//Try this
Applicant app = new Applicant ();
app.name ='Test Applicant';
insert app;
hight_school_data__c A = new hight_school_data__c ();
A.hight_school__c=app.id;
insert A;

Thanks
Shaijan
FlyBeaver.comFlyBeaver.com
It is not clear wich object is master and which is detail. If Applicant is master use below example

Applicant__c a = new Applicant__c();
insert a;

high_school__c school = new high_school__c();
school.Applicant__c = a.Id;
insert school;

Please make sure to insert record you need to populate all required fields. Also check the right relationship name.