Using Opportunity Selector to retrieve OpportunityContactRole

Viewed 90

I am very new to apex, I am trying to understand apex code written by someone else. Its retrieving opportunity data. It gets all the fields except opportunity contact role. Its always empty, even though there is data in it.

It calls the opportunity selector from the service class

Opportunity opp = cq.Opportunities.GetByIdWithOCR(new Set{opportunityId})[0];

in the opportunitySelector.apxc

public List<Opportunity> GetByIdWithOCR(Set<Id> idSet) {
        return (List<Opportunity>) GetQueryFactory()
                .WithCriteria(cq_Criteria.ValueIn(Opportunity.Id, idSet))
                .WithRelatedField(Opportunity.AccountId, Account.Name)
                .WithRelatedField(Opportunity.AccountId, Account.BillingStreet)
                .WithRelatedField(Opportunity.AccountId, Account.BillingCity)
                .WithRelatedField(Opportunity.AccountId, Account.BillingState)
                .WithRelatedField(Opportunity.AccountId, Account.BillingPostalCode)
                .WithRelatedField(Opportunity.AccountId, Account.BillingCountry)
                .WithChildQuery(
                        cq.OpportunityContactRoles.GetQueryFactory()
                            .WithCriteria(cq_Criteria.Equals(OpportunityContactRole.Role, 'Decision Maker'))
                            .WithRelatedField(OpportunityContactRole.ContactId, Contact.Name)
                            .WithRelatedField(OpportunityContactRole.ContactId, Contact.Phone)
                            .WithRelatedField(OpportunityContactRole.ContactId, Contact.Email))
                .Execute();
    }

all the values are there but contact role is empty. Please advice

1 Answers

This is all custom written by your colleague, some attempt at ORM layer. "Normal" apex would be to write the query directly, better performance, some compile-time checks... Hard to say what lurks in that code.

Which community it is? Customer or Partner? Last I checked customer community has no access to Opportunity. Assuming it's Partner - do you know how to check the Profile of the community user and access to whole OpportunityContactRole table and all fields in it?

There are some hacks to let code jump out of the Salesforce security model (without sharing etc) but I wouldn't recommend unless you really know what you're doing and normal ways of controlling data visibility (profiles/permission sets, sharing rules, like sharing sets) failed you.

Try to capture debug logs. Open developer console (if possible, you tagged this as communities) and run your page or experiment with Setup -> Debug Logs. You should be able to find the query and try to execute it manually.

If you want to rule out issues with this query factory try to swap the code with this and see if it's any better:

return [SELECT Account.Name, Account.BillingStreet, Account.BillingCity,
    Account.BillingState, Account.BillingPostalCode, Account.BillingCountry,
    (SELECT Contact.Name, Contact.Phone, Contact.Email
    FROM OpportunityContactRoles
    WHERE Role = 'Decision Maker')
    FROM Opportunity
    WHERE Id IN :idSet];
Related