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
RK BheemreddyRK Bheemreddy 

Trigger to clear Contact field on case when Supplied Email does not match the primary email of any Contact in Salesforce

Hi,

My requirement is to attach a Case to a Contact only when the Supplied email matches the Primary Email of a Contact in the system. Currently, Salesforce OOTB feature links a Case to a Contact if the Supplied Email matches any custom or standard Email on the Contact. I wrote a trigger to match a  User in the system to a custom lookup field. I have no idea how to achieve the Contact thing from here.

Any help is greatly appreciated.

Thanks,
RK

trigger EmailtoCaseTrigger on Case (before insert) {
    
    
    set<String> casebyEmail = new set<String>();
    map<string,string> userEmailIdMap = new map<string, string>();
    
    for(Case c: trigger.new){
        casebyEmail.add(c.SuppliedEmail);
    }
    
    List<User> matchingUsers = [Select Id, Name, Email from User where Email IN: casebyEmail limit 1000];
    
    for(User u: matchingUsers){
        userEmailIdMap.put(u.Email,u.Id);
    }
    
    for(Case c: Trigger.new){
        c.Requestor__c = userEmailIdMap.get(c.SuppliedEmail);
    }    
    }