How to Access the SubQuery Result In Trigger

List listOpenActivity = [SELECT StageName, Id, (SELECT Id FROM OpenActivities), Probability FROM Opportunity WHERE Id =: Trigger.New];

In Above query there is OpenActivities which I would like to access in my Trigger can anyone tell me how to access that value


See the blog for SOQL Subquery You can use a inner query to achive this.

The sample example will be like,

List<Account> accountList = [Select Id, Name,(Select id from AccountContactRoles), (Select Id from Contacts)   from Account where Id=: 'some id'];


for(Account obj : accountList){

You can use code like this:

for (Opportunity o : [
        SELECT StageName, Id, (SELECT Id, Status FROM OpenActivities), Probability
        FROM Opportunity
        WHERE Id in :Trigger.New
        ]) {
    // Access Opportunity object fields via "o" e.g.:
    for (OpenActivity a : o.OpenActivities) {
        // Access the related OpenActivity object fields via "a" e.g.:

There is a list of Opportunity objects and each one of those has a list of OpenActivity objects accessed via the OpenActivities relationship field.

See e.g. A Deeper look at SOQL and Relationship Queries on for more detail.

Category: trigger Time: 2016-07-30 Views: 3
Tags: trigger

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development


Front-end development


development tools

Open Platform

Javascript development

.NET development

cloud computing


Copyright (C), All Rights Reserved.

processed in 4.786 (s). 13 q(s)