Thursday, July 27, 2017

"Who knows who" relations in Salesforce Contact and Accounts - part 2: Solution description

"Who Knows Who" - insert, update and delete reverse records

"Who Knows Who" custom object has Master-Detail relation with "reverse" record which inserts after insertion first record. When "Who Knows Who" record inserted, fires trigger which inserts "reverse" record:
 if(trigger.IsInsert){        
      for(Linked_Relationship__c l: trigger.new){
          if(l.Master_Relationship__c == null && l.Firm__c != l.Related_To_Firm__c){
               Linked_Relationship__c lc = new Linked_Relationship__c();
               lc.Firm__c =  l.Related_To_Firm__c;
               lc.Contact__c =    l.Related_To_Contact__c;
               lc.Related_To_Contact__c = l.Contact__c;
               lc.Related_To_Firm__c = l.Firm__c;
               lc.Reverse_Type__c =   l.Type__c;
               lc.Type__c = l.Reverse_Type__c;
               lc.Comments__c = l.Comments__c;
               lc.Master_Relationship__c = l.id;
               lc.Reverse_Contact_Relationship__c = l.Contact_Relationship__c; 
               lc.Contact_Relationship__c = l.Reverse_Contact_Relationship__c;
               toInsert.Add(lc); 
           }            
       }                                                    
   }
After "Who Knows Who" record update "reverse" record also will be updated (no difference - master or detail):
if(trigger.IsUpdate){        
     for(Linked_Relationship__c lr : trigger.new){
         if(lr.Master_Relationship__c == null){
              lrIds.Add(lr.Id);
         }
         else{
              masterIds.Add(lr.Master_Relationship__c);
         }
     }
     List updateList = [SELECT Id, Firm__c, Contact__c, Related_To_Contact__c, Related_To_Firm__c,
                                                  Reverse_Type__c, Type__c, Comments__c, Master_Relationship__c, 
                                                  Reverse_Contact_Relationship__c, Contact_Relationship__c
                                                  FROM Linked_Relationship__c WHERE
                                                  Master_Relationship__c IN: lrIds OR Id IN: masterIds];
    for(Linked_Relationship__c l : updateList ){
       if(l.Master_Relationship__c != null){
            lrMap.put(l.Master_Relationship__c, l);
       }
       else{
            lrMap.put(l.Id, l);  
       }
   }

   for(Linked_Relationship__c lr : trigger.new){
        if(lrMap.containskey(lr.Id)) {
            if (!(lrMap.get(lr.Id).Firm__c==lr.Related_To_Firm__c&&lrMap.get(lr.Id).Related_To_Firm__c==lr.Firm__c&&lrMap.get(lr.Id).Related_To_Contact__c==lr.Contact__c&&lrMap.get(lr.Id).Contact__c==lr.Related_To_Contact__c&&lrMap.get(lr.Id).Type__c==lr.Reverse_Type__c&&lrMap.get(lr.Id).Reverse_Type__c==lr.Type__c&&lrMap.get(lr.Id).Comments__c==lr.Comments__c&&lrMap.get(lr.Id).Reverse_Contact_Relationship__c == lr.Contact_Relationship__c&&lrMap.get(lr.Id).Contact_Relationship__c == lr.Reverse_Contact_Relationship__c )){
                lrMap.get(lr.Id).Firm__c =  lr.Related_To_Firm__c;
                lrMap.get(lr.Id).Contact__c = lr.Related_To_Contact__c;
                lrMap.get(lr.Id).Related_To_Contact__c = lr.Contact__c;
                lrMap.get(lr.Id).Related_To_Firm__c = lr.Firm__c;
                lrMap.get(lr.Id).Reverse_Type__c =   lr.Type__c;
                lrMap.get(lr.Id).Type__c = lr.Reverse_Type__c;
                lrMap.get(lr.Id).Comments__c = lr.Comments__c;
                lrMap.get(lr.Id).Reverse_Contact_Relationship__c = lr.Contact_Relationship__c; 
                lrMap.get(lr.Id).Contact_Relationship__c = lr.Reverse_Contact_Relationship__c;
                toInsert.Add(lrMap.get(lr.Id));
            }   
        }
       if(lrMap.containskey(lr.Master_Relationship__c)){ 
           if(!(lrMap.get(lr.Master_Relationship__c).Firm__c==lr.Related_To_Firm__c&&lrMap.get(lr.Master_Relationship__c).Related_To_Firm__c==lr.Firm__c&&lrMap.get(lr.Master_Relationship__c).Related_To_Contact__c==lr.Contact__c&&lrMap.get(lr.Master_Relationship__c).Contact__c==lr.Related_To_Contact__c&&lrMap.get(lr.Master_Relationship__c).Type__c==lr.Reverse_Type__c&&lrMap.get(lr.Master_Relationship__c).Reverse_Type__c==lr.Type__c&&lrMap.get(lr.Master_Relationship__c).Comments__c==lr.Comments__c&&lrMap.get(lr.Master_Relationship__c).Reverse_Contact_Relationship__c == lr.Contact_Relationship__c&&lrMap.get(lr.Master_Relationship__c).Contact_Relationship__c == lr.Reverse_Contact_Relationship__c )){
                lrMap.get(lr.Master_Relationship__c).Firm__c =  lr.Related_To_Firm__c;
                lrMap.get(lr.Master_Relationship__c).Contact__c = lr.Related_To_Contact__c;
                lrMap.get(lr.Master_Relationship__c).Related_To_Contact__c = lr.Contact__c;
                lrMap.get(lr.Master_Relationship__c).Related_To_Firm__c = lr.Firm__c;
                lrMap.get(lr.Master_Relationship__c).Reverse_Type__c =   lr.Type__c;
                lrMap.get(lr.Master_Relationship__c).Type__c = lr.Reverse_Type__c;
                lrMap.get(lr.Master_Relationship__c).Comments__c = lr.Comments__c;
                lrMap.get(lr.Master_Relationship__c).Reverse_Contact_Relationship__c = lr.Contact_Relationship__c; 
                lrMap.get(lr.Master_Relationship__c).Contact_Relationship__c = lr.Reverse_Contact_Relationship__c;
                toInsert.Add(lrMap.get(lr.Master_Relationship__c));
           }       
       }                             
   }
}
If "Who Knows Who" record deleted, "reverse" record also will be deleted:
for(Linked_Relationship__c lr: trigger.old){                          
              lrMap.put(lr.Firm__c, lr);
        }
List lrList = [SELECT Id, Firm__c, Contact__c, Related_To_Contact__c, Related_To_Firm__c,
                                               Reverse_Type__c, Type__c, Comments__c,  Master_Relationship__c,
                                               Reverse_Contact_Relationship__c, Contact_Relationship__c
                                               FROM Linked_Relationship__c WHERE
                                               Related_To_Firm__c IN: lrMap.keySet() ];
for(Linked_Relationship__c l : lrList )  {
    if(lrMap.get(l.Related_To_Firm__c).Firm__c==l.Related_To_Firm__c&&lrMap.get(l.Related_To_Firm__c).Related_To_Firm__c==l.Firm__c&&lrMap.get(l.Related_To_Firm__c).Related_To_Contact__c==l.Contact__c && lrMap.get(l.Related_To_Firm__c).Contact__c==l.Related_To_Contact__c&&lrMap.get(l.Related_To_Firm__c).Reverse_Type__c==l.Type__c&&lrMap.get(l.Related_To_Firm__c).Type__c==l.Reverse_Type__c&&lrMap.get(l.Related_To_Firm__c).Contact_Relationship__c==l.Reverse_Contact_Relationship__c&&lrMap.get(l.Related_To_Firm__c).Reverse_Contact_Relationship__c==l.Contact_Relationship__c){
          toDelete.Add(l);
    }
}                                                                                                              
delete toDelete;

Trigger on Contact

When Contact record inserted, trigger insert "Who Knows Who" records("User knows Contact" scenario).
for(Contact c: trigger.new){                
     Linked_Relationship__c l = new Linked_Relationship__c();
     l.Firm__c = knowContList[0].AccountId;
     l.Contact__c = knowContList[0].Id;
     l.Related_To_Firm__c = c.AccountId;
     l.Related_To_Contact__c = c.Id;
     toInsert.Add(l);                       
   }                       
}
After insertion of the first record, trigger on "Who Knows Who" inserts second - reverse record.
After update of Contact - change Contact's Account, triggers updates related "Who Knows Who" records.
for(Contact c: trigger.new){
     if(c.AccountId != trigger.oldMap.get(c.Id).AccountId ){
        contIds.Add(c.id);
        Linked_Relationship__c l = new Linked_Relationship__c();
        l.Firm__c = c.AccountId;
        l.Related_To_Firm__c = c.AccountId;
        l.Related_To_Contact__c = c.Id;
        l.Reverse_Contact_Relationship__c = 'Employee';
        l.Type__c = 'Employer';
        toInsert.Add(l); 
    }
}
List l_oldList = [SELECT Id, Firm__c, Type__c, Comments__c, Contact_Relationship__c,
                                                      Related_To_Firm__c, Related_To_Contact__c, Reverse_Type__c, 
                                                      Reverse_Contact_Relationship__c, Contact__c FROM Linked_Relationship__c 
                                                      WHERE Related_To_Contact__c IN: contIds ];                                                                                                 

Map> lrMap = new Map>();
List otherList = new List();

for(Linked_Relationship__c lr : l_oldList ){
    if(lr.Reverse_Contact_Relationship__c != null && lr.Reverse_Contact_Relationship__c.contains('Employee')){
        if(lrMap.containskey(lr.Related_To_Contact__c)){
            lrMap.get(lr.Related_To_Contact__c).Add(lr);
        }
        else{
            lrMap.put(lr.Related_To_Contact__c, new List{lr});
        } 
     }
     else{
        otherList.Add(lr);
     }                       
}
for(Contact c: [SELECT Id, AccountId, Account.Name FROM Contact WHERE Id IN:  trigger.new]){
                 Linked_Relationship__c l_new = new Linked_Relationship__c();
                 if(!lrMap.containskey(c.Id)){
                     l_new.Firm__c = c.AccountId;
                     l_new.Reverse_Type__c = 'Former Employer';
                     l_new.Contact__c = c.Id;
                     l_new.Contact_Relationship__c = 'Former Employee';
                     l_new.Related_To_Firm__c = trigger.oldMap.get(c.Id).AccountId;
                     l_new.Comments__c = string.valueof(date.today()) + ' - ' + c.Account.Name ;
                     toInsert.Add(l_new);
                 }
                 else{
                     for(Linked_Relationship__c l_old : lrMap.get(c.Id)){
                         l_old.Type__c = 'Former Employer';                        
                         l_old.Reverse_Contact_Relationship__c = 'Former Employee';                     
                         l_old.Comments__c = string.valueof(date.today())+ ' - ' + c.Account.Name;                          
                         l_old.Related_To_Firm__c = c.AccountId;
                         l_old.Related_To_Contact__c = c.Id;                                                                                                                                                          
                         toInsert.Add(l_old);                         
                     }
                }
               for(Linked_Relationship__c l_other : otherList ){
                    if(l_other.Contact__c == c.Id){
                        l_other.Firm__c = c.AccountId;
                        l_other.Contact__c = c.Id;
                        toInsert.Add(l_other);
                    }
               }

If Contact deleted, all related "Who Knows Who" records also will be deleted.
for(Contact c: trigger.old){
     contIds.Add(c.Id);
}
List lrList = [SELECT Id, Related_To_Contact__c, Contact__c 
        FROM Linked_Relationship__c
        WHERE Contact__c IN: contIds OR Related_To_Contact__c IN: contIds];

if(lrList.size()>0){        
     delete lrList;
}

"Who knows who" relations for Salesforce Contact and Accounts - part 1: How this works

Sometimes we need to store information about Contact-Contact, Account-Contact, Account-Account relations:
  • User knows Contact and Account
  • Contact is employee of a Company;
  • Account is Employer of a Contact;
  • Contact is former employee of a Company;
  • Account is former Employer of a Contact, etc.
For this purpose we used Custom object "Who Knows Who":

The records can be created manually and automatic. After insert "Who Knows Who" record fires trigger which inserts "reverse" record.
Automatic scenarios
User "knows" Account and created Contact
When new Contact created, new "Who Knows Who" record is inserted:
Contact related lists:
This Contact and Company related to User:
The user who creates this Contact "knows" it and Contact's Company (reverse record).
"Known" Contact("User" field - lookup to User):

Employer - Former Employer
When Contact reassigned to other Account, insert 2 "Who Know Who" records - "Employer" and "Former Employer": Account is Former Employer of Contact:
In left column information about Contact: current Contact's Company(Account field), and date of termination in previous company. Reverse record: In this way, information about Account's former employees stores in "Who Knows Who" related list. The account is the employer of Contact: Current Account stores info about former employer of Contact in "Who Knows Who" related list:
Manually scenarios
If you want to create a relation between some Contacts or Account, you can manually to insert "Who Knows Who" record and select kind of relationship in "Account relationship" and "Contact relationship" picklists.