简体   繁体   中英

Salesforce Develpment

I'd like to write and trigger using Salesforce best practise that will link all contacts from the related account that are flagged as key contacts to an opportunity on that account when it is created.

What I already did:

trigger OppCreatedKeyContactTrigger on Opportunity (after insert, after Update) {
    for(Opportunity opp : Trigger.New){

        if(Trigger.isInsert || Trigger.isUpdate){
            
            Contact con = [SELECT Id,Name,Key_Contact__c FROM Contact WHERE AccountId =:opp.AccountId];

            con.Key_Contact__c = true;
            update con;
            
        }
    }
}

I also need a test class

Do you use OpportunityContactRole (standard related list under opportunities but might have to be enabled in setup) or do you have 1 or more custom lookups from Opportunity to Contact? What should happen "after update"?

Something like this should push you in right direction. I don't guarantee it'll compile, if you get stuck edit the question. It'll be just 1 query, not in a loop (bulk-safe). If you want to really go with best practices - next step would be to use some trigger handler framework.

trigger OpportunityTrigger on Opportunity (after insert) {

    Set<Id> accountIds = new Set<Id>();
    for(Opportunity o : trigger.new){
        accountIds.add(o.AccountId);
    }
    accountIds.remove(null);

    if(!accountIds.isEmpty()){
        Map<Id, Account> accounts = new Map<Id, Account>([
            SELECT Id,
                (SELECT Id FROM Contacts WHERE Key_Contact__c = true)
            FROM Account 
            WHERE Id IN :accountIds
        ]);
        List<OpportunityContactRole> roles = new List<OpportunityContactRole>();

        for(Opportunity o : trigger.new){
            Account a = accounts.get(o.AccountId);
            if(a != null && !a.Contacts.isEmpty()){
                for(Contact c : a.Contacts){
                    roles.add(new OpportunityContactRole(
                        OpportunityId = o.Id,
                        ContactId = c.Id,
                        Role = 'Decision Maker'
                    ));
                }
            }
        }
        insert roles;
    }
}

trigger OppCreatedKeyContactTrigger on Opportunity (after insert, after Update) {

   Set<Id> accountIds = new Set<Id>();

   for(Opportunity o : trigger.new){

       accountIds.add(o.AccountId);

   }

   accountIds.remove(null);

   List<Contact> conList = [SELECT Id,Name,Key_Contact__c FROM Contact WHERE AccountId IN :accountIds];

   List<contact> updateConList = new List<contact>();

   if(Trigger.isInsert || Trigger.isUpdate){

   for(Contact cons : conList){

       updateConList.add(new contact(id=cons.id,Key_Contact__c=true));

   }

}

if(updateConList.size()>0){

     update updateConList;

}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM