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

Need help with a for loop trigger - Thinking the logic is right, but the syntax isn't

Use case - Field is updated on opportunity, select contactid and role associated to opportunity via opportunitycontactrole, then update related fields on the various contact records.

I am having issues getting the relationship between objects correct. 

I am looping through an opportunity list based on the ID.
In the loop I'm pulling all the contact IDs and roles from the OpportunityContactRole base related to said ID.

After that I'm trying to update the contact record based on the contact record id, but I cant get the relationship between the contact ID and opporuntitycontactrole object's contactID to associate.  Any ideas?

Appreciate the help.
ANUTEJANUTEJ (Salesforce Developers) 
Hi Jim,

According to your second statement I see that you are querying in a loop that would lead to soql 101 error in case if you have a large number of id's so to work aroung this scenario I would suggest you to query all the id's in a set and use IN operator in soql qury to get all the necessary details.

So for getting the relationship name you can navigate to that particular object via the setup from UI and you can get the relation ship name from the field that is present on the object.

In case if this information was helpful I would request you to please close the thread and mark this answer as the best answer so that it can be used by others in the future and also helps in keeping the community clean.

Hi Anutej,

Sorry for the delay.  After posting I realized that I had done that (SOQL in a loop) and have pulled the necessary data into a map prior to looping.  As far as the relationship I was over thinking it.  

Thanks for the suggestion.