Last active
June 27, 2019 21:03
-
-
Save tet3/6e67943c91f9074b0d24088a576a0b5c to your computer and use it in GitHub Desktop.
Mail Merge ID finding Task trigger
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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