+ Start a Discussion
Stancioiu Stefan 1Stancioiu Stefan 1 

Hi guys.I'm new on salesforce. Anybody can help me to write this trigger?

1. Create a custom field on the Contact object: Is Primary Contact(checkbox)
2. Create a custom field on the Contact object: Is Primary Contact(checkbox);
3. A validation should be added so that if the Account already has a Primary Contact set a new one cannot be added;​
4. When a contact is set as primary, the Primary Contact Phone should be updated to all Contacts related to the same account. This should be an asynchronous process. Make sure that if one Contact update fails, it doesn’t rollback the changes for the others.
Suraj TripathiSuraj Tripathi

Hi Stancioiu,
Please try this piece of code. Hope it will help you.
Trigger

trigger PrimaryCont on Contact (before insert,after Update) {
    if(Trigger.IsInsert && Trigger.IsBefore){
        UpdateprimaryContact.testInsert(Trigger.new);
    }else if(Trigger.IsUpdate && Trigger.IsAfter){
      //  UpdateprimaryContact.testInsert(Trigger.new);
        UpdateprimaryContact.testUpdate(Trigger.new,Trigger.oldMap);
    }
}
Handler
public class UpdateprimaryContact {
    public static void testInsert(List<Contact> conlist){
        set<Id> AccId = new set<Id>();
        for(Contact con : conlist){
            if(con.AccountId != null){
                AccId.add(con.AccountId);
            }
        }
        if(AccId.size()>0){
            List<Contact> cnNew = new List<Contact>();
            cnNew = [select Is_Primary_Contact__c,AccountId from Contact where AccountId =: AccId];
            if(cnNew.size()>0){
                for(Contact con : conlist){
                    for(Contact conN : cnNew){
                        if(con.AccountId == conN.AccountId){
                            if(conN.Is_Primary_Contact__c==true){
                                con.Is_Primary_Contact__c.addError('This account has already primary contact!');
                            }
                        }
                    }
                }   
            }
        }
    }
    
    public static void testUpdate(List<Contact> conlist, Map<Id, Contact> mapList){
        System.debug('_--------------------------------------'+conlist);
        map<id,id> ContAccountId = new map<id,id>();
        set<Id> AccId = new set<Id>();
        
        for(Contact cnAc : conlist){
            if(cnAc.AccountId!=null && cnAc.Is_Primary_Contact__c == true && mapList.get(cnAc.Id).Is_Primary_Contact__c == false){
                ContAccountId.put(cnAc.id,cnAc.AccountId);
                AccId.add(cnAc.AccountId);
            }
        }
        if(AccId.size()>0){
            System.debug('_--------------------------------------'+AccId);
            List<Contact> cnNew = new List<Contact>();
            cnNew = [select id,LastName,Phone,AccountId, Is_Primary_Contact__c from Contact where AccountId =: AccId];
            
            List<Contact> cnUpdate = new List<Contact>(); 
             
            
            for(Contact cn : conlist){
                if(cn.Is_Primary_Contact__c == true){
                    System.debug('_---------primary-----------------------------'+cn);
                    if(ContAccountId.ContainsKey(cn.Id)){
                        for(Contact cnnn : cnNew){
                            if(cnnn.AccountId == ContAccountId.get(cn.Id)){ 
                                Contact cndoub = new Contact();
                                cndoub.id = cnnn.id; 
                                cndoub.Phone = cn.Phone;
                                cnUpdate.add(cndoub)  ;                            
                                }  
                        }
                    } 
                }
            }
            System.debug('_---------last;-----------------------------'+cnUpdate);
            if(cnUpdate.size()>0){
                update cnUpdate;   
            } 
        }
    }
}


If this code helps you. Please mark this as best.

Regards,
Suraj