0

All Apex experts!

I am correctly displaying an Aggregate Result set from 3 distinct Salesforce Objects.

However I am seeing an issue with repeating values on my Visualforce component with these 2 items on my list:

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 ) );

Not sure what I am doing wrong here! Greatly appreciate the help removing repeating entries!

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() {
Account a = getAccount(this.controller != null ? this.controller.getId() : accountId);
    List<CoverageTeamMember> l = getCoverageTeam(a);
    l.addAll(getClaimsCoverageTeam(a));
return l;
}

//Account query to get Servicer 1 & 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
private List<CoverageTeamMember> getCoverageTeam( Account account ) {
    List<CoverageTeamMember> result = this.getStaticCoverageTeam( account );

    for ( AggregateResult records : [ 
        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( records ) );
    }

    return result;
}

//Claims query to get Active Claims Coverage Team Members
private List<CoverageTeamMember> getClaimsCoverageTeam( Account account ) {
    List<CoverageTeamMember> result = this.getStaticCoverageTeam( account );

    for ( AggregateResult claims : [ 
        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( claims ) );
    }

    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 ) );
    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 records) {
    this( String.valueOf( records.get( 'role' ) ), 
    String.valueOf( records.get( 'name' ) ), 
    String.valueOf( records.get( 'actualname' ) ),
    String.valueOf( records.get( 'email' ) ), 
    String.valueOf( records.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;
}
}
}

dupe

0

It looks like the same Servicer__c object may be referenced from the two queries.

One solution is to modify the logic to also capture Servicer__r.Id in the aggregate queries and to store that in the CoverageTeamMember class (in a field called say servicerId). Then you can discard the duplicates at the end of the getCoverageTeam method:

...
l.addAll(getClaimsCoverageTeam(a)); 

Set<Id> ids = new Set<Id>();
List<CoverageTeamMember> ll = new List<CoverageTeamMember>();
for (CoverageTeamMember ctm : l) {
    if (!ids.contains(ctm.servicerId)) {
        ll.add(l);
        ids.add(ctm.servicerId);
    }
}
return ll;

PS

Based on the comments the above isn't right. Instead moving the pieces into one method might be the simplest way to go:

public List<CoverageTeamMember> getCoverageTeam() {

    Account account = getAccount(this.controller != null ? this.controller.getId() : accountId);

    // Add Producer and Client Owner once
    List<CoverageTeamMember> result = this.getStaticCoverageTeam(account);

    // Policy query to get Active Policy Coverage Team Members
    for ( AggregateResult records : [ 
        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( records ) );
    }

    // Claims query to get Active Claims Coverage Team Members
    for ( AggregateResult claims : [ 
        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( claims ) );
    }

    return result;
}
  • The issue I am seeing is with these 2 repeating values that are pulled from the Account object. I suppose I'm not clear on what this has to do with Servicer__c. – SFDC-ThEq Mar 21 '16 at 21:04
  • 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 ) ); – SFDC-ThEq Mar 21 '16 at 21:06
  • @SFDCCrystalCo OK I misunderstood. Another attempt in the PS. – Keith C Mar 21 '16 at 21:39
  • That did the trick! Cheers to you @KeithC! Where should I send the beers? – SFDC-ThEq Mar 22 '16 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.