+ Start a Discussion
TejTej 

Case field Update based on attached emailmessage status

I created a trigger to update a field in Case to either New or Read based on attached emailmessages status.

 

if there are any new emails attached to case, the case email status should be "new".

 

If there are no New emails the case email status should be "Read".

 

everything is working fine except, the trigger is not getting fired when they open the new email to read, the emailmessage status is getting updated from New to Read, but the trigger is not getting fired to update the case email status.

 

please check the below code.

trigger CaseEmailStatusUpdate on EmailMessage (after insert, after update) {

	ID caseId;
	list<EmailMessage> emails = new list<EmailMessage>();
	set<string> status =  new set<String>();
	Case c = new Case();

	
	for( EmailMessage e : trigger.new){
	
		caseId = e.ParentId;
	}
	
	emails = [ SELECT Id, ParentId, Status, Subject FROM EmailMessage where parentId = :caseId];
	c = [ Select Id, Case_Email_Status__c From Case where Id = :caseId];
	
	for( EmailMessage e : emails){
			
			status.add(e.Status); 
		
	}
	
	if(status.size() > 0 && status.Contains('0')){
	
		c.Case_Email_Status__c = 'New';
		
	}else if (status.size() > 0 && !status.Contains('0')){
	
		c.Case_Email_Status__c = 'Read';
		
	}
	
	update c;
	
	
}

 

Naidu PothiniNaidu Pothini
trigger CaseEmailStatusUpdate on EmailMessage (after insert, after update)
{
  List<Id> caseIds = new List<Id>();
  List<Case> emailCases = new List<Case>();

  if(Trigger.isInsert)
  {
    for( EmailMessage e : trigger.new)
    {
      caseIds = e.ParentId;
    }
  }

  if(Trigger.isUpdate)
  {
    for( EmailMessage e : trigger.new)
    {
      if(e.Status <> trigger.oldMap.get(e.Id).Status)
      {
        caseIds = e.ParentId;
      }
    }
  }

  Map<Id, AggregateResult> arMap = new Map<Id, AggregateResult>([SELECT ParentId Id, Count(Id) Cnt 
                                                                 FROM EmailMessage
                                                                 WHERE ParentID IN :caseIds
                                                                 AND Status = 'New'
                                                                 GROUP BY ParentId]);

  for(Case c : [SELECT Id, Case_Email_Status__c FROM Case WHERE Id IN :caseIds])
  {
    if(arMap.get(c) <> null)
    {
      c.Case_Email_Status__c = 'New';
    }
    else
    {
      c.Case_Email_Status__c = 'Read';
    }

    emailCases.add(c);
  }

  update emailCases;
}

 try this.

TejTej

the trigger will never get invoked in my case because, per SFDC support email status change from New to Read is not a DML operation, its salesforce standard functionality, therefore it can't invoke a trigger or wrkflow.

Rebecca VandersliceRebecca Vanderslice
did you ever find a solution for this?  I just ran into the same thing and i'm trying to figure out some way that i can still kickoff a workflow when the email changes from new to read.