+ Start a Discussion
JIDzenpriseJIDzenprise 

Update Partner Field on Account from Opportunity Trigger

I've got this trigger halfway there whereby it will update Opportunity Description with the Partner ID from the Partner on the related list. I only did it this way to make sure the value being returned was the one I wanted. Anyway, I need this to update a custom Partner Account look up field on the Account Object. (I'm new to this). Can anyone point me in the right direction in terms of what I need to do next?

 

trigger PopulatePartnerOnAccountFromList on Opportunity (before update) {
// THIS TRIGGER WILL OVERWRITE ANY PARTNER DEFINED IN THE FIELD Partner ON THE ACCOUNT OBJECT.
// SET THIS FIELD TO READ ONLY OR CHANGE THE FUNCTIONALITY BELOW TO AVOID DATA BEEING OVERWRITTEN BY MISTAKE...

   for (Opportunity o : Trigger.new) {
   
 


       // CREATE ARRAY OF ALL PARTNERS ON THIS OPPORTUNITY. THE REASON WHY WE DONT PICK THE PRIMARY PARTNER ONLY
       // IS BECAUSE THE PRIMARY FLAG IS NOT ALWAYS SET WHEN PARTNERS ARE ADDED TO OPPORTUNITIES. ONLY WHEN SALES DOES TIS
       // MANUALLY FROM THE RELATED LIST IS PRIMARY CHECKBOX CHECKED...


       OpportunityPartner[] PartnerArray =
       [select AccountToID, IsPrimary from OpportunityPartner where OpportunityId = :o.id ORDER BY isPrimary DESC, CreatedDate];
       if (PartnerArray.size() > 0) {
         
           // IF PRIMARY IS DEFINED THEN THIS WILL BE THE FIRST OBJECT. IF NOT THE FIRST ADDED CONTACT ROLE WILL BE ADDED...
           o.Description = PartnerArray[0].AccountToID;
          
       }else{
      
           // IF NO PARTNERS EXIST RETURN NULL...
           o.Description = null;
                 }
   }
 }

 

Best Answer chosen by Admin (Salesforce Developers) 
JIDzenpriseJIDzenprise

If anyone ever needs to do something like this, I got it working

 

 

trigger PopulatePartnerOnAccountFromList2 on Opportunity (before update) {
// THIS TRIGGER WILL OVERWRITE ANY PARTNER DEFINED IN THE FIELD Partner ON THE ACCOUNT OBJECT.
// SET THIS FIELD TO READ ONLY OR CHANGE THE FUNCTIONALITY BELOW TO AVOID DATA BEEING OVERWRITTEN BY MISTAKE...

   for (Opportunity o : Trigger.new) {
  

 


       // CREATE ARRAY OF ALL PARTNERS ON THIS OPPORTUNITY. THE REASON WHY WE DONT PICK THE PRIMARY PARTNER ONLY
       // IS BECAUSE THE PRIMARY FLAG IS NOT ALWAYS SET WHEN PARTNERS ARE ADDED TO OPPORTUNITIES. ONLY WHEN SALES DOES TIS
       // MANUALLY FROM THE RELATED LIST IS PRIMARY CHECKBOX CHECKED...


       OpportunityPartner[] PartnerArray = 
       [select AccountToID, IsPrimary from OpportunityPartner where OpportunityId = :o.id ORDER BY isPrimary DESC, CreatedDate];
       if (PartnerArray.size() > 0) {
         
           // IF PRIMARY IS DEFINED THEN THIS WILL BE THE FIRST OBJECT. IF NOT THE FIRST ADDED PARTNER WILL BE ADDED...
                     List<Account> AccountArray = [select id,Partner_Account__c from Account where Id = :o.AccountId ];
           for (Account a: AccountArray ){
                       a.Partner_Account__c=PartnerArray[0].AccountToID;
            update a;
                       }
            
       }else{
      
           // IF NO PARTNERS EXIST RETURN NULL...
           o.Description = 'No Partner';
           
       }
   }
 }

 

All Answers

JIDzenpriseJIDzenprise

If anyone ever needs to do something like this, I got it working

 

 

trigger PopulatePartnerOnAccountFromList2 on Opportunity (before update) {
// THIS TRIGGER WILL OVERWRITE ANY PARTNER DEFINED IN THE FIELD Partner ON THE ACCOUNT OBJECT.
// SET THIS FIELD TO READ ONLY OR CHANGE THE FUNCTIONALITY BELOW TO AVOID DATA BEEING OVERWRITTEN BY MISTAKE...

   for (Opportunity o : Trigger.new) {
  

 


       // CREATE ARRAY OF ALL PARTNERS ON THIS OPPORTUNITY. THE REASON WHY WE DONT PICK THE PRIMARY PARTNER ONLY
       // IS BECAUSE THE PRIMARY FLAG IS NOT ALWAYS SET WHEN PARTNERS ARE ADDED TO OPPORTUNITIES. ONLY WHEN SALES DOES TIS
       // MANUALLY FROM THE RELATED LIST IS PRIMARY CHECKBOX CHECKED...


       OpportunityPartner[] PartnerArray = 
       [select AccountToID, IsPrimary from OpportunityPartner where OpportunityId = :o.id ORDER BY isPrimary DESC, CreatedDate];
       if (PartnerArray.size() > 0) {
         
           // IF PRIMARY IS DEFINED THEN THIS WILL BE THE FIRST OBJECT. IF NOT THE FIRST ADDED PARTNER WILL BE ADDED...
                     List<Account> AccountArray = [select id,Partner_Account__c from Account where Id = :o.AccountId ];
           for (Account a: AccountArray ){
                       a.Partner_Account__c=PartnerArray[0].AccountToID;
            update a;
                       }
            
       }else{
      
           // IF NO PARTNERS EXIST RETURN NULL...
           o.Description = 'No Partner';
           
       }
   }
 }

 

This was selected as the best answer
Mani PenumarthiMani Penumarthi

hi is it working or u want sollution?

JIDzenpriseJIDzenprise

It is working well. Thank you!

Amuktha Rao 19Amuktha Rao 19
Hi could you please provide me the solution to do the above in thee reverse.
I want partners on the Account to be copied to Opportunity partner.