1

I examined the questions that popped up for "may already have your answer," but was still not able to resolve my issue.

These are my goals:

1) Runs after a trigger is inserted or updated, and checks if a sales rep is present, and if so is different from previous value.

2) if a rep is present(and different from previous value), we check to see if it already exists in our "Sales Team"(using a Sales Team custom object. The original Opportunity team object is being used for something else, not my call).

3) Issue: I want to build this thing so it is properly bulkified. I need to create some SOQL queries to compare my results, to make sure we are only inserting reps to team members if they are assigned to one, and were not previousely added to the sales team at a prior point in time.

What I have worked out already:

This is the code I have worked out using different examples from Stack Exchange and the official salesforce support forums, and documentation for SOQL.

if((Trigger.isInsert||Trigger.isUpdate) && Trigger.isAfter)
{   
    List<SObject> oppList = new List<SObject>();
    for(Opportunity curOp : Trigger.new)
    {
        System.debug('Inside of Trigger.new for loop...');
        Opportunity oldmap = Trigger.oldmap.get(curOp.Id);
        if(curOp.Sales_Rep__c != oldmap.Sales_Rep__c) oppList.add(curOp);
    }
    //Query using the returned opp list to generate a rep list with recruiters
    List<sObject> repList = [SELECT Id, Sales_Rep__c, Sales_Rep__r.Recruiter__c, Sales_Rep__r.AccountId 
                            FROM Opportunity
                            WHERE Id in : oppList
                            ];
    System.debug('repList :: ' + repList);
    //Query - we want to check to see if the current sales rep exists in an opp sales team already - 
    //If so, we do not want to add them to the same sales team a second time
    List<Sales_Team_Member__c> toRelate = [SELECT Id, Account__c, Opportunity__c, Contact__c 
                                            FROM Sales_Team_Member__c
                                            WHERE (Opportunity__r.Id in : oppList)
                                            AND (Contact__c not in : repList)
                                            ];
    System.debug('toRelate :: ' + toRelate);

}

The following error is thrown by salesforce: System.QueryException: Invalid bind expression type of Opportunity does not match domain of foreign key

I know once I have a list returned again, I'll need to iterate through it to continue operations. I'm pretty sure I can get through that part. I just cannot figure out how to format this query.

I've returned replist, which shows our opportunities we are updating, and the associated sales rep and recruiter related to those opportunities. Each is a contact lookup field for the latter. I'm then trying to compare this list with what is returned by the toRelate query, and make sure we are only returning results where the opportunity exists within our oppList, but does the user who has been designated as the rep or recruiter has not been assigned to the opportunity before.

If I can get it to return the correct results, then I'll will iterate through the list and insert the new sales team members for their related opportunities.

The first system debug works fine, and returns the rep list. But I keep getting errors for the toRelate query, and cannot resolve it. Any advice would be greatly appreciated. Thanks!

  • 1
    repList contains list of opportunities, but in query you comparing to Contact ids (I believe that Contact__c is relashionship name to Contacts) – Ilya Lepesh Aug 22 '16 at 22:50
2

I've returned replist, which shows our opportunities we are updating, and the associated sales rep and recruiter related to those opportunities.

List<sObject> repList = [SELECT Id, Sales_Rep__c, Sales_Rep__r.Recruiter__c, 
    Sales_Rep__r.AccountId FROM Opportunity WHERE Id in : oppList];

If I'm not mistaked, you're expecting to use filter on Sales_Rep__c and Sales_Rep__r.Recruiter__c ids. It's not gonna happened. Your list will return only opportunity Ids. Yes, those fields are part of the opportunity, but you have to populate your filter manually.

If Sales_Rep__c and Sales_Rep__r.Recruiter__c are pointing to Contact, following example should work:

set<id> repIds = new set<id>();

for (Opportunity opp :[SELECT Id, Sales_Rep__c, Sales_Rep__r.Recruiter__c, 
                            Sales_Rep__r.AccountId 
                            FROM Opportunity WHERE Id in : oppList]) {
    repIds.add(opp.Sales_Rep__c);
    repIds.add(opp.Sales_Rep__r.Recruiter__c);
}
repIds.remove(Null);

List<Sales_Team_Member__c> toRelate = [SELECT Id, Account__c, Opportunity__c, Contact__c 
    FROM Sales_Team_Member__c 
    WHERE (Opportunity__r.Id in : oppList) AND (Contact__c not in : repIds)
];
  • Sorry, I meant to comment sooner. I was indeed just expecting that the query would return IDs. Your query examples made a big difference for me. I'm making progress now with my code. I appreciate the help very much! – Genko Aug 23 '16 at 21:08

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.