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
Brandon Bridges 16Brandon Bridges 16 

From Subject Received Size Categories Salesforce Success Community (salesforce.com): New reply to your question. Sat 12/10 16 KB

In the service cloud, I am trying to set up a trigger.

The trigger is on emailMessage. When a consultant sends a second email to a customer, I want to create a new case with all of the same field values as the original case,except for id, and attach the trigger email to that new case. 
Currently, I'm having major trouble with the SOQL query that does this.
 
...
for(EmailMessage e:Trigger.new){
 if(e.Incoming==false){//Email must be outgoing
    string expr='[SELECT ';
    Map<String, String> labelMap = new Map<String, String>();
    Schema.DescribeSObjectResult objectDescribe  = Case.SObjectType.getDescribe();
    Map<String, Schema.SObjectField>fieldMap = objectDescribe.fields.getMap();
   	for(String fieldName:fieldMap.keySet()) {
      		Schema.SObjectField field=fieldMap.get(fieldName);
        	Schema.DescribeFieldResult fieldDescribe=field.getDescribe();      		labelMap.put(fieldDescribe.getLabel(),fieldDescribe.getName());
    		expr+=fieldDescribe.getName()+',';
    		}
    	expr=expr.substring(0,expr.length()-1)+
               ' FROM case WHERE id=:e.parentId]';
    	System.Debug(expr);
        System.Debug(e.ParentId);
        System.Debug('size'+expr.length());
        List<case>myCases=[SELECT expr FROM case WHERE id=:e.ParentId LIMIT 1];