0

Calling all the apex gurus!

I have a class that is working as expected with 1 Object and 1 Aggregate Result query.

I have a requirement to Aggregate a second Object and include that in my result set as well?

Both of these Objects are simply child objects (Policy/Claim) to the parent (Account).

Thank you in advance!

public with sharing class SL_ctrl_AccountCoverageTeam_comp {

private final ApexPages.StandardController controller;
public String accountId {get;set;}

public SL_ctrl_AccountCoverageTeam_comp() { 
    //accountId = ApexPages.currentPage().getParameters().get('id');
}

public List<CoverageTeamMember> getCoverageTeam() {
    if(this.controller != null)
        return this.getCoverageTeam( getAccount( this.controller.getId() ) );
    else
        return this.getCoverageTeam( getAccount( accountId ) );
}

//Account query to get Servicer1 & 2
private Account getAccount( ID account ) {
    return [ SELECT Producer__r.Name, Producer__r.Email__c, Producer__r.Work_Phone__c, Servicer_1__c, Serv1__r.Name, Serv1__r.Email__c, Serv1__r.Work_Phone__c, Servicer_2__c FROM Account WHERE Id = :account ];
}

//Policy query to get Active Policy Coverage Team Members - this is already working as expected
private List<CoverageTeamMember> getCoverageTeam( Account account ) {
    List<CoverageTeamMember> result = this.getStaticCoverageTeam( account );

    for ( AggregateResult policy : [ 
        SELECT Servicer_Department_Name__c role, Servicer__r.Id name, Servicer__r.Name actualname, Servicer__r.Email__c email, Servicer__r.Work_Phone__c phone, Active__c a
        FROM Policy__c
        WHERE Account__c = :account.Id AND 
            Servicer_Department_Name__c != null AND
            Servicer__c != null AND
            Active__c = True
        GROUP BY Servicer_Department_Name__c, Servicer__r.Id, Servicer__r.Name, Servicer__r.Email__c, Servicer__r.Work_Phone__c, Active__c
    ] ) {
        result.add( new CoverageTeamMember( policy ) );
    }

    return result;
}

//**** I would like to add this to my resultset ***
private List<CoverageTeamMember> getClaimsCoverageTeam( Account account ) {
    List<CoverageTeamMember> result = this.getStaticCoverageTeam( account );

    for ( AggregateResult claim : [ 
        SELECT Servicer_Department_Name__c role, Servicer__r.Id name, Servicer__r.Name actualname, Servicer__r.Email__c email, Servicer__r.Work_Phone__c phone, IsClosed__c a
        FROM Claim__c 
        WHERE Account__c = :account.Id AND 
            Servicer_Department_Name__c != null AND
            Servicer__c != null AND
            IsClosed__c = False
        GROUP BY Servicer_Department_Name__c, Servicer__r.Id, Servicer__r.Name, Servicer__r.Email__c, Servicer__r.Work_Phone__c, IsClosed__c
    ] ) {
        result.add( new CoverageTeamMember( claim ) );
    }

    return result;
}

private List<CoverageTeamMember> getStaticCoverageTeam( Account account ) {
    List<CoverageTeamMember> result = new List<CoverageTeamMember>();
    result.add( new CoverageTeamMember( 'Producer', account.Producer__r.Id, account.Producer__r.Name, account.Producer__r.Email__c, account.Producer__r.Work_Phone__c) );
    result.add( new CoverageTeamMember( 'Client Owner', account.Serv1__r.Id, account.Serv1__r.Name, account.Serv1__r.Email__c,account.Serv1__r.Work_Phone__c ) );
    //result.add( new CoverageTeamMember( 'P&C Servicer Coordinator', account.Servicer_1__c ) );
    //result.add( new CoverageTeamMember( 'Benefits Coordinator', account.Servicer_2__c ) );
    return result;
}

public class CoverageTeamMember {
public final String role { get; set; }
public final String name { get; set; }
public final String actualname { get; set; }
public final String email { get; set; }
public final String phone { get; set; } 


public CoverageTeamMember( AggregateResult policy) {
    this( String.valueOf( policy.get( 'role' ) ), 
    String.valueOf( policy.get( 'name' ) ), 
    String.valueOf( policy.get( 'actualname' ) ),
    String.valueOf( policy.get( 'email' ) ), 
    String.valueOf( policy.get( 'phone') ) );
}

public CoverageTeamMember( String role, String name, String actualname, String email, String phone) {
    this.role = role;
    this.name = name;
    this.actualname = actualname;
    this.email = email;
    this.phone = phone;
    }
  }
}

1 Answer 1

1

If you mean you want to append the second set of records then this should do it:

public List<CoverageTeamMember> getCoverageTeam() {
    Account a = getAccount(this.controller != null ? this.controller.getId() : accountId);
    List<CoverageTeamMember> l = getCoverageTeam(a);
    l.addAll(getClaimsCoverageTeam(a));
    return l;
}

(You should probably rename the policy parameter in public CoverageTeamMember( AggregateResult policy) to something more general as you are using it for both policy and claim).

4
  • The addAll method returns void...
    – Adrian Larson
    Commented Mar 21, 2016 at 18:33
  • @AdrianLarson Thanks Adrian. I've fixed in the answer.
    – Keith C
    Commented Mar 21, 2016 at 18:36
  • Granted, they really should make it fluent. There's basically no reason to ever return void. I doubt it affects performance...
    – Adrian Larson
    Commented Mar 21, 2016 at 18:38
  • Smashing gents! Greatly appreciate the help. Now working as expected! @KeithC
    – SFDC-ThEq
    Commented Mar 21, 2016 at 19:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .