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

Replay

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'];

System.debug('###'+accountList);

for(Account obj : accountList){
    System.debug(':::'+obj.Contacts);
    System.debug(':::'+obj.AccountContactRoles);
}

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.:
    System.debug(a.StageName);
    for (OpenActivity a : o.OpenActivities) {
        // Access the related OpenActivity object fields via "a" e.g.:
        System.debug(a.Status);
    }
}

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 Force.com 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

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

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