function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
mitsvikmitsvik 

How to validate my code enhancement when a optn condition is added to existing trigger?

I have been asked to add a optn see underline to the contact object, when a contact is created automatically from case using the following trigger.However, how do i validate this trigger? what should be the process to test this?


trigger AutocreateContactForCase on Case (before insert) {
    List<String> emailAddresses = new List<String>();
    for (Case caseObj:Trigger.new) {
        if (caseObj.ContactId==null && 
            caseObj.Origin == 'Contact Us Web Form' && 
            String.isNotBlank(caseObj.SuppliedEmail)){
                emailAddresses.add(caseObj.SuppliedEmail);
        }
    }
    List<Contact> listContacts = [Select Id,Email From Contact Where Email in :emailAddresses];
    Set<String> takenEmails = new Set<String>();
    for (Contact c:listContacts) {
        takenEmails.add(c.Email);
    }
    
    Map<String,Contact> emailToContactMap = new Map<String,Contact>();
    List<Case> casesToUpdate = new List<Case>();
    Account acc;
    try{
        acc = [Select Id from Account where Name = 'No Account' limit 1];
    }
    catch(Exception ex){system.debug('Error: '+ex.getMessage());}
        
    for (Case caseObj:Trigger.new){
    
        if (caseObj.ContactId==null && caseObj.Origin == 'Contact Us Web Form' && 
                         String.isNotBlank(caseObj.SuppliedName) &&
                             String.isNotBlank(caseObj.SuppliedEmail) &&
                                 !takenEmails.contains(caseObj.SuppliedEmail)){
                                    
            string firstName=''; string lastName='';
            list<string> lstName= caseObj.SuppliedName.split(' ');
            if(lstName!=null && lstName.size()>0){
                if(lstName.size()==1){
                    lastName=lstName[0];
                }else{
                    firstName=lstName[0];
                    for(integer i=1;i<=lstName.size()-1;i++){
                        lastName=lastName+' '+lstName[i];
                    }
                    lastName=lastName.Trim();
                }
                
            }
            Contact cont = new Contact(FirstName=firstName,LastName=lastName,
                                       Email=caseObj.SuppliedEmail,Phone = caseObj.SuppliedPhone
                                       ,AccountId = acc == null?null: acc.Id,Opt_Me_In_For_Future_Communications__c=true,Opt_Me_In_For_Market_Research__c=true,LeadSource='Contact Us Web Form'
                                       );
            emailToContactMap.put(caseObj.SuppliedEmail,cont);
            casesToUpdate.add(caseObj);
        }
        if (caseObj.ContactId==null && caseObj.Origin == 'Contact Us Web Form' &&
                    String.isNotBlank(caseObj.SuppliedName) && 
                    String.isNotBlank(caseObj.SuppliedEmail) &&
                    takenEmails.contains(caseObj.SuppliedEmail)){
                        caseObj.Found_Duplicate_Contacts__c = true;
           }
    }
    try{
        List<Contact> newContacts = emailToContactMap.values();
        system.debug('newContacts: '+newContacts);
        insert newContacts;
    }catch(Exception ex){system.debug('Error: '+ex.getMessage());}
    
    
    for (Case caseObj:casesToUpdate) {
        Contact newContact = emailToContactMap.get(caseObj.SuppliedEmail);        
        caseObj.ContactId = newContact.Id;
    }
}