+ Start a Discussion
jpb@uhcjpb@uhc 

Opportunity Trigger

We have created a trigger (or attempted to) on Opportunity that will in effect go out and look at the Account associated with the Oppty and then look at the Partners associated with that Account. We are then attempting to populate a custom text field  (special_instructions__c) on the Opportunity with the addresses of ALL Partners (currently only pulling BillingStreet). However, what we are currently getting is just the BillingStreet of just one of the Partners. I am new to Apex so wonder if I am missing some sort of loop method, any help is greatly appreciated. Here is the trigger:

 

trigger SpecialInstructions on Opportunity (before insert, before update) {
    
    //Create Map references. Trigger maps to Opportunity, Opportunity maps to Account, Account maps to Partner.
    Map<Id, Id> OpportunityToAccountMap = new Map<Id, Id>();
    Map<Id, Id> AcctToPartnerMap = new Map<Id, Id>();
    Map<Id, Account> PartnerResponseMap = new Map<Id, Account>();

    
    //Map the Account Ids by querying the AccountID to OpportunityId (Trigger.New)
    for(Opportunity oppty : [SELECT Id, AccountID FROM Opportunity WHERE ID in: trigger.new]){
        OpportunityToAccountMap.put(oppty.id, oppty.AccountId);
    }
    
    //Map the Partner Ids by querying the AccountFromId to the AccountToID from the map above
    for(Partner partner : [SELECT Id, AccountToId, AccountFromId FROM Partner WHERE AccountFromId in: OpportunityToAccountMap.values()]){
        AcctToPartnerMap.put(partner.AccountFromId, partner.AccountToId);
    } 
    
    //Map the needed fields from AccountToID by querying the Account from the map above
    for(Account part : [SELECT Id, BillingStreet FROM Account WHERE ID in: AcctToPartnerMap.values()]){
        PartnerResponseMap.put(part.ID, part);
    }
    
    for(Opportunity o : trigger.new){
        
        //Retreived mapped records specific to the current Opportunity record
        
        //Get account id from OppportunityToAccountMap using current opportunity id
        Id acctid = OpportunityToAccountMap.get(o.Id);
        //Get accountid for partners from AcctToPartnerMap using acctid reference above
        Id partnerid = AcctToPartnerMap.get(acctid);
        
        //Avoid "System.NullPointerException: Attempt to de-reference a null object" error by
        //first verifying that your query produced results
        if(PartnerResponseMap.containsKey(partnerid)) {
            
            //If query produced results, then proceed to mapping the PartnerResponse using the AccountToID
            //refrence above
            Account PartnerResponse = PartnerResponseMap.get(partnerid);
        
            //Update Opportunity using values from PartnerResponse (Opportunity-->Account-->Partner-->PartnerResponse-->Opportunity)
            if(o.Special_Instructions__c == null){
                o.Special_Instructions__c = o.Special_Instructions__c + PartnerResponse.BillingStreet;  
            }
        }
    } 
}

 

SrikanthKuruvaSrikanthKuruva

I see that the following code will result in only one partner

 Account PartnerResponse = PartnerResponseMap.get(partnerid);
        
            //Update Opportunity using values from PartnerResponse (Opportunity-->Account-->Partner-->PartnerResponse-->Opportunity)
            if(o.Special_Instructions__c == null){
                o.Special_Instructions__c = o.Special_Instructions__c + PartnerResponse.BillingStreet;  
            }

 so you will have to keep the above code in a loop which will get all the partners related to the opportunity's account.

jpb@uhcjpb@uhc

Thanks SrikanthKuruva ! I implemented a "List" and added the values i needed to the List to populate the custom text field later. This was an interesting excercise too in takeing the values in multiple fields and inserting them in to a single text field. For anyone interested, here is the code...let me know if you think there are enhancements i could make to it (i'm more of an admin and not a developer):

 

trigger SpecialInstructions on Opportunity (before insert, before update) {
    
    //Create Map references. Trigger maps to Opportunity, Opportunity maps to Account, Account maps to Partner.
    Map<Id, Id> OpportunityToAccountMap = new Map<Id, Id>();
    Map<Id, Id> AcctToPartnerMap = new Map<Id, Id>();
    Map<Id, Account> PartnerResponseMap = new Map<Id, Account>();

    
    //Map the Account Ids by querying the AccountID to OpportunityId (Trigger.New)
    for(Opportunity oppty : [SELECT Id, AccountID FROM Opportunity WHERE ID in: trigger.new]){
        OpportunityToAccountMap.put(oppty.id, oppty.AccountId);
    }
    
    //Map the Partner Ids by querying the AccountFromId to the AccountToID from the map above
    list <Id> partner1 = new list <Id>();
    for(Partner partner : [SELECT Id, AccountToId, AccountFromId FROM Partner WHERE AccountFromId in: OpportunityToAccountMap.values()]){
     
        AcctToPartnerMap.put(partner.AccountToId , partner.AccountToId);
    } 
    //system.debug('partner1' + partner1);
    
    //Map the needed fields from AccountToID by querying the Account from the map above
    for(Account part : [SELECT Id, BillingStreet, BillingCity FROM Account WHERE ID in: AcctToPartnerMap.values()]){
        PartnerResponseMap.put(part.ID, part);
    }
    
    for(Opportunity o : trigger.new){
        
        //Get account id from OppportunityToAccountMap using current opportunity id
        Id acctid = OpportunityToAccountMap.get(o.Id);

        //Get accountid for partners from AcctToPartnerMap using acctid reference above
         list <String> partnerAcc = new list <String>(); 
         for( id Accid : AcctToPartnerMap.keyset()){
             Account acc = PartnerResponseMap.get(Accid);
             partnerAcc.add(acc.BillingStreet+'\n');
             partnerAcc.add(acc.BillingCity);
             partnerAcc.add('\n\n');
         }
         for(integer i=0;i<partnerAcc.size();i++){
         }
        
            //Update Opportunity using values from PartnerResponse (Opportunity-->Account-->Partner-->PartnerResponse-->Opportunity)
            if(o.Special_Instructions__c == null){
                o.Special_Instructions__c = '';
               for(integer i=0;i< partnerAcc.size();i++){
                       o.Special_Instructions__c += partnerAcc[i];  
                }
        }
    } 
}