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
Hugo_BHugo_B 

Sample code to loop through a related list.

I need to loop through one (and more) related lists of a custom object.  The related lists are also custom objects.

 

Any help on the basic code structure?  How do I identify the records only associated with the current object, then loop through them and, for example, assign records of that related list to a variable, which I in turn update the parent object.   In essence creating a trigger "roll-up" function to a custom object and its related list records.

Best Answer chosen by Admin (Salesforce Developers) 
pankaj.raijadepankaj.raijade

here is the code

 

list<Project__c> lstProject = [select id, (select id, Price_Per_Unit__c, Actual_Units__c from Project__r ) from project__c where id in :setProjectID];

 

for(Project__c oProject: lstProject)

{

integer TotalPrice = 0;

for(Budget__c oBudget :oProject.Project__r )

{

TotalPrice += Price_Per_Unit__c *Actual_Units__c ;

}

oProject.Total_Price__c = TotalPrice ;

}

 

Hope this helps.

 

Regards,

Pankaj Raijade

All Answers

pankaj.raijadepankaj.raijade

In trigger you can have a standard member " trigger.new", " trigger.Old" This is list of all the record that are inserted, update or deleted.

You can also get " trigger.Mapnew" "trigger.MapOld" these are tha maps of <ID, sobject>,

 

you can get id of all current object and retrive the releted records and process them.

 

if you are updating one records using standard UI this list or map will contain one records. 

 

if you are bulk updating or inserting or deleteing records it will contain all the records being processed.

 

Regards,

Pankaj Raijade,

Hugo_BHugo_B

Thank you, however, I am looking for syntax to identify and loop through the related list records; not just the trigger.... (unless I misunderstand your reply)

pankaj.raijadepankaj.raijade

We could help you best if you post your exact requirement.

 

Regards,

Pankaj Raijade,

Hugo_BHugo_B

Custom Object "Project" has related List "Budget".

 

Budget has the following fields that I would like to loop through (there can be anywhere from 1 to "n" related budget records to any project record.

- Product1__c (Lookup to Product object)

- Unit__c (Picklist).

- Price_Per_Unit__c (Currency field)

- Actual_Units__c (Number field).

 

 

I would like to loop through each related record, multiply the price per unit by the actual units and store the result in a variable, then update a field on the parent record with the value (adding it to the existing value).

 

 

pankaj.raijadepankaj.raijade

here is the code

 

list<Project__c> lstProject = [select id, (select id, Price_Per_Unit__c, Actual_Units__c from Project__r ) from project__c where id in :setProjectID];

 

for(Project__c oProject: lstProject)

{

integer TotalPrice = 0;

for(Budget__c oBudget :oProject.Project__r )

{

TotalPrice += Price_Per_Unit__c *Actual_Units__c ;

}

oProject.Total_Price__c = TotalPrice ;

}

 

Hope this helps.

 

Regards,

Pankaj Raijade

This was selected as the best answer
Hugo_BHugo_B

Thanks - I'll give that a whirl!