+ Start a Discussion

Need help in Trigger

I have one scenario for Email to Case Customisation(for this I have some conditions to create new cases):

User sends request via email link on website,
1.If that domain listed on Account page,then new case is created.
2.If it is not on Account then it will check availability in contact,then new case is created.
I need to go for trigger or else someother validation.Is this possible to customise email to case in salesforce.
Please give me some idea to start.

Thank You
SonamSonam (Salesforce Developers) 
Following is what I think you can try:
1)Create a field on Account which stores the allowed domain
2)Create a before insert trigger on Case such that when a new case is about to be created -
a) split the email address of the contact on the case to get the domain using the split method of String class:http://www.salesforce.com/us/developer/docs/dbcom_apex250/Content/apex_methods_system_string.htm#SplittingStringsExample
b)Compares it to the Account field which has the allowed domains to see if the field contains this domain(using contains method of String class)

If it does, you create the case else send an email to the email address on the Web email field which captures the email of the case creator throwing an error:

trigger sample code:

Sample code for sending email:
Hi Sonam,
Thanks to your reply. I have tried in trigger but its not working to me which means I have enabled Email to Case and I wrote trigger to check simple function first if that hardcoded email address  will present in Contact then only able to create 'New Case 'else 'No need to create'.

Here my trigger:

trigger caseemailhandler on Case(before insert) {

     List<Contact> c = [Select id,Email from Contact where Email = 'john@gmail.com'];
     Set<Id> lstct = new Set<Id>();
     for(Contact con : c)
         if(con.Email == 'john@gmail.com')
             String stremail = con.Email;
             List<Case> cas = new List<Case>();
             List<EmailMessage> lstem = [Select id,FromAddress,Subject,TextBody from EmailMessage where FromAddress =:stremail];
             for(EmailMessage em : lstem)
             if(em.FromAddress != '')
                 cas.add(new Case(Subject = em.Subject,Origin = 'Email',Status = 'New',Description = em.TextBody,SuppliedEmail = em.FromAddress));
             insert cas;
             system.debug('Inserted Cases'+cas);

But I had one problem here, Case is not created through trigger it will be firing through email to case - How can I customise this Email to Case  based on certain conditions.(here just I'm checking simple customisation based on this I can enhance my work from here)

Thank you