+ Start a Discussion
Sai ManojSai Manoj 

Merge existing two before insert triggers.

Hi Experts,

Please find below two triggers, i need a help for merge these two triggers.

First Trigger:

trigger Case_SupportFirstSave on Case (before insert) {

    String strDefaultCaseProjectName = 'Support';
    String strDefaultCaseProjectID;
    String strRequestedUserID;

    for (Case objCase: Trigger.new) {
    
        If ((objCase.RecordTypeID == '012w00000006VBu') || (objCase.RecordTypeID == '012w00000006VBv')) {

            //Try to locate user record for person who logged the case
            list<User> objUsers = [select id, name from user where name = :objCase.SuppliedName and isActive = true limit 1];
            
            if (!objUsers.isEmpty()) { 
                objCase.Requested_By__c = objUsers[0].ID;
            }
            
            //If no project entered in by user; use support as default
            if (objCase.Project__c == null) { 
            
                //Locate Default Support Project by Name
                objCase.Project__c = [select id, name from SFDC_project__c where name = :strDefaultCaseProjectName limit 1].ID;
                
            }
                        
        }

    }
  

}


Second Trigger:

trigger AccountandContactUpdateTrigger on Case(before insert, before update){

    List<String> AccNoSet = new List<String>();
    
    for(Case c: trigger.new){
     AccNoSet.add(c.Account_Number__c);
    }
    
    Map<String, Account> AccMap  = new Map<String, Account>();

    for(Account acc : [SELECT Id, Parent_VRP_Number__c, VRP_Account_Number__c from Account where VRP_Account_Number__c IN :AccNoSet]){
     AccMap.put(acc.Parent_VRP_Number__c , acc);
    }
    

    
    for(Case c:trigger.new){
         c.Accountid = AccMap.get(c.Account_Number__c).id;
         
         if(c.accountid!=null){
         
         List<Contact> ConList = [select id, name from Contact where accountid =:c.accountid];
         
         if(!ConList.isempty()){
         c.Contactid = ConList[0].id;
         }
         }
    }
}

Thanks in advance.

Regards,
Sai