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
sfdc dev 2264sfdc dev 2264 

Apex class using metadata

Hi ,

I have the following requirement for which i need help to acheive via apex class using custom metadata

1) I have a metadata which has few words in it 

ie:

test,ablility

2) i want to check if any of the keywords from the metadata are present in the case description (exact match along with the content) 
then change the case owner to a particular queue.


I need help on the same with the apex class


Thanks in Advance
 
chanchal_:)chanchal_:)
I have Created a Custom Metadata for it -
Api name of metadata - Case_Description__mdt
Created a field in metadata - API name of the field in metadata - Description_Keywords__c.
Created two records in metadata like this -

User-added image




And then created an apex class - We can't filter queries on the basis of fields with type - long text area.


 
public  class CaseTestDescription {
    public static void HandleCase(string RecId){
        List<string> ListCaseIds = new List<string>(); 
        ListCaseIds.add(RecId);
        
        map<Id, string> mapIdWiseDescCase = new  map<Id, string>();
        for(case occ : [SELECT id, Description from Case WHERE id = :ListCaseIds]){
            mapIdWiseDescCase.put(occ.Id, occ.Description);
        }
        List<Case_Description__mdt> ListCaseKeyWords = [SELECT Description_Keywords__c FROM Case_Description__mdt];
        
        
        List<string> KeywordsCaseDescriptionString = new List<string>();
        for(Case_Description__mdt oCDMdt : ListCaseKeyWords){
            KeywordsCaseDescriptionString.add(oCDMdt.Description_Keywords__c);
        }
        
        List<string> CaseToMoveForowrd = new List<string>();
        for(string skey : mapIdWiseDescCase.keySet()){
            string Description = mapIdWiseDescCase.get(skey);
            for(string keyWord : KeywordsCaseDescriptionString){
                if(Description.contains(keyWord)){
                    CaseToMoveForowrd.add(skey);
                }        
            }
        }
        List<case> ListcaseToUpdateOwner= new   List<case>();
        List<case> ListCaseToChange = [Select id, owner.Id, OwnerId from Case where id = :CaseToMoveForowrd];
        Group sGQueue = [select Id from Group where  Type = 'Queue' AND NAME = 'Booyah'];
        for(case occ : ListCaseToChange){
            occ.OwnerId = sGQueue.Id;
            //Either----->  occ.owner.Id = sGQueue.Id;
            ListcaseToUpdateOwner.add(occ);
        }
        if(ListcaseToUpdateOwner.size()>0){
            update ListcaseToUpdateOwner;
        }
    }
}

Let me know if it works, Thank you