If I was trying to compare a lot of strings, my code is quite easily read (I feel) but I know there is a lot of duplication. Especially when the field values are the same, is there a tidier way of writing this. This is expected to be ran in a beforeInsert context and I have 13 spam cases to capture and maybe another 10 more customised cases.
//Spam Cases
for (case cs : newCase){
String comparetext = cs.subject.tolowerCase();
Boolean result = comparetext.contains('whisky inactive users deleted');
if (result){
cs.type = 'Spam';
cs.reason = 'Spam';
cs.status = 'Closed';
cases2Update.add( cs );
}
comparetext = cs.subject.tolowerCase();
result = comparetext.contains('out of office');
if (result){
cs.type = 'Spam';
cs.reason = 'Spam';
cs.status = 'Closed';
cases2Update.add( cs );
}
comparetext = cs.subject.tolowerCase();
result = comparetext.contains('Automatic Reply');
if (result){
cs.type = 'Spam';
cs.reason = 'Spam';
cs.status = 'Closed';
cases2Update.add( cs );
}
comparetext = cs.subject.tolowerCase();
result = comparetext.contains('Your order is due for delivery by Parcel');
if (result){
cs.type = 'Spam';
cs.reason = 'Spam';
cs.status = 'Closed';
cases2Update.add( cs );
}
}
update cases2Update;
Edit: I am currently taking apart 3 different approval processes and 2 workflow rules inserting it all into a method executed on the beforeInsert context of cases. This will help me assign ownership easier and set default values as below shows. (hopefully).
Edit: I ended up creating a custom object for holding the settings. I then created the following code which is much shorter and in my first 5 tests, worked fine, Contains_Text__c is the custom field that contains the text to compare against. The rest is just fluff...
public static void updateNewCases(List<case> newCase){
//Is this method enabled by the admin settings.
List<Admin_Settings__c>classSettingsList = [SELECT Id, setting__c, Status__c, Sobject__c FROM Admin_Settings__c WHERE setting__c = 'updateNewCases' AND sobject__c = 'Case' LIMIT 1];
for (Admin_Settings__c settings : classSettingsList){
system.debug('status is' + settings.Status__c);
if (settings.Status__c != 'Live'){
system.debug('BREAKING OUT OF updateNewCases');
return;
}
}
List<Admin_Settings__c> AdminSettingsList = [SELECT Id, Contains_Text__c,Case_Status__c, Case_Reason__c, Case_Type__c, Queue_Id__c FROM Admin_Settings__c WHERE Status__c = 'Live'];
List<Case> cases2Update = new List<Case>();
for (Case cs : newCase){
String compareText = cs.subject.toLowerCase();
for (Admin_Settings__c settings : AdminSettingsList) {
Boolean StringCheck = compareText.contains(settings.Contains_Text__c);
if (StringCheck){
cs.type = settings.Case_Type__c;
cs.reason = settings.Case_Reason__c;
cs.status = settings.Case_Status__c;
cs.OwnerId = settings.Queue_id__c;
return;
}
}
}
}