trigger ShareRecordWithIM on nnr__c (after update)
{
List<nnr__Share> shareList = new List<nnr__Share>();
List<nnr__Share> deleteList = new List<nnr__Share>();
nnr__Share ImpShare,ImpDelete;
List<RecordType> MMRT = [SELECT Id FROM RecordType WHERE DeveloperName IN ('knr','wg')];
Map<Id,Id> MMRTMap = new Map<Id,Id>{};
String[] toAddress = new String[]{UserInfo.getUserEmail()};
List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
Messaging.SingleEmailMessage email;
Id oldRecShare;
for(RecordType rt : MMRT)
MMRTMap.put(rt.Id,rt.Id);
for(nnr__c newRec : Trigger.new)
{
oldRecShare = trigger.oldMap.get(newRec.Id).Share_Record__c;
if(newRec.Share_Record__c !=null && (oldRecShare != newRec.Share_Record__c) && MMRTMap.containsKey(newRec.RecordTypeId))
{
ImpShare = new nnr__Share();
ImpShare.ParentId = newRec.Id;
ImpShare.UserOrGroupId = newRec.Share_Record__c;
ImpShare.AccessLevel = 'Edit';
shareList.add(ImpShare);
//send email to TL
email = new Messaging.SingleEmailMessage();
email.setToAddresses(toAddress);
String IM = [SELECT Name FROM User WHERE Id =:newRec.Share_Record__c LIMIT 1].Name;
email.setSubject(newRec.Opportunity_UniqueID__c+' Case Access Shared Notification');
email.setHTMLBody('Hello,<br><br>Shared access to Implementation '+newRec.Name+' has been given to '+IM+'.<br><br>Kind regards,<br>Centralised Implementation Solutions '+newRec.Market_Referred__c);
email.setSaveAsActivity(false);
mails.add(email);
}
if((oldRecShare != null) && (oldRecShare != newRec.Share_Record__c) && MMRTMap.containsKey(newRec.RecordTypeId))
{
ImpDelete = [SELECT Id FROM nnr__Share WHERE UserOrGroupId =:oldRecShare AND ParentId =: newRec.Id];
deleteList.add(ImpDelete);
}
}
try
{
if(shareList.size()>0)
insert shareList;
if(deleteList.size()>0)
delete deleteList;
if(mails != null)
//Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
Messaging.sendEmail(mails);
}
catch(Exception e)
{
System.debug('Following error occured while inserting sharing record: '+e);
}
}
============================TEST CLASS=======================
@isTest
public class Test_ShareRecordWithIM
{
static TestMethod void testSRIM()
{
Profile p = [select id from profile where name='System Administrator'];
//User Insert
User u = new user(alias = 'stdt', email='testsharerecorduser1@testorg.com', emailencodingkey='UTF-8', lastname='DPSRTest', languagelocalekey='en_US', localesidkey='en_US', profileid = p.Id, timezonesidkey='America/Los_Angeles', username='testsharerecorduser1@testorg.com',Segment__c = 'Mid Market', Market__c = 'UK');
Insert u;
User u1 = new user(alias = 'stdt', email='test_share_record_user_2@testorg.com', emailencodingkey='UTF-8', lastname='DPSRTest', languagelocalekey='en_US', localesidkey='en_US', profileid = p.Id, timezonesidkey='America/Los_Angeles', username='test_share_record_user_2@testorg.com',Segment__c = 'Mid Market', Market__c = 'UK');
Insert u1;
//Account Insert
Account acc = new Account();
acc.Name = 'Test Account';
acc.AnnualRevenue = 12345;
acc.CurrencyIsoCode = 'USD';
Insert acc;
test.starttest();
//Opportunity Record 1
Opportunity opp = new Opportunity();
opp.Name = 'Test Opp';
opp.StageName = '7 - Contract Fully Executed';
opp.Core_Product__c = 'Corporate Card';
opp.Sub_Product__c = 'Gold';
opp.Type = 'New';
opp.CloseDate = System.now().date();
opp.Opportunity_Market__c = 'UK';
opp.AccountId = acc.Id;
opp.LeadSource = 'Test';
opp.Probability = 34;
opp.Potential_CV_USD__c = 23;
opp.CurrencyIsoCode = 'USD';
opp.Commodity__c = 'Test';
Insert opp;
EMEA_Implementation__c imp = new EMEA_Implementation__c();
imp.Name = 'Test imp';
imp.Opportunity__c = opp.Id;
Insert imp;
imp.Share_record__c = u.Id;
Update imp;
imp.Share_record__c = u1.Id;
Update imp;
Test.stoptest();
}
{
List<nnr__Share> shareList = new List<nnr__Share>();
List<nnr__Share> deleteList = new List<nnr__Share>();
nnr__Share ImpShare,ImpDelete;
List<RecordType> MMRT = [SELECT Id FROM RecordType WHERE DeveloperName IN ('knr','wg')];
Map<Id,Id> MMRTMap = new Map<Id,Id>{};
String[] toAddress = new String[]{UserInfo.getUserEmail()};
List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
Messaging.SingleEmailMessage email;
Id oldRecShare;
for(RecordType rt : MMRT)
MMRTMap.put(rt.Id,rt.Id);
for(nnr__c newRec : Trigger.new)
{
oldRecShare = trigger.oldMap.get(newRec.Id).Share_Record__c;
if(newRec.Share_Record__c !=null && (oldRecShare != newRec.Share_Record__c) && MMRTMap.containsKey(newRec.RecordTypeId))
{
ImpShare = new nnr__Share();
ImpShare.ParentId = newRec.Id;
ImpShare.UserOrGroupId = newRec.Share_Record__c;
ImpShare.AccessLevel = 'Edit';
shareList.add(ImpShare);
//send email to TL
email = new Messaging.SingleEmailMessage();
email.setToAddresses(toAddress);
String IM = [SELECT Name FROM User WHERE Id =:newRec.Share_Record__c LIMIT 1].Name;
email.setSubject(newRec.Opportunity_UniqueID__c+' Case Access Shared Notification');
email.setHTMLBody('Hello,<br><br>Shared access to Implementation '+newRec.Name+' has been given to '+IM+'.<br><br>Kind regards,<br>Centralised Implementation Solutions '+newRec.Market_Referred__c);
email.setSaveAsActivity(false);
mails.add(email);
}
if((oldRecShare != null) && (oldRecShare != newRec.Share_Record__c) && MMRTMap.containsKey(newRec.RecordTypeId))
{
ImpDelete = [SELECT Id FROM nnr__Share WHERE UserOrGroupId =:oldRecShare AND ParentId =: newRec.Id];
deleteList.add(ImpDelete);
}
}
try
{
if(shareList.size()>0)
insert shareList;
if(deleteList.size()>0)
delete deleteList;
if(mails != null)
//Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
Messaging.sendEmail(mails);
}
catch(Exception e)
{
System.debug('Following error occured while inserting sharing record: '+e);
}
}
============================TEST CLASS=======================
@isTest
public class Test_ShareRecordWithIM
{
static TestMethod void testSRIM()
{
Profile p = [select id from profile where name='System Administrator'];
//User Insert
User u = new user(alias = 'stdt', email='testsharerecorduser1@testorg.com', emailencodingkey='UTF-8', lastname='DPSRTest', languagelocalekey='en_US', localesidkey='en_US', profileid = p.Id, timezonesidkey='America/Los_Angeles', username='testsharerecorduser1@testorg.com',Segment__c = 'Mid Market', Market__c = 'UK');
Insert u;
User u1 = new user(alias = 'stdt', email='test_share_record_user_2@testorg.com', emailencodingkey='UTF-8', lastname='DPSRTest', languagelocalekey='en_US', localesidkey='en_US', profileid = p.Id, timezonesidkey='America/Los_Angeles', username='test_share_record_user_2@testorg.com',Segment__c = 'Mid Market', Market__c = 'UK');
Insert u1;
//Account Insert
Account acc = new Account();
acc.Name = 'Test Account';
acc.AnnualRevenue = 12345;
acc.CurrencyIsoCode = 'USD';
Insert acc;
test.starttest();
//Opportunity Record 1
Opportunity opp = new Opportunity();
opp.Name = 'Test Opp';
opp.StageName = '7 - Contract Fully Executed';
opp.Core_Product__c = 'Corporate Card';
opp.Sub_Product__c = 'Gold';
opp.Type = 'New';
opp.CloseDate = System.now().date();
opp.Opportunity_Market__c = 'UK';
opp.AccountId = acc.Id;
opp.LeadSource = 'Test';
opp.Probability = 34;
opp.Potential_CV_USD__c = 23;
opp.CurrencyIsoCode = 'USD';
opp.Commodity__c = 'Test';
Insert opp;
EMEA_Implementation__c imp = new EMEA_Implementation__c();
imp.Name = 'Test imp';
imp.Opportunity__c = opp.Id;
Insert imp;
imp.Share_record__c = u.Id;
Update imp;
imp.Share_record__c = u1.Id;
Update imp;
Test.stoptest();
}
No comments:
Post a Comment