function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ryan BobadillaRyan Bobadilla 

Trying to pull Partner from PartnerOpportunity Object (related list on Opp) into lookup field on Opportunity... Not executing...please help :(

Hello

Here is the trigger that was created... pleass help!!

trigger UpdatePartnerOpportunity on Opportunity (before insert, before update) {
// THIS TRIGGER WILL OVERWRITE ANY PARTNER DEFINED IN THE FIELD Partner ON THE Opportunity 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...

                       System.debug(' output inside ' + o);

       OpportunityPartner[] PartnerArray = 
       [select AccountToID, IsPrimary from OpportunityPartner where OpportunityId = :o.id ORDER BY isPrimary DESC, CreatedDate];
       
                              System.debug(' PartnerArray ' + PartnerArray);

       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<Opportunity> AccountArray = [select id,Partner_on_Opportunity__c from Opportunity where Id = :o.AccountId ];
           for (Opportunity a: AccountArray ){
                       o.Partner_on_Opportunity__c=PartnerArray[0].AccountToID;
                       System.debug(' output inside' + o.Partner_on_Opportunity__c);
            update o;
                       }
            
       }else{
      
           // IF NO PARTNERS EXIST RETURN NULL...
           o.Description = 'No Partner';
           System.debug(' Output else statement' + o.Description);
           
       }
   }
 }
anto nirmalanto nirmal
Hi Ryan,

It would be helpful if you share the error message as well.
But still I noted a few keys things in your code.

Update and Insert events should be handled seperately when you are updating the record.
And you need not update the object in the before update event, because anyway the record gets updated.

Regards,
Anto Nirmal