+ Start a Discussion
verynewbieverynewbie 

case trigger help

Hi experts

 

I just started working in a salesforce company as a developer.

This is my trigger.I have a trigger on case object which was written by my old developer,

 

http://wiki.developerforce.com/page/Autocreating_Contacts_From_Web_To_Case

This is what he used.

 

It works fine now.I should include new use case in this trigger where i have to also check with web phone number.

If email match is not found then i should try to find match on contact phone number.If more than one contact has the phone number, pick the one with the most recent created date and  associate the Case with that contact.

 

I dont understand how i can get this.Please help.!!!

 

Best Answer chosen by Admin (Salesforce Developers) 
Vadim RudkovVadim Rudkov

Just like a starting point:

trigger CaseAutocreateContact on Case (before insert) {
    List<String> emailAddresses = new List<String>();
    List<String> phones = new List<String>();
    //First exclude any cases where the contact is set
    for (Case caseObj:Trigger.new) {
        if (caseObj.ContactId==null) {
            if (caseObj.SuppliedEmail!='') {
                emailAddresses.add(caseObj.SuppliedEmail);
            }
            if (caseObj.SuppliedPhone!='') {
                phones.add(caseObj.SuppliedPhone);
            }
        }
    }

    //Now we have a nice list of all the email addresses.  Let's query on it and see how many contacts already exist.
    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, List<Contact>> phoneToContactMap = new Map<String, List<Contact>>();
    for (Contact c: [select MobilePhone from Contact where MobilePhone in :phones order by CreatedDate]) {
        if (!phoneToContactMap.containsKey(c.MobilePhone)) phoneToContactMap.put(c.MobilePhone, new List<Contact>());
        phoneToContactMap.get(c.MobilePhone).add(c);
    }

    Map<String,Contact> emailToContactMap = new Map<String,Contact>();
    List<Case> casesToUpdate = new List<Case>();

    for (Case caseObj:Trigger.new) {
        if (caseObj.ContactId==null &&
            caseObj.SuppliedName!=null &&
            caseObj.SuppliedEmail!=null &&
            caseObj.SuppliedName!='' &&
            !caseObj.SuppliedName.contains('@') &&
            caseObj.SuppliedEmail!='' &&
            !takenEmails.contains(caseObj.SuppliedEmail))
        {

            if (phoneToContactMap.containsKey(caseObj.SuppliedPhone)) {
                caseObj.ContactId = phoneToContactMap.get(caseObj.SuppliedPhone)[0].Id;
                continue;
            }


            //The case was created with a null contact
            //Let's make a contact for it
            String[] nameParts = caseObj.SuppliedName.split(' ',2);
            if (nameParts.size() == 2)
            {
                Contact cont = new Contact(FirstName=nameParts[0],
                                            LastName=nameParts[1],
                                            Email=caseObj.SuppliedEmail,
                                            Autocreated__c=true);
                emailToContactMap.put(caseObj.SuppliedEmail,cont);
                casesToUpdate.add(caseObj);
            }
        }
    }
    
    List<Contact> newContacts = emailToContactMap.values();
    insert newContacts;
    
    for (Case caseObj:casesToUpdate) {
        Contact newContact = emailToContactMap.get(caseObj.SuppliedEmail);
        
        caseObj.ContactId = newContact.Id;
    }
}

 

All Answers

Vadim RudkovVadim Rudkov

Just like a starting point:

trigger CaseAutocreateContact on Case (before insert) {
    List<String> emailAddresses = new List<String>();
    List<String> phones = new List<String>();
    //First exclude any cases where the contact is set
    for (Case caseObj:Trigger.new) {
        if (caseObj.ContactId==null) {
            if (caseObj.SuppliedEmail!='') {
                emailAddresses.add(caseObj.SuppliedEmail);
            }
            if (caseObj.SuppliedPhone!='') {
                phones.add(caseObj.SuppliedPhone);
            }
        }
    }

    //Now we have a nice list of all the email addresses.  Let's query on it and see how many contacts already exist.
    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, List<Contact>> phoneToContactMap = new Map<String, List<Contact>>();
    for (Contact c: [select MobilePhone from Contact where MobilePhone in :phones order by CreatedDate]) {
        if (!phoneToContactMap.containsKey(c.MobilePhone)) phoneToContactMap.put(c.MobilePhone, new List<Contact>());
        phoneToContactMap.get(c.MobilePhone).add(c);
    }

    Map<String,Contact> emailToContactMap = new Map<String,Contact>();
    List<Case> casesToUpdate = new List<Case>();

    for (Case caseObj:Trigger.new) {
        if (caseObj.ContactId==null &&
            caseObj.SuppliedName!=null &&
            caseObj.SuppliedEmail!=null &&
            caseObj.SuppliedName!='' &&
            !caseObj.SuppliedName.contains('@') &&
            caseObj.SuppliedEmail!='' &&
            !takenEmails.contains(caseObj.SuppliedEmail))
        {

            if (phoneToContactMap.containsKey(caseObj.SuppliedPhone)) {
                caseObj.ContactId = phoneToContactMap.get(caseObj.SuppliedPhone)[0].Id;
                continue;
            }


            //The case was created with a null contact
            //Let's make a contact for it
            String[] nameParts = caseObj.SuppliedName.split(' ',2);
            if (nameParts.size() == 2)
            {
                Contact cont = new Contact(FirstName=nameParts[0],
                                            LastName=nameParts[1],
                                            Email=caseObj.SuppliedEmail,
                                            Autocreated__c=true);
                emailToContactMap.put(caseObj.SuppliedEmail,cont);
                casesToUpdate.add(caseObj);
            }
        }
    }
    
    List<Contact> newContacts = emailToContactMap.values();
    insert newContacts;
    
    for (Case caseObj:casesToUpdate) {
        Contact newContact = emailToContactMap.get(caseObj.SuppliedEmail);
        
        caseObj.ContactId = newContact.Id;
    }
}

 

This was selected as the best answer
verynewbieverynewbie

Thanks but It didnt work..
It is not even creating contact name.It is showing null in salesforce.
It is NOT linking with contact which has same phone number as supplied phone:(

It is working with Email address only:(

 

Please help  ... I need tis ASAP

Vadim RudkovVadim Rudkov

"If email match is not found then i should try to find match on contact phone number.If more than one contact has the phone number, pick the one with the most recent created date and  associate the Case with that contact."

"It is linking with contact which has same phone number as supplied phone:("

Wasn't it exactly that you were looking for?
asish1989asish1989

HI

I am sharing some code ,hope It will help you.

I have created two custom field SuppliedEmail__c and SuppliedPhone__c ,

when you create a case you will give value to suppliedEmail and suppliedEmail, then It will check in Contact whether that email exists or not,If exists then case will be created under that contact record.

 

If there is no matching records founnd with respect to SuppliedEmail, then It will check w.r.t. SuppliedPhone, If found case will be created in most recent record.

trigger CaseAutocreateContactcheckingWithEmailAndPhone on Case (before insert, before update) {
    List<String> emailAddresses = new List<String>();
    List<String> phones = new List<String>();
    //First exclude any cases where the contact is set
    for (Case caseObj:Trigger.new) {
        if (caseObj.ContactId==null) {
            if (caseObj.SuppliedEmail__c!='') {
                emailAddresses.add(caseObj.SuppliedEmail__c);
            }
            if (caseObj.SuppliedPhone__c!='') {
                phones.add(caseObj.SuppliedPhone__c);
            }
        }
        if(emailAddresses !=null && emailAddresses.size() >0){
            for(contact c : [Select Id,Email From Contact Where Email in :emailAddresses]){
                caseObj.ContactId = c.id;             
            }
        }   
        else{
            if(phones!=null && phones.size()>0){
                for(contact c : [select MobilePhone from Contact where MobilePhone in :phones order  by CreatedDate]){
                    caseObj.ContactId = c.id;
              
                }
            }
        }   
    
    }   
}   
    

 

 

verynewbieverynewbie
Sorry sir.
I missed not.
It is : It is not linking with contact which has same phone number as supplied phone:(

Sorry sir
Vadim RudkovVadim Rudkov

I've created a simple test and it pass successfully:

public class CaseAutocreateContactTest {
      public static testMethod void testMethod_1() {
          Contact contact = new Contact(
            FirstName = 'John',
            LastName = 'Doe',
            MobilePhone='123123123');

          insert contact;

          Case cs = new Case(
            SuppliedPhone='123123123',
            SuppliedEmail='jdoe_test_test@doe.com',
            SuppliedName='John Doe',
            Subject='Feedback - Something');

          insert cs;
          Case check = [select ContactId from Case where Id = :cs.Id];

          System.assertEquals(contact.Id, check.ContactId);
      }
}

 It's supposed that there is a contact with MobilePhone and new case with non-empty fields SuppliedName, SuppliedEmail and SuppliedPhone will be linked to that contact by the MobilePhone. I think the problem is in the if statement wich I took as is from the link you provided:

        if (caseObj.ContactId==null &&
            caseObj.SuppliedName!=null &&
            caseObj.SuppliedEmail!=null &&
            caseObj.SuppliedName!='' &&
            !caseObj.SuppliedName.contains('@') &&
            caseObj.SuppliedEmail!='' &&
            !takenEmails.contains(caseObj.SuppliedEmail))
        {

You should rewrite this statement for your needs.

verynewbieverynewbie

@Asish Kumar: Thanks sir..I tired this.It worked well for email but its not working for phone number.

Chris KuehnelChris Kuehnel
So what is the point of auto filling in 'John Doe' when I need it to create a contact based off the Case information... all this does is create a Contact with John Doe, I'd be better off creating the Contact manually lol. 

If I remove the class and just have the trigger, it does nothing, if I change the class to reflect something like 'SuppliedEmail=SuppliedEmail' then that does not work and tells me I need to have a ' ' involved in it which would require me to have a predefined email which is not what we are trying to do here. I know this is a 3 year old thread but it doesn't make sense to have the class auto creating cases with predefined inforamtion.

Any help would be greatly appreciated.