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
dev_sfdc1dev_sfdc1 

Case Creation in Trigger

Hi,
I have to customize Email to Case . Please help me.
My scenario is,
If my domain name is filled with values in accounts and based on that email field is filled in Contacts, then I need to create Case through Email to Case.But here my problem is Email is not firing through this trigger.
Trigger UpatestatusonCase on EmailMessage (before insert) 
{
        List<Contact> lstcon = new List<Contact>();
        List<Account> lstacc = [ Select id,Name from Account where Domain_Name__c ='www.techlab.com'];
        if(lstacc.size() > 0)
        {      
        lstcon= [Select id,Email,AccountId from Contact where AccountId =: lstacc[0].id and Email = 'tech@techlab.com'];
        }
        if(lstcon.size() > 0)
        {
        for(Contact con : lstcon)
        {
        String stremail = lstcon[0].Email;
        List<EmailMessage> em= new List<EmailMessage>();   
        em=[select id,parentid,FromAddress from EmailMessage where id =: em[0].id and FromAddress =: stremail];
        system.debug('1'+em[0].FromAddress);
        //String emid = em[0].Parentid;
        if(em.size() > 0)
        {
        system.debug('2passed');
        List<Case> cs= new List<Case>();
        for(EmailMessage e : em)
        {
            if(e.FromAddress != '')
            {
                 cs.add(new Case(Subject = e.Subject,Origin = 'Email',Status = 'New',Description = e.TextBody,SuppliedEmail = e.FromAddress));
            }
        }
        insert cs;
        
        
       }
       }
       }
     
  
}

Thank You
John PipkinJohn Pipkin
If you are trying to access the email that should call this trigger, you will need to use Trigger.new. See below:
Trigger UpatestatusonCase on EmailMessage (before insert) 
{
    List<Contact> lstcon = new List<Contact>();
    List<Account> lstacc = [ Select id,Name from Account where Domain_Name__c ='www.techlab.com'];

    if(lstacc.size() > 0)
    {      
        lstcon= [Select id,Email,AccountId from Contact where AccountId =: lstacc[0].id and Email = 'tech@techlab.com'];
    }
    if(lstcon.size() > 0)
    {
        List<Case> cs= new List<Case>();
        for(Contact con : lstcon)
        {
            String stremail = lstcon[0].Email;   
            system.debug('2passed');
            
            for(EmailMessage e : Trigger.new)
            {
                system.debug('1'+em[0].FromAddress);
                if(e.FromAddress != '' && e.FromAddress == stremail)
                {
                     cs.add(new Case(Subject = e.Subject,Origin = 'Email',Status = 'New',Description = e.TextBody,SuppliedEmail = e.FromAddress));
                }
            }
       }
       insert cs;
   }
}

Hope that helps
dev_sfdc1dev_sfdc1
Hi John,

Thanks for you reply..But still having same problem.
John PipkinJohn Pipkin
turn on the debug log. Is this trigger executing at all?
dev_sfdc1dev_sfdc1
My trigger is firing based on the above given mail id its working.But if I change anyother mail with other domain name also creating the cases automatically, at this point. How can I prevent other domain or mail to stop creating cases through Email to Case customisation..
Please help me.
Thank you.
 
John PipkinJohn Pipkin
Is the other domain set up as an email in email-to-case standard functionality?