Skip to content

Instantly share code, notes, and snippets.

@tet3
Last active June 27, 2019 21:03
Show Gist options
  • Save tet3/6e67943c91f9074b0d24088a576a0b5c to your computer and use it in GitHub Desktop.
Save tet3/6e67943c91f9074b0d24088a576a0b5c to your computer and use it in GitHub Desktop.
Mail Merge ID finding Task trigger
trigger TaskMailMergeTrigger on Task (before insert) {
//Assumes "Mail Merge ID: " *with the space* is always on the line before
//the ID, which is all numbers
Pattern mmID = Pattern.compile('Mail Merge ID: ([0-9]+)');
for (Task t : trigger.new) {
Matcher mTask = mmID.matcher(t.Description);
if (mTask.find()) {
//Assumes you have a text field, Mail_Merge_ID__c, on the Activity object
t.Mail_Merge_ID__c = mTask.group(1);
}
}
}
@isTest
public class TestTaskMailMerge {
@isTest
public static void testWithMatch() {
String mergeId = '50403161';
Task hasMatch = new Task(OwnerId = UserInfo.getUserId(),
Subject = 'Task with Mail Merge ID',
Description = 'Mail Merge: Yes\n\nMail Merge ID: ' + mergeId + '\n\nMail Merge Name: Sample email',
Status = 'Not Started',
Priority = 'Normal'
);
Test.startTest();
insert hasMatch;
Test.stopTest();
hasMatch = [SELECT Id, Mail_Merge_ID__c FROM Task WHERE Id = :hasMatch.Id];
System.assertEquals(mergeId, hasMatch.Mail_Merge_ID__c,'Merge Id field not correctly populated');
}
@isTest
public static void testNoMatch() {
Task noMatch = new Task(OwnerId = UserInfo.getUserId(),
Subject = 'Plain old Task',
Description = 'Just a regular Task description.',
Status = 'Not Started',
Priority = 'Normal'
);
Test.startTest();
insert noMatch;
Test.stopTest();
noMatch = [SELECT Id, Mail_Merge_ID__c FROM Task WHERE Id = :noMatch.Id];
System.assert(String.isBlank(noMatch.Mail_Merge_ID__c),'Merge Id field is not blank');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment