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
Lisa KattawarLisa Kattawar 

Opportunity Trigger to copy partner from account

Background:
In our org
Accounts have a Partner related list (this is an account to account relationship with a "role" of "Reseller")
Opportunities have a Partner related list (this is an opportunity to account relationship with a "role" of "Reseller")
I need a trigger, such that when an opportunity is created on an account that has a "Reseller" Partner, the Opportunity gets created with the same "Reseller" Partner

I  believe the Opportunity Trigger will need to do the following:
On Opportunity Create
If Opportunity.Account has a Partner where Role=Reseller
    Add the Partner to the Opportunity with Role=Reseller

I think that's it.  I don't believe there's any "else" logic.

If  you have code that you're willing to share, I will be extremely grateful.  I'm not a developer, but if I can get some code that is close to what I need I can usually modify it for my specific needs.

Thanks in advance, for any assistance!!!
Balaji BondarBalaji Bondar
Hi Lisa ,

Seems like we cannt update these field :
https://success.salesforce.com/ideaView?id=08730000000BqP8
trigger PopulatePartner on Opportunity (after Insert) {
Set <Id> accountIdSet = new Set<Id>();
Map<Id , Partner> AccountIdPartnerMap = new Map<Id , partner>();
List<OpportunityPartner> partnerList = new List<OpportunityPartner>();

    for(Opportunity OpportunityObj :Trigger.New){
        accountIdSet.add(OpportunityObj.AccountId );
    }

    for(Partner partnerObj : [Select Id, Role, OpportunityId, AccountToId From Partner where Role='Vendor']){
        AccountIdPartnerMap.put(partnerObj.AccountToId , partnerObj); 
    }
    
    for(Opportunity OpportunityObj :Trigger.New){
        if(AccountIdPartnerMap.containskey(OpportunityObj.AccountId)){
            OpportunityPartner partnerObj = new OpportunityPartner ();
            partnerObj.role =  AccountIdPartnerMap.get(OpportunityObj.AccountId).role;
            partnerObj.OpportunityId = OpportunityObj.Id; 
            partnerObj.AccountToId =  AccountIdPartnerMap.get(OpportunityObj.AccountId).AccountToId;

            
            partnerList.add(partnerObj ); 
        }
    }
    insert partnerList;
}
I have develped this code.

Important :
If this is what you were looking for then please mark it as a "SOLUTION" or You can Click on the "Like" Button if this was beneficial for you.