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
John WorthJohn Worth 

Web to case trigger

The standard web to case logic to match to a contact is email; I'm not confident in the quality of the email addresses we'll get on our cases, and accounts are matched via the contact. We would like to use a custom field to associate a case with an account: NPI number. We can force that into the web to case HTML and we know it's a one-to-one match. I've never written a trigger and would like some guidance on the best way to appraoch this.
Best Answer chosen by John Worth
TechingCrewMattTechingCrewMatt
Hello, John. You'll want to create a handler class to handle the logic for the trigger.. It will look something like this:
 
public class CaseTriggerHandler {

    public static void assignCasesToAccounts(List<Case> cases){
        Set<String> npiNumbers = new Set<String>();
        for(Case newCase : cases){
            npiNumbers.add(newCase.NPI_Number__c);
        }
        Map<String, Id> accountIdsByNpiNumbers = new Map<String, Id>();
        for(Account account : [SELECT Id FROM Account WHERE NPI_Number__c IN :npiNumbers]){
            accountIdsByNpiNumbers.put(account.NPI_Number__c, account.Id);
        }       
        for(Case newCase : cases){
            newCase.AccountId = accountIdsByNpiNumbers.get(newCase.NPI_Number__c);
        }
    }
}

Then, your trigger can call that method as seen below:
trigger CaseTrigger2 on Case (before insert, after insert, before update, after update, before delete, after undelete) {
    if(Trigger.isInsert && Trigger.isBefore){
        CaseTriggerHandler.assignCasesToAccounts(Trigger.new);
    }
}

 

All Answers

TechingCrewMattTechingCrewMatt
Hello, John. You'll want to create a handler class to handle the logic for the trigger.. It will look something like this:
 
public class CaseTriggerHandler {

    public static void assignCasesToAccounts(List<Case> cases){
        Set<String> npiNumbers = new Set<String>();
        for(Case newCase : cases){
            npiNumbers.add(newCase.NPI_Number__c);
        }
        Map<String, Id> accountIdsByNpiNumbers = new Map<String, Id>();
        for(Account account : [SELECT Id FROM Account WHERE NPI_Number__c IN :npiNumbers]){
            accountIdsByNpiNumbers.put(account.NPI_Number__c, account.Id);
        }       
        for(Case newCase : cases){
            newCase.AccountId = accountIdsByNpiNumbers.get(newCase.NPI_Number__c);
        }
    }
}

Then, your trigger can call that method as seen below:
trigger CaseTrigger2 on Case (before insert, after insert, before update, after update, before delete, after undelete) {
    if(Trigger.isInsert && Trigger.isBefore){
        CaseTriggerHandler.assignCasesToAccounts(Trigger.new);
    }
}

 
This was selected as the best answer
John WorthJohn Worth
Sorry to be delayed in responding; thanks for your help in this!!