You need to sign in to do that
Don't have an account?
Update failed for the test class of a trigger giving coverage of 72%,not more than that
Main trigger
trigger AccountBeforeUpdate on Account (after insert, after Update)
{
//Get the RecordType Id
List<RecordType> rTypeList = new List<RecordType>();
map<Id,String> curUsMap = new Map<Id,String>();
map<Id,String> curUsNAMap = new Map<Id,String>();
set<Id> currentIds = new set<Id>();
set<Id> currentSoldTos = new Set<Id>();
SET<Id> oldOwnerIds = new set<Id>();
rTypeList = [SELECT ID FROM RECORDTYPE WHERE NAME='Sold-to' And SOBJECTTYPE = 'Account' limit 1];
List<AccountShare> oldShareList = new List<AccountShare>();
//CASE 1 Sold To Account associated with any National Account
List<Account> mainList = new List<Account>();
//If any Sold to created with National Account then
//create sharing rule
if(trigger.IsInsert) // When A new Sold to account is created with National Account
{
for(Account acNew:trigger.new)
{
if(rTypeList[0].Id == acNew.RecordTypeId && acNew.NationalAccount__C !=null )
{
mainList.add(acNew);
}
}
}
if(trigger.IsUpdate)
{
//If there is already any sharing rule then get the Combination of account record and sharing userId
oldShareList = [SELECT ACCOUNTID,USERORGROUPID FROM ACCOUNTSHARE WHERE ACCOUNTID IN:(TRIGGER.OLDMAP.KEYSET())];
if(!oldShareList.isEmpty())
{
for(ACCOUNTSHARE A:oldShareList)
{
curUsMap.put(A.ACCOUNTID,A.USERORGROUPID);
}
}
for(Account acOld:trigger.Old)
{
for(Account ac:trigger.new)
{
//DO FILTERATION
if(!rTypeList.isEmpty())
{
// If Account is Sold To and is associated with National Account then do Operation
System.Debug('Old Account:' + ' ' + acOld.NationalAccount__C);
if(rTypeList[0].Id == ac.RecordTypeId && ac.NationalAccount__C !=null )
{
if(acOld.NationalAccount__C !=ac.NationalAccount__C && acOld.NationalAccount__C !=null )
{
// FINALLY ADD FILTERED RECORD To Main Account List to create sharing rule for them
//to provide National Account Owner R/W Permission
mainList.add(ac);
}
else if(acOld.NationalAccount__C == null)
{
// FINALLY ADD FILTERED RECORD To Main Account List to create sharing rule for them
//to provide National Account Owner R/W Permission
mainList.add(ac);
}
}
}
}
}
//CASE 2 - OWNERSHIP FOR ONE NATIONAL ACCOUNT CHANGES TO ANOTHER OWNER
rTypeList.clear();
//Get the recorTypeId for "National Account" type of Account
//we would track the ownership change events for these accounts only
rTypeList = [SELECT ID FROM RECORDTYPE WHERE NAME='National Account' And SOBJECTTYPE = 'Account' limit 1];
//iterating Old National Account Ownership records
for(Account acNOld:trigger.Old)
{
System.Debug('New List:' + ' ' + trigger.new);
//iterating new National Account Ownership records
for(Account acNNew:trigger.New)
{
//If account is National Account
if(acNOld.RecordTypeId == rTypeList[0].Id && acNNew.RecordTypeId == rTypeList[0].Id)
{
//If current National Account Owner is different to Old National Account Owner
if(acNOld.OwnerId != acNNew.OwnerId)
{
//then prepare list of those accounts
currentIds.add(acNNew.Id);
oldOwnerIds.add(acNOld.OwnerId);
}
}
}
}
//Now get the List of all Sold To Accounts associated to the
List<Account> updateSoldToOwnerAcList = new List<Account>();
updateSoldToOwnerAcList = [SELECT ID,NATIONALACCOUNT__c FROM ACCOUNT WHERE NATIONALACCOUNT__c IN:(currentIds)];
if(!updateSoldToOwnerAcList.IsEmpty())
{
for(Account acOwnerChangedSoldTo:updateSoldToOwnerAcList)
{
mainList.add(acOwnerChangedSoldTo);
currentSoldTos.add(acOwnerChangedSoldTo.Id);
}
}
//Now get the List of Sold To Id shared with Old National Account Owner
oldShareList.clear();
curUsNaMap.clear();
//If there is already any sharing rule then get the Combination of account record and sharing userId
oldShareList = [SELECT ACCOUNTID,USERORGROUPID FROM ACCOUNTSHARE WHERE ACCOUNTID IN:(currentSoldTos)
AND USERORGROUPID IN:(oldOwnerIds) AND ISDELETED =FALSE];
if(!oldShareList.isEmpty())
{
for(ACCOUNTSHARE A:oldShareList)
{
curUsNaMap.put(A.ACCOUNTID,A.USERORGROUPID);
}
}
}
//Finally invoke method for sharing rule creaton / revokation
if(!mainList.IsEmpty() && !curUsMap.isEmpty())
{
AccountTriggerCls.doRemoveSharingRule(curUsMap,mainList); // Remove current sharing rule and create new sharing rule
}
else if(!mainList.IsEmpty() && !curUsNaMap.isEmpty())
{
AccountTriggerCls.doRemoveSharingRule(curUsNaMap,mainList); // Remove current sharing rule and create new sharing rule
}
else
{
AccountTriggerCls.doCreateSharingRule(mainList); // Create first time new sharing rule once any Sold To is associated to any National Account
}
}
Apex class based on trigger
public class AccountTriggerCls
{
//Region global variable
public static List<AccountShare> acShareList= new List<AccountShare>();
static Map<Id,String> nationalIds = new Map<Id,String>();
static Map<Id,String> mapOwnerIds = new Map<Id,String>();
//end
//Creating sharing rule for the National Account Owner
public static void doCreateSharingRule(List<Account> acMainList)
{
//We would get the Id for the record
//mention it for dynamic sharing rule
for(Account acMa:acMainList)
{
nationalIds.put(acMa.NATIONALACCOUNT__c,acMa.Id);
}
//Get the NationalAccountOwner Ids map
List<Account> acNat = new List<Account>();
acNat = [SELECT ID,OWNERID FROM ACCOUNT WHERE ID IN:(nationalIds.KeySet())];
if(!acNat.isEmpty())
{
for(Account acNational:acNat)
{
mapOwnerIds.put(acNational.Id,acNational.OwnerId);
}
}
for(Account ac:acMainList)
{
AccountShare aShare = new AccountShare();
aShare.AccountId=ac.id;
aShare.AccountAccessLevel= 'Edit';
// System.Debug('Owner Id:' + ' ' + ac.nationalAccount__r.OwnerId);
aShare.UserOrGroupId=mapOwnerIds.get(ac.NationalAccount__C);
aShare.OpportunityAccessLevel = 'Edit';
acShareList.add(aShare);
}
upsert acShareList;
}
//Creating sharing rule for the National Account Owner
public static void doCreateReverseSharingRule(List<Account> acMainList,Map<Id,String> OldIds)
{
for(String n:OldIds.values())
{
for(Account ac:acMainList)
{
AccountShare aShare = new AccountShare();
aShare.AccountId=ac.id;
aShare.AccountAccessLevel= 'Read';
// System.Debug('Owner Id:' + ' ' + ac.nationalAccount__r.OwnerId);
aShare.UserOrGroupId=n;
aShare.OpportunityAccessLevel = 'Read';
acShareList.add(aShare);
}
}
upsert acShareList;
}
//Removing sharing rule for Old Owner
public static void doRemoveSharingRule(Map<Id,String> oldIdMap,List<Account> acMainList)
{
List<AccountShare> oldShareList = new List<AccountShare>();
oldShareList = [SELECT ID,USERORGROUPID,AccountAccessLevel FROM ACCOUNTSHARE WHERE ACCOUNTID IN:(oldIdMap.keySet())
];
if(!oldShareList.isEmpty())
{
for(AccountShare acOls:oldShareList)
{
mapOwnerIds.put(acOls.USERORGROUPID,acOls.USERORGROUPID);
}
doCreateReverseSharingRule(acMainList,mapOwnerIds);
}
// finally create the sharing rule
doCreateSharingRule(acMainList);
}
//end
}
Test Class
@isTest
private class AccountTriggerCls_Test{
Static AccountTriggerCls mainClass=null;
Static Account accountObj;
Static testMethod void AccountTriggerCls_Test()
{
Test.StartTest();
PrepareTestData();
Test.StopTest();
}
static void PrepareTestData()
{
//Create User and attach National Account Manager profile
List<Profile> pList = new List<Profile>();
pList = [SELECT ID FROM PROFILE WHERE NAME='National Account Manager' Limit 1];
User u = new User(FirstName='Test',LastName='tt',username='ab@a.com',CommunityNickname='aa',
profileId=pList[0].Id,alias='ATE',email='a@a.com',TimeZoneSidKey='America/Los_Angeles',LocaleSidKey='en_US',
LanguageLocaleKey='en_US',EmailEncodingKey='UTF-8');
insert u;
//Create National Account and Assign Owner
Account acNa = (Account)DataCreator.creatObjectDataWithAllButNoRelationship('Account',1);
List<RecordType> rTypeList = new List<RecordType>();
rTypeList = [SELECT ID FROM RECORDTYPE WHERE NAME='National Account' Limit 1];
if(!rTypeList.IsEmpty())
{
acNa.RecordTypeId = rTypeList[0].Id;
acNa.OwnerId = u.Id;
acNa.National__c = true;
}
update acNa;
//Create Sold To Account
Account acSt = (Account)DataCreator.creatObjectDataWithAllButNoRelationship('Account',1);
List<RecordType> rTypeListSt = new List<RecordType>();
rTypeListSt = [SELECT ID FROM RECORDTYPE WHERE NAME='Sold-to' Limit 1 ];
if(!rTypeListSt.IsEmpty())
{
acSt.RecordTypeId = rTypeListSt[0].Id ;
acSt.NationalAccount__c= acNa.Id;
acSt.National__c = true;
}
update acSt;
}
}
I have used datacreator which is an accelarator class to cover the test class.
Please help me to solve the update problem.As doCreateSharingRule(acMainList) in the rigger is not executing.
You can take a look at this post , hope this will help you.
http://abhithetechknight.blogspot.in/2013/10/salesforce-test-class-basics.html