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
Rudi OrnelasRudi Ornelas 

insert child records into related list

Hi Everyone,

I have a trigger populating an Account lookup(id) and a distance field (num) on Account record. This limits me to only one record. I would like to see the 10 nearest record. I understand I will need a custom object with a master-detail lookup to account as well as the Account lookup(id) and a distance field (num). 

How would I achieve this?
Here's the part of my code where I'm looping thru my acctList and insert. I just can't figure out how to change my code to insert detail records instead 
//Compare Distance of all Index and store nearest one in Intialized variable.
	for(Integer i = 0; i < accList.size(); i++ ){
Double Distance = ds.getDistanceAtIndex(i);
Double Traveltime = ds.getTravelTimeAtIndex(i);
List<double> distances = ds.getListOfDistances();


    if(i == 0){
        NearestDistance = Distance;
        String_AccountId = accList.get(i).Id;
        NearestTravelTime = 0;
       
    }
        If(i == 1){
        if(NearestDistance > Distance){
        NearestDistance = Distance;
        String_AccountId = accList.get(i).Id;
        NearestTravelTime = Traveltime;
    }
            
    }
    
} 
    
    
     
//add final distance, Account Id and Drive time to newly inserted Account recod.
objAccount.Distance__c = NearestDistance;
objAccount.NearestAccount__c = String_AccountId;
objAccount.TravelTime__c = NearestTravelTime;
    
Update objAccount;

    }
}​