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
DarrellDDarrellD 

Using Get for SObject List Item

Having a little trouble understand how to reference an SObject ID in my test class. The SObject was created earlier in the class and now I need to get the ID from that to populate a lookup field on another SObject for the test.  Actually, there are 3 fields in total.  I put the 2 lists in question below:

 

List 1 - No errors received here

List<IndividualAddress__c> indaddrlist = new List<IndividualAddress__c>();

for (Integer x=0;x<200;x++){
IndividualAddress__c ia = new IndividualAddress__c (Active__c=True,Address__c=addrlist.get(x).Id,Address_Type__c='Practice',
Individual__c=indlist.get(x).Id
);
indaddrlist.add(ia);
}
System.assertEquals(indaddrlist.size(),200);
insert indaddrlist;

 

List 2 - Getting an error on line below.  For IndividualAddress__c, I'm trying to iterate to get the IDs of the SObjects created in list above and populate from those.
// create individual schedule
List<Indvidual_Schedule__c> indschlist = new List<Indvidual_Schedule__c>();

for (Integer x=0;x<200;x++){
Indvidual_Schedule__c is = new Indvidual_Schedule__c (Comment__c='Test Schedule', Day__c='Monday',
Start_Time__c='08:00:00 AM', End_Time__c='09:00:00 AM', RecordTypeId='01240000000I4yx',
**TROUBLE SPOT  IndividualAddress__c =indaddrlist.get(x).Id, Individual__c = indaddrlist.Individual__c, Address__c = indaddrlist.Address__c
);
indschlist.add(is);
}

 

Thanks for any help!

Darrell

Best Answer chosen by Admin (Salesforce Developers) 
UVUV

indaddrlist List will give you id's of IndividualAddress__c object by using directly like myfield=indaddrlist[0].id but in case you want to assign any other field then query the IndividualAddress__c where Id in:indaddrlist and then iterate through earch record to assign the fields.

I hope this answer your question.