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
Smurfet15Smurfet15 

Email to case field population from user object

Hi Guro,

We have an email to case setup, and we want populate field on case based on the related email fromaddress, since email address will be the lookup to user object then populated the rest of the field then save the case. I have tried to create the trigger with below logic and i am getting an error(invalid type: email) on the "set<email> = new set<email>()" tag.

Can you please help mo on this code?

----------------------------------
trigger  Fieldpopulation on Case (before insert) {
    List<Case> C =  trigger.new;      
    List<EmailMessage> EMessage = [Select id, FromAddress, parentid
                                   From EmailMessage
                                   where parentid in :C];
 
    //assigning the email address
       Set<email> fromadd = new Set<email>(); //getting error here
       for( EmailMessage EM: EMessage )
       {
          fromadd.add( EMessage.FromAddress );
       }
    
    
    //Select in the user object to populate Requestor data
       List <User> ulist = [Select id, name, email, Department, Sales_Office__c, Phone  
                           from user
                          where email = :fromAdd ];
         
    // Update the Case
       c.loc_name__c   = ulist.name;
       c.loc_email__c   = ulist.email;
       c.loc_Dept__c    = ulist.Department;
    update c;
    }

Thanks in advance.

 
daniel_hdaniel_h
Email is not a type that can be used in Salesforce. You will need to change that to a string.

Set<String> fromadd = new Set<String>();
Smurfet15Smurfet15
Thanks Daniel,

I did try that but error now is here:
  //assigning the email address
       Set<string> fromadd = new Set<string>();
       for( EmailMessage EM: EMessage )   // error : Initial term of field expression must be a concrete SObject: List<EmailMessage>
       {
          fromadd.add( EMessage.FromAddress );
       }

----------
on the User object email field and EmailMessage fromaddress has both email type. 

Mary
 
daniel_hdaniel_h
Looking more closely at your code, you've got a number of issues. The biggest one is that you have not bulkified your code. Triggers can have up to 200 records come into them at a time in Trigger.new. If this happens your code wouldn't work.

To solve the compile error, do this:
for( EmailMessage EM: [Select id, FromAddress, parentid
                                   From EmailMessage
                                   where parentid in :trigger.new] ) {
          fromadd.add( EM.FromAddress );
       }

You're going to have problems after that code though because c is actually a List<Case> and not a single Case. You need to use some maps to hold values and then do loops to update records correctly. See http://sfdc.arrowpointe.com/2008/09/13/bulkifying-a-trigger-an-example/ for an example of how that might look.
 
daniel_hdaniel_h
Also, you are doing a before insert, so you won't have any Emails yet. Even in an after insert on case, you probably won't have the emails avaiolable for querying. You should probably trigger on EmailMessage instead.