I am trying to create a trigger that will automatically update contact on case depending on certain criteria.

Let me explain more.

I have cases that come from different domains and because of that i have person accounts that are identical both have same name and same email address. The only thing different is that they have a different domain name. So when I receive a case from that customer with same name and same email address the system confuses and doesn't add the case to that specific customer with that domain. So what i am trying to do is the following.

When i receive a case the case automatically assigns the domain to that case depending to which email the customer sends. So on the case i have a Domain_Name__c and accordingly on the account i have that domain_name__pc as well.


When a customer sends an email that in salesforce it is registered with 2 person accounts with different domains. I want the web email to check if the contact email matches if it does do nothing but if it doesn't check if that email exists and if it does check the domain on the case to match the domain on the account and then update the case with that contact information.

This is what i did up until know and i need some assistance if possible. Code below:

trigger updateContactonCase on Case (before update) {
    for(case c:Trigger.New){
        if(c.SuppliedEmail != c.ContactEmail){
            List<Account> acc = [select id,Domain_Name__pc from Account];
            for(Account a : acc){
              if(c.SuppliedEmail == a.PersonEmail){            
                if(c.Domain_Name__c == a.Domain_Name__pc){
                    c.ContactId = a.id;

What the code does: So the code needs to check if webemail != contact email on case. If that is not matched it should get all accounts from the query and see if that webemail is matching some of the PersonEmail. It should detect two at least and then needs to check the case domain to match the domain on that account. if both steps are satisfied update the contact name with the id of that account.

i also kinda need a test class for this because i am really bad with tests.

closed as off-topic by David Reed, Raul, Pranay Jaiswal, battery.cord, glls Dec 12 at 19:32

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions on problems in code you've written must describe the specific problem and include valid code to reproduce it. For help writing short, self-contained syntactically-valid examples, see: SSCCE.org" – David Reed, Raul, Pranay Jaiswal, battery.cord, glls
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 3
    Darko, when you say you need assistance, what specific issue are you struggling with? What does the code do now? What error do you see, if any? If the behavior is not what you expect, could you give a description of what's going wrong? – David Reed Dec 6 at 15:19
  • @DavidReed you are right I am sorry I updated the code with my thoughts and also gave more information about what i need to achieve. – Darko Todorovski Dec 6 at 15:27
  • I think what david meant was for you to actually describe what problem in your code you are facing, rather than updating the requirements you have and what you would like from the community (as is, it basically looks like you are expecting to be spoonfed with a working code sample). – glls Dec 6 at 15:50
  • Yup, exactly. Remember, we usually can't run your code in our orgs to see what it does. Give us a summary of what your code currently does and what aspects of your requirements you're stuck on, and we can probably help you out. – David Reed Dec 6 at 15:59