Apex Update Case Priority when Bad Words in Incoming Email

Updating a case priority of a case when you receive a email with some words in the body that will make it more important. By looking at the words you can update the priority to high so Support reps can faster attend to those cases

public with sharing class CaseMatchIncomingEmail {
	public enum badWords {
		Frustrated,
		Frustrating,
		Angry,
		Mad,
		Annoyed,
		Annoying,
		Complain,
		Complaint,
		Lost,
		Upset,
		Awful,
		Bad,
		Mislead,
		Misleading,
		Fraud,
		Scam,
		request,
		SEC,
		transfer,
		Broken,
		Problem,
		Issue,
		Disappoint,
		Disappointing,
		Prosper,
		Lawsuit,
		Sue,
		Hate,
		Attorney,
		Discrepancy,
		Pissed,
		ridiculous,
		immediate,
		immediately,
		kidding,
		joking,
		joke}

	public void mathIncomingCaseEmail(List<Case> casesToUpdate) {
		List<Case> caseToUpdateList = new List<Case>();

		for (Case caseToUpdate : casesToUpdate){
			for (badWords badword : badWords.values()){
				if (caseToUpdate.Subject!=null){
					if (caseToUpdate.Subject.containsIgnoreCase(badword.name())){
						caseToUpdate.Priority = 'High';
					}
				}
				if (caseToUpdate.Description!=null){
					if (caseToUpdate.Description.containsIgnoreCase('disclaimer')){
						if (caseToUpdate.Description.lastIndexOf('disclaimer')>0){
							if (caseToUpdate.Description.substring(0,caseToUpdate.Description.lastIndexOf('disclaimer')).containsIgnoreCase(badword.name())){
							caseToUpdate.Priority = 'High';
							}
						}
					}
					else if (caseToUpdate.Description.containsIgnoreCase(badword.name())){
						caseToUpdate.Priority = 'High';
					}
				}
			}

			if (caseToUpdate.SuppliedEmail != null){
				List<List<SObject>> searchList = searchByField(caseToUpdate.SuppliedEmail);
				List<Contact> contactSearchList =  ((List<Contact>)searchList[0]);
				List<Admin_Member_ID_Account__c> adminMemberSearchList =  ((List<Admin_Member_ID_Account__c>)searchList[1]);

				if (contactSearchList.size() > 0){
					for (Contact contactSearchResult:contactSearchList){
						if (contactSearchResult.Email == caseToUpdate.SuppliedEmail){
							caseToUpdate.ContactId = contactSearchList.get(0).Id;
							caseToUpdate.AccountId = contactSearchList.get(0).AccountId;
						}
					}
				}
				if (adminMemberSearchList.size() > 0){
					for (Admin_Member_ID_Account__c adminMemberIdAccount : adminMemberSearchList){
						if (adminMemberIdAccount.AM_ID_Email__c == caseToUpdate.SuppliedEmail)
							caseToUpdate.AM_Member_ID__c = adminMemberSearchList.get(0).Id;
					}
				}
			}
		}
	}
}

1 Comment

  1. Chirag Mehta says:

    Client Sues and Trigger makes it respond faster šŸ™‚ Nice and good one!

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s