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
Peter CowenPeter Cowen 

trigger to assign cases based on key words

Is it possible to create a trigger that can set the priority and the reason for the case based on key words. It would also be good to have the ability to add,remove or update key words without the need to update the trigger?
Best Answer chosen by Peter Cowen
Ginny MahantGinny Mahant
trigger CaseAutomation on Case (before insert, before update, after insert) {
    if(Trigger.isBefore())
    { 
    Id caseRecordTypeId = [Select id from RecordType where sObjectType = 'Case' and developerName ='Service_Desk' ].id ;
    
    Set <String> CaseAutomationset = new Set <String> () ;
    MAP <String, Case_key_words__c> rv = Case_key_words__c.getAll();
    CaseAutomationset.addAll(rv.Keyset());
        
    for (Case c : trigger.new) 
    {
        if (c.RecordTypeID == caseRecordTypeId )
        {
            for (String settingName: CaseAutomationset)
            { 
                 List<Strings> listOfKeywords = (rv.get(settingName).Keyword__c).split(',');
                 for(String str: listOfKeywords ) 
                 {
                    if (c.Subject.contains(str) || c.Body.contains(str))
                    {
                        c.Reason = rv.get(settingName).Reason__c;
                        c.Priority= rv.get(settingName).Priority__c;
                    } 
                 }
            }
        }
    
    }
    
    }
    //Assuming that tfs number is entered only upon record creation
    if(Trigger.isafter())
    {
        Set<Integer> tfsNos = new Set<Integer>();
        List<Your_custom_object__c> cobjToRecsCreate = new List<Your_custom_object__c>();        
        AggregateResult[] groupedResults = [SELECT tfsnmber, COUNT(Id) FROM Case WHERE createdDate >= '10/13/2017' && createdDate < '17/13/2017' GROUP BY tfsnmber HAVING COUNT(Id) >= 3];

        for (AggregateResult ar : groupedResults)  
        {
            tfsNos.add(ar.get('tfsnmber'));
            Your_custom_object__c cobjRec = new You_custom_object__c();
            cobjRec.tfsnmber = ar.get('tfsnmber');
            //set other fields cobjRec
            cobjToRecsCreate.add(cobjRec);
        }
        
        insert(cobjToRecsCreate);
        Map<Integer, Your_custom_object__c> map = new Map<Integer, Your_custom_object__c>();
        
        for (List<Your_custom_object__c> lst : cobjToRecsCreate)
           map.put(cobjToRecsCreate.tfsnmber,cobjToRecsCreate.Id);
        
        List <Case> casesToLinkAndUpd = [SELECT Id, tfsnmber, Your_custom_object__c FROM Case WHERE createdDate >= '10/13/2017' && createdDate < '17/13/2017' AND tfsnmber in : tfsNos ]
        for(Case c : casesToLinkAndUpd )
            c.Your_custom_object__c = map.get(c.tfsnmber);
            
        update casesToLink ;
}
           
    }

//I have hard coded the dates of the week but you can substitue them with variable.

If this solution helps please mark as best answer to help others identify this solution.

Regards,

All Answers

Ginny MahantGinny Mahant
Hi Peter,
    You can achieve this by a creating list custom setting with fields Keyword(String field), Priority,Reason. The keyword field can hold comma separated keywords for which you can enter appropriate reason in Reason field and Priority in Priority field. Since its a list type, you can make as many combinations of these three as you want. Your case trigger can just query all values of this setting and populate reason and priority as per the keywords in case. Hope this helps.
Peter CowenPeter Cowen
I have started to create the trigger. I have created a custom setting list with Keyword, Reason & Priority set as fields. How do I check whether the subject or body of a case contain the keywords specified in this list and then set the Reason & Prioirity picklists that are set against the key words. My trigger is checking that the Case record type is Service Desk.

trigger CaseAutomation on Case (before insert, before update) {
Set <String> CaseAutomationset = new Set <String> () ;
for (Case_key_words__c rv : Case_key_words__C.getAll().values()) {
CaseAutomationset.Add(rv.Keyword__c)
}
Id caseRecordTypeId = [Select id from RecordType where sObjectType = 'Case' and developerName ='Service_Desk' ].id ;
for (Case c : trigger.new) {
if (c.RecordTypeID == caseRecordTypeId )
if (c.Subject.contains(rv.Keyword__c))
}
}
If anyone has any guidance please let me know.

Eventually I would also like the trigger to count the number of entries in a specific field (tfs number) within a week period that are the same and once it hits 3 creates a entry in a custom object and then assocaites all cases with the same tfs number to that custom object entry. Some guidance on that would also be appricated.
Ginny MahantGinny Mahant
trigger CaseAutomation on Case (before insert, before update, after insert) {
    if(Trigger.isBefore())
    { 
    Id caseRecordTypeId = [Select id from RecordType where sObjectType = 'Case' and developerName ='Service_Desk' ].id ;
    
    Set <String> CaseAutomationset = new Set <String> () ;
    MAP <String, Case_key_words__c> rv = Case_key_words__c.getAll();
    CaseAutomationset.addAll(rv.Keyset());
        
    for (Case c : trigger.new) 
    {
        if (c.RecordTypeID == caseRecordTypeId )
        {
            for (String settingName: CaseAutomationset)
            { 
                 List<Strings> listOfKeywords = (rv.get(settingName).Keyword__c).split(',');
                 for(String str: listOfKeywords ) 
                 {
                    if (c.Subject.contains(str) || c.Body.contains(str))
                    {
                        c.Reason = rv.get(settingName).Reason__c;
                        c.Priority= rv.get(settingName).Priority__c;
                    } 
                 }
            }
        }
    
    }
    
    }
    //Assuming that tfs number is entered only upon record creation
    if(Trigger.isafter())
    {
        Set<Integer> tfsNos = new Set<Integer>();
        List<Your_custom_object__c> cobjToRecsCreate = new List<Your_custom_object__c>();        
        AggregateResult[] groupedResults = [SELECT tfsnmber, COUNT(Id) FROM Case WHERE createdDate >= '10/13/2017' && createdDate < '17/13/2017' GROUP BY tfsnmber HAVING COUNT(Id) >= 3];

        for (AggregateResult ar : groupedResults)  
        {
            tfsNos.add(ar.get('tfsnmber'));
            Your_custom_object__c cobjRec = new You_custom_object__c();
            cobjRec.tfsnmber = ar.get('tfsnmber');
            //set other fields cobjRec
            cobjToRecsCreate.add(cobjRec);
        }
        
        insert(cobjToRecsCreate);
        Map<Integer, Your_custom_object__c> map = new Map<Integer, Your_custom_object__c>();
        
        for (List<Your_custom_object__c> lst : cobjToRecsCreate)
           map.put(cobjToRecsCreate.tfsnmber,cobjToRecsCreate.Id);
        
        List <Case> casesToLinkAndUpd = [SELECT Id, tfsnmber, Your_custom_object__c FROM Case WHERE createdDate >= '10/13/2017' && createdDate < '17/13/2017' AND tfsnmber in : tfsNos ]
        for(Case c : casesToLinkAndUpd )
            c.Your_custom_object__c = map.get(c.tfsnmber);
            
        update casesToLink ;
}
           
    }

//I have hard coded the dates of the week but you can substitue them with variable.

If this solution helps please mark as best answer to help others identify this solution.

Regards,
This was selected as the best answer