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
bouscalbouscal 

Web-to-Case Contact matching

Since web-to-case will only attempt to validate an email address and then will only accept a single match I need to create a way to match on a custom field, username__c.  This value is captured on the web-to-case form and is also present on the Contact object.  Here's what I have so far but the compiler is yelling at me for using Case instead of sObject.  If I use sObject instead of Case I cannot directly stipulate the field I want to match.  Where have I gone wrong?
 
public class CaseAssociationByUserId {

    public void AssociateCases(Case[] cases){
        // get ids of incoming cases
        Set<String> userids = new Set<String>();
        Map<String,Id> CaseMap = new Map<String, Id>();
        // loop through Cases and create map and array of usernames
        for(Case c : Trigger.new){
            userids.add(c.Username__c);
            CaseMap.put(c.Username__c, c.Id); 
        }
        List<Contact> con = [SELECT id, username__c FROM Contact WHERE username__c IN : userids];
        Map<String, Id> ContactMap = new Map<String, Id>();
        // create map of contact usernames to ids
        for(Contact co : con){
            ContactMap.put(co.username__c, co.id);
        }
        List<Case> toUpdate = new List<Case>();
        // loop through cases again and populate the contact for Cases where the Contact is blank
        for(Case c : Trigger.new){
            if(c.ContactId==null || c.ContactId==''){
            	// find the username and add to the toUpdate list
            	c.contactid=ContactMap.get(c.Username__c);
        	}
        }
        update toUpdate;
    }
}

 
ShashankShashank (Salesforce Developers) 
Can you may be post the exact error message?