0

I have a lookup to custom object from Opportunity called Team__c. Team__c has a child object called Team_Members__c (Master-Detail relationship between them). I need to create an new opportunity when opportunity is closed won and Account field on opportunity is different from Account field on Team_Members__c object.I am not sure how to compare the Opportunity.Account field with Team member Account Field.Please help me.

trigger OpportunityTrigger on Opportunity (after update) {
 map<Id,Id>oppAccountMap = new map<Id,Id>();
 map<Id,Id>oppTeamMap = new map<Id,Id>();
 list<Team__c> teamlist = new list<Team__c>();
for(Opportunity opp : trigger.new){
  if((opp.StageName == 'Closed Won') && (opp.Team__c!=null)&& (opp.AccountId!=null)){
      oppAccountMap.put(opp.Id,opp.AccountId);
      oppTeamMap.put(opp.Id,opp.Team__c);
    }
 }
list<Team__c> tlist = [Select  c.Id, (Select Id, Name, Account__c, Team__c, From Team_Members__r) From Team__c where Id in: oppTeamMap.values()];
for(Team__c team : tlist){
   for(Team_Members__c members :team.Team_Members__r){
       if(members.Account__c != null){
          // I am not sure how to compare members.Account__c != opportunity.Account__c
       }
    }
 } 
}
4
  • 2
    Please reformat your code (remove the tabs and replace with spaces) to make it easier for us to read. Additionally, provide us with what exactly you need help with. Do you need help compiling the code, does the code just not work as you'd expect, do you need design help, etc. Simply posting a code snippet doesn't allow us to help you efficiently and effectively. Apr 1, 2014 at 15:13
  • @JamesLoghry I am not sure how to compare members.Account__c != opportunity.Account__c.Can you help me
    – sfdc
    Apr 1, 2014 at 15:26
  • @sf.dev You would get more help if your code layout was clean and anyone who has to touch your code in the future would also benefit.
    – Keith C
    Apr 1, 2014 at 16:21
  • @KeithC I have updated code layout.please look into it
    – sfdc
    Apr 1, 2014 at 16:24

1 Answer 1

1

As I understand the data model, the Team__c object is parent object of both Team_Member__c and Opportunity.

If that is correct, I suggest code like this:

trigger OpportunityTrigger on Opportunity (after update) {

    Set<Id> teamIds = new Set<Id>();
    for (Opportunity opp : Trigger.new){
        if (opp.StageName == 'Closed Won' && opp.Team__c != null && opp.AccountId != null) {
            teamIds.add(opp.Team__c);
        }
    }

    if (teamIds.size() > 0) {
        List<Opportunity> inserts = new List<Opportunity>();
        for(Team__c team : [
                select (select Account__c from Team_Members__r where Account__c != null),
                        (select Account__c from Opportunities__r where Account__c != null)
                from Team__c
                where Id in :teamIds
                ]) {
            Set<Id> memberAccountIds = new Set<Id>();
            for (Team_Members__c member : team.Team_Members__r) {
                memberAccountIds.add(member.Account__c);
            }
            Set<Id> oppAccountIds = new Set<Id>();
            for (Opportunity opp : team.Opportunities__r) {
                oppAccountIds.add(opp.Account__c);
            }
            // Decide what to do here by comparing the two sets
            // Add the required Opportunity objects to the inserts list
        }
        insert inserts;
    }
}

Instead of trying to model the relationships in maps, by requerying the __r fields become available and those lists can be looped over. The code assumes the Team__c to Opportunity relationship is named Opportunities__r.

Note that the first part of the code that identifies the teamIds should check for changes on the specific fields that this logic is concerned about to avoid doing unnecessary work.

You must log in to answer this question.

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