Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- trigger EmailToTask on EmailMessage (after insert) {
- list<Task> lstTask = new list<Task>();
- map<String,Id> mapEmail = new map<String,Id>();
- map<String,Id> mapWorkEmail = new map<String,Id>();
- map<String,Id> mapHomeEmail = new map<String,Id>();
- for(Contact con: [SELECT Id,Email,npe01__WorkEmail__c,npe01__HomeEmail__c FROM Contact]){
- if(string.isNotBlank(con.Email))
- mapEmail.put(con.Email,con.Id);
- if(string.isNotBlank(con.npe01__WorkEmail__c))
- mapWorkEmail.put(con.npe01__WorkEmail__c,con.Id);
- if(string.isNotBlank(con.npe01__HomeEmail__c))
- mapHomeEmail.put(con.npe01__HomeEmail__c,con.Id);
- }
- for (EmailMessage e : Trigger.new) {
- if (e.RelatedToId != NULL
- && (mapEmail <> NULL && !mapEmail.isEmpty()
- || mapWorkEmail <> NULL && !mapWorkEmail.isEmpty()
- || mapHomeEmail <> NULL && !mapHomeEmail.isEmpty())
- ) {
- Task t = new Task();
- t.ActivityDate = date.today();
- t.Description = e.TextBody;
- t.Move_Type__c = 'Personal Note/Email from Donor';
- t.Priority = 'Normal';
- t.Status = 'Completed';
- t.Subject = e.Subject;
- t.WhatId = e.RelatedToId;
- if(mapEmail.containsKey(e.ToAddress)){
- t.WhoId = mapEmail.get(e.ToAddress);
- }
- else if(mapWorkEmail.containsKey(e.ToAddress )){
- t.WhoId = mapWorkEmail.get(e.ToAddress);
- }
- else if(mapHomeEmail.containsKey(e.ToAddress )){
- t.WhoId = mapHomeEmail.get(e.ToAddress);
- }
- lstTask.add(t);
- }
- }
- if(lstTask <> NULL && !lstTask.isEmpty()){
- insert lstTask;
- }}
- @isTest
- public with sharing class TestEmailTrigger {
- @isTest static void test_method_c() {
- Account a = new Account();
- a.Name = 'Bob Smith Household';
- insert a;
- Contact c = new Contact();
- c.lastname = 'Smith';
- c.FirstName = 'Bob';
- c.AccountId = a.Id;
- c.npe01__HomeEmail__c = 'bob.test@gmail.com';
- c.npe01__WorkEmail__c = 'bob.test+5@gmail.com';
- c.Email = NULL;
- c.npe01__Preferred_Email__c = 'Work';
- insert c;
- EmailMessage e = new EmailMessage();
- e.BccAddress = 'reporting@foodcorps.org';
- e.FromAddress = 'reporting@foodcorps.org';
- e.FromName = 'Medhanie Habte';
- e.MessageDate = datetime.now();
- e.MessageIdentifier = '';
- e.Status = '3';
- e.Subject = 'test';
- e.TextBody = 'test';
- e.RelatedToId = c.AccountId;
- e.ToAddress = 'bob.test@gmail.com';
- e.ValidatedFromAddress = 'reporting@foodcorps.org';
- insert e;
- Task t = new Task();
- t.ActivityDate = date.today();
- t.Description = e.TextBody;
- t.Move_Type__c = 'Personal Note/Email from Donor';
- t.Priority = 'Normal';
- t.Status = 'Completed';
- t.Subject = e.Subject;
- t.WhatId = e.RelatedToId;
- t.WhoId = c.Id;
- insert t;
- }
- @isTest static void test_method_b() {
- Account a = new Account();
- a.Name = 'Bob Smith Household';
- insert a;
- Contact c = new Contact();
- c.lastname = 'Smith';
- c.FirstName = 'Bob';
- c.AccountId = a.Id;
- c.npe01__HomeEmail__c = '';
- c.npe01__WorkEmail__c = 'bob.test+5@gmail.com';
- c.Email = NULL;
- c.npe01__Preferred_Email__c = 'Work';
- insert c;
- EmailMessage e = new EmailMessage();
- e.BccAddress = 'reporting@foodcorps.org';
- e.FromAddress = 'reporting@foodcorps.org';
- e.FromName = 'Medhanie Habte';
- e.MessageDate = datetime.now();
- e.MessageIdentifier = '';
- e.Status = '3';
- e.Subject = 'test';
- e.TextBody = 'test';
- e.RelatedToId = c.AccountId;
- e.ToAddress = 'bob.test+5@gmail.com';
- e.ValidatedFromAddress = 'reporting@foodcorps.org';
- insert e;
- Task t = new Task();
- t.ActivityDate = date.today();
- t.Description = e.TextBody;
- t.Move_Type__c = 'Personal Note/Email from Donor';
- t.Priority = 'Normal';
- t.Status = 'Completed';
- t.Subject = e.Subject;
- t.WhatId = e.RelatedToId;
- t.WhoId = c.Id;
- insert t;
- }
- @isTest static void test_method_d() {
- Account a = new Account();
- a.Name = 'Bob Smith Household';
- insert a;
- Contact c = new Contact();
- c.lastname = 'Smith';
- c.FirstName = 'Bob';
- c.AccountId = a.Id;
- c.npe01__HomeEmail__c = 'bob.test+5@gmail.com';
- c.npe01__WorkEmail__c = NULL;
- c.Email = NULL;
- c.npe01__Preferred_Email__c = 'Work';
- insert c;
- EmailMessage e = new EmailMessage();
- e.BccAddress = 'reporting@foodcorps.org';
- e.FromAddress = 'reporting@foodcorps.org';
- e.FromName = 'Medhanie Habte';
- e.MessageDate = datetime.now();
- e.MessageIdentifier = '';
- e.Status = '3';
- e.Subject = 'test';
- e.TextBody = 'test';
- e.RelatedToId = c.AccountId;
- e.ToAddress = 'bob.test+5@gmail.com';
- e.ValidatedFromAddress = 'reporting@foodcorps.org';
- insert e;
- Task t = new Task();
- t.ActivityDate = date.today();
- t.Description = e.TextBody;
- t.Move_Type__c = 'Personal Note/Email from Donor';
- t.Priority = 'Normal';
- t.Status = 'Completed';
- t.Subject = e.Subject;
- t.WhatId = e.RelatedToId;
- t.WhoId = c.Id;
- insert t;} @isTest static void test_method_e() {
- Account a = new Account();
- a.Name = 'Bob Smith Household';
- insert a;
- Contact c = new Contact();
- c.lastname = 'Smith';
- c.FirstName = 'Bob';
- c.AccountId = a.Id;
- c.npe01__HomeEmail__c = 'bob.test@gmail.com';
- c.npe01__WorkEmail__c = 'bob.test+5@gmail.com';
- c.Email = 'bob.test@aoltv.com';
- c.npe01__Preferred_Email__c = 'Work';
- insert c;
- EmailMessage e = new EmailMessage();
- e.BccAddress = 'reporting@foodcorps.org';
- e.FromAddress = 'reporting@foodcorps.org';
- e.FromName = 'Medhanie Habte';
- e.MessageDate = datetime.now();
- e.MessageIdentifier = '';
- e.Status = '3';
- e.Subject = 'test';
- e.TextBody = 'test';
- e.RelatedToId = c.AccountId;
- e.ToAddress = 'bob.test@gmail.com';
- e.ValidatedFromAddress = 'reporting@foodcorps.org';
- insert e;
- Task t = new Task();
- t.ActivityDate = date.today();
- t.Description = e.TextBody;
- t.Move_Type__c = 'Personal Note/Email from Donor';
- t.Priority = 'Normal';
- t.Status = 'Completed';
- t.Subject = e.Subject;
- t.WhatId = e.RelatedToId;
- t.WhoId = c.Id;
- insert t;}}
- || mapWorkEmail <> NULL && !mapWorkEmail.isEmpty()
- || mapHomeEmail <> NULL && !mapHomeEmail.isEmpty())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement