• DY Ray
  • NEWBIE
  • 0 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 3
    Replies
Hi, I'm trying to implement auto creation trigger of contacts for all incoming email to case emails. It should auto create the contact if the contact is not in our system.

The auto creation features works in sandbox and production. However, our agents are not allowed to create a new case in production. Does anyone know why I am getting this error? Thank you for your help!

Here is the error message when creating a new case:
Error Message when creating a new case

Here is the code we used:
trigger TriggertoCreateContactformCase on Case (before insert) {
    List<String> UseremailAddresses = new List<String>();
    //First exclude any cases where the contact is set
    for (Case c:Trigger.new) {
        if (c.ContactId==null &&
            c.SuppliedEmail!=''|| c.SuppliedEmail==null)
        {
            UseremailAddresses.add(c.SuppliedEmail);
        }
    }

    //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> listofallContacts = [Select Id,Email From Contact Where Email in:UseremailAddresses];
    Set<String> ExstingEmails = new Set<String>();
    for (Contact c:listofallContacts) {
        ExstingEmails.add(c.Email);
    }
    
    Map<String,Contact> emailToContactMap = new Map<String,Contact>();
    List<Case> casesToUpdate = new List<Case>();

    for (Case c:Trigger.new) {
        if (c.ContactId==null &&
            c.SuppliedName!=null &&
            c.SuppliedEmail!=null &&
            c.SuppliedName!='' &&
           !c.SuppliedName.contains('@') &&
            c.SuppliedEmail!='' &&
           !ExstingEmails.contains(c.SuppliedEmail))
        {
            //The case was created with a null contact
            //Let's make a contact for it
            String[] Emailheader = c.SuppliedName.split(' ',2);
            if (Emailheader.size() == 2)
            {
                Contact conts = new Contact(FirstName=Emailheader[0],
                                            LastName=Emailheader[1],
                                            Email=c.SuppliedEmail
                                            );
                emailToContactMap.put(c.SuppliedEmail,conts);
                casesToUpdate.add(c);
            }
        }
    }
    
    List<Contact> newContacts = emailToContactMap.values();
    insert newContacts;
    
    for (Case c:casesToUpdate) {
        Contact newContact = emailToContactMap.get(c.SuppliedEmail);
        
        c.ContactId = newContact.Id;
    }
}

Here is the Apex class we used:
 
@isTest
public class Test_CreateContactFormCase {
    
     static TestMethod  void createData()
     {
           contact c = new contact(FirstName='test',LastName='contact94',Email='abctest@123.com'); 
           insert c;
            case cse1 = new case(subject='Test',suppliedEmail='abctest@123.com',suppliedName='TestUser1');
            insert cse1;
            case cse2 = new case(subject='Test',suppliedEmail='abctest94@123.com',suppliedName='Test User2');
            insert cse2;
     }       
    

}

 
  • August 19, 2019
  • Like
  • 0
Does anyone know how to create a validation rule to block viewing of cases if the case owner is a queue?

I would like to prevent cherry picking of cases before cases are assigned to agents.

Thank you in advance!

Hi all,

I'm having trouble autopopulating the account field when we select a contact based on lookup.
Currently, the user would have to lookup both the contact and the account fields. We would like
to just look up the contact field and it will autopopulate the Account lookup field with that contact
account. This would be used on the Orders section.

Would the following Apex Trigger work? 

trigger IAR_trigger_Contact on Contact (before insert)
{
    IAR_handler_Contact handler = new IAR_handler_Contact();
    if(trigger.isInsert && trigger.isBefore && !IAR_handler_Contact.isCreatingAccountForContact)
    {
        handler.CreateAccountForContact(trigger.new);
    }

}

Thank you!

  • March 28, 2016
  • Like
  • 0
Hi, I'm trying to implement auto creation trigger of contacts for all incoming email to case emails. It should auto create the contact if the contact is not in our system.

The auto creation features works in sandbox and production. However, our agents are not allowed to create a new case in production. Does anyone know why I am getting this error? Thank you for your help!

Here is the error message when creating a new case:
Error Message when creating a new case

Here is the code we used:
trigger TriggertoCreateContactformCase on Case (before insert) {
    List<String> UseremailAddresses = new List<String>();
    //First exclude any cases where the contact is set
    for (Case c:Trigger.new) {
        if (c.ContactId==null &&
            c.SuppliedEmail!=''|| c.SuppliedEmail==null)
        {
            UseremailAddresses.add(c.SuppliedEmail);
        }
    }

    //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> listofallContacts = [Select Id,Email From Contact Where Email in:UseremailAddresses];
    Set<String> ExstingEmails = new Set<String>();
    for (Contact c:listofallContacts) {
        ExstingEmails.add(c.Email);
    }
    
    Map<String,Contact> emailToContactMap = new Map<String,Contact>();
    List<Case> casesToUpdate = new List<Case>();

    for (Case c:Trigger.new) {
        if (c.ContactId==null &&
            c.SuppliedName!=null &&
            c.SuppliedEmail!=null &&
            c.SuppliedName!='' &&
           !c.SuppliedName.contains('@') &&
            c.SuppliedEmail!='' &&
           !ExstingEmails.contains(c.SuppliedEmail))
        {
            //The case was created with a null contact
            //Let's make a contact for it
            String[] Emailheader = c.SuppliedName.split(' ',2);
            if (Emailheader.size() == 2)
            {
                Contact conts = new Contact(FirstName=Emailheader[0],
                                            LastName=Emailheader[1],
                                            Email=c.SuppliedEmail
                                            );
                emailToContactMap.put(c.SuppliedEmail,conts);
                casesToUpdate.add(c);
            }
        }
    }
    
    List<Contact> newContacts = emailToContactMap.values();
    insert newContacts;
    
    for (Case c:casesToUpdate) {
        Contact newContact = emailToContactMap.get(c.SuppliedEmail);
        
        c.ContactId = newContact.Id;
    }
}

Here is the Apex class we used:
 
@isTest
public class Test_CreateContactFormCase {
    
     static TestMethod  void createData()
     {
           contact c = new contact(FirstName='test',LastName='contact94',Email='abctest@123.com'); 
           insert c;
            case cse1 = new case(subject='Test',suppliedEmail='abctest@123.com',suppliedName='TestUser1');
            insert cse1;
            case cse2 = new case(subject='Test',suppliedEmail='abctest94@123.com',suppliedName='Test User2');
            insert cse2;
     }       
    

}

 
  • August 19, 2019
  • Like
  • 0

Hi all,

I'm having trouble autopopulating the account field when we select a contact based on lookup.
Currently, the user would have to lookup both the contact and the account fields. We would like
to just look up the contact field and it will autopopulate the Account lookup field with that contact
account. This would be used on the Orders section.

Would the following Apex Trigger work? 

trigger IAR_trigger_Contact on Contact (before insert)
{
    IAR_handler_Contact handler = new IAR_handler_Contact();
    if(trigger.isInsert && trigger.isBefore && !IAR_handler_Contact.isCreatingAccountForContact)
    {
        handler.CreateAccountForContact(trigger.new);
    }

}

Thank you!

  • March 28, 2016
  • Like
  • 0