• Sparty
  • NEWBIE
  • 10 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 3
    Replies
Hi,

What I'm trying to do is be able to have leads automatically convert to contacts if there is an account in the system for that company. I think that's the best way to describe it. For example, if there is an account already in Salesforce for Comerica Bank and we recieve a lead from a webform submission that works at Comerica as well, I would like that lead to automatically convert to a contact in that scenario. Another example is if there is an account for Chevrolet in Salesforce but there are still leads in the system that work at Chevrolet we would like those leads to convert over as contacts as well since they match the company. Any help would be appreciated!

Thanks!
  • November 16, 2016
  • Like
  • 0
Hi,

I found a trigger on here that fits exactly what we're trying to do in our org. We would like to automatically add all contacts of a company to any existing opportunites as contact roles. This is the trigger I found on here from Joseph Dindinger on a old post. 

01trigger CreateContactRole on Opportunity (after insert, after update) {
02 
03    //get the id of all involved accounts
04    Set<ID> accountIds = new Set<ID>();
05    for(Opportunity opt:Trigger.New){
06        accountIds.add(opt.AccountId);
07    }
08     
09    //get all contacts for those accounts
10    list<Contact> contacts = new list<Contact>();
11    contacts = [select id, AccountId from Contact where AccountId in: accountIds order bycreateddate Limit 5000];
12     
13    //organize these contacts by account
14    Map<Id,List<Contact>> contactsByAccount = new Map<ID,List<Contact>>();
15    for(Contact c:contacts){
16        if(contactsByAccount.get(c.AccountId) == null){
17            contactsByAccount.put(c.AccountId,new List<Contact>());
18        }
19        contactsByAccount.get(c.AccountId).add(c);
20    }
21     
22    // check to see if the Opportunity already has a contact role.  If it does, add to a set of Ids to exclude
23    List<OpportunityContactRole> existingOCR = new List<OpportunityContactRole>();
24    Set<Id> existingOCRIds = new Set<Id>();
25    existingOCR = [select OpportunityId from OpportunityContactRole where OpportunityIdin:Trigger.newMap.keySet() limit 5000];
26    for(OpportunityContactRole ocr:existingOCR) if(!existingOCRIds.contains(ocr.OpportunityId)) existingOCRIds.add(ocr.OpportunityId);
27     
28    //create the OpportunityContactRole objects
29    list<OpportunityContactRole> lstOCR = new list<OpportunityContactRole>();
30    for(Opportunity opt:Trigger.New){
31        if(!existingOCRIds.contains(opt.Id) && contactsByAccount.get(opt.AccountId) != null){
32            Boolean firstContact = true;
33            for(Contact c: contactsByAccount.get(opt.AccountId)){
34                OpportunityContactRole ocr = new OpportunityContactRole(OpportunityId=opt.id,ContactId=c.id);
35                if(firstContact) {
36                    ocr.IsPrimary = TRUE;
37                    firstContact = FALSE;
38                }
39                lstOCR.add(ocr);
40            }
41        }
42    }
43    insert lstOCR;
44}

The trigger works perfectly in the sandbox but I need to create a test class for it and I'm having problems doing so. I'm very new to apex or any type of coding. I've searched around and looked at youtube videos on creating a test class but I can't get it right. Any help on creating a test class for the above trigger would be very much appreciated. 

Thanks
  • November 16, 2016
  • Like
  • 0
Hi,

I found a trigger on here that fits exactly what we're trying to do in our org. We would like to automatically add all contacts of a company to any existing opportunites as contact roles. This is the trigger I found on here from Joseph Dindinger on a old post. 

01trigger CreateContactRole on Opportunity (after insert, after update) {
02 
03    //get the id of all involved accounts
04    Set<ID> accountIds = new Set<ID>();
05    for(Opportunity opt:Trigger.New){
06        accountIds.add(opt.AccountId);
07    }
08     
09    //get all contacts for those accounts
10    list<Contact> contacts = new list<Contact>();
11    contacts = [select id, AccountId from Contact where AccountId in: accountIds order bycreateddate Limit 5000];
12     
13    //organize these contacts by account
14    Map<Id,List<Contact>> contactsByAccount = new Map<ID,List<Contact>>();
15    for(Contact c:contacts){
16        if(contactsByAccount.get(c.AccountId) == null){
17            contactsByAccount.put(c.AccountId,new List<Contact>());
18        }
19        contactsByAccount.get(c.AccountId).add(c);
20    }
21     
22    // check to see if the Opportunity already has a contact role.  If it does, add to a set of Ids to exclude
23    List<OpportunityContactRole> existingOCR = new List<OpportunityContactRole>();
24    Set<Id> existingOCRIds = new Set<Id>();
25    existingOCR = [select OpportunityId from OpportunityContactRole where OpportunityIdin:Trigger.newMap.keySet() limit 5000];
26    for(OpportunityContactRole ocr:existingOCR) if(!existingOCRIds.contains(ocr.OpportunityId)) existingOCRIds.add(ocr.OpportunityId);
27     
28    //create the OpportunityContactRole objects
29    list<OpportunityContactRole> lstOCR = new list<OpportunityContactRole>();
30    for(Opportunity opt:Trigger.New){
31        if(!existingOCRIds.contains(opt.Id) && contactsByAccount.get(opt.AccountId) != null){
32            Boolean firstContact = true;
33            for(Contact c: contactsByAccount.get(opt.AccountId)){
34                OpportunityContactRole ocr = new OpportunityContactRole(OpportunityId=opt.id,ContactId=c.id);
35                if(firstContact) {
36                    ocr.IsPrimary = TRUE;
37                    firstContact = FALSE;
38                }
39                lstOCR.add(ocr);
40            }
41        }
42    }
43    insert lstOCR;
44}

The trigger works perfectly in the sandbox but I need to create a test class for it and I'm having problems doing so. I'm very new to apex or any type of coding. I've searched around and looked at youtube videos on creating a test class but I can't get it right. Any help on creating a test class for the above trigger would be very much appreciated. 

Thanks
  • November 16, 2016
  • Like
  • 0

I'd like to have a program that would automatically connect all contacts associated with an account to an opportunity as contact roles. I already know that if you create an opportunity from a contact, that the contact is assigned as the contact role. However, I have multiple contacts associated with some of my accounts and I need them to be connected to the opportunity as well. I've created a flow to make opportunity entry easier for the office staff, however getting them to remember to go back and add the contact roles after they have created the opportunities is a pain. It would be so much nicer if this could be done behind the scenes with programing.  Is it possible to do this with a flow? I'm using the desktop flow developer, and I see you can make a data source/target with an opportunitycontactrole table. But I'm not sure how to go about adding this to my flow. I'm assuming I'd have to add another lookup element, have the person find the contact to attach, selected it and assign the role.  The problem with this, is that the operator, has to know who to add to the opportunity, which opens up a possbility of adding the wrong people. If there was a way to make it automatic, it would remove the potential for operator error.