You need to sign in to do that
Don't have an account?
John McAfee
test case for entitlement Trigger
Hey all, I am new to to Apex code and wanted to see if anyone could help me with a test class for this trigger.. also thanks to whoever wrote this.. the community is awesome.
I just need to match entitlement to a case based on the account in lighning. thanks in advance.
trigger DefaultEntitlement on Case (Before Insert, Before Update) {
List<Id> contactIds = new List<Id>();
List<Id> acctIds = new List<Id>();
for (Case c : Trigger.new){
if (c.EntitlementId == null && c.ContactId != null && c.AccountId != null){
contactIds.add(c.ContactId);
acctIds.add(c.AccountId);
}
}
if(contactIds.isEmpty()==false || acctIds.isEmpty()==false){
List <EntitlementContact> entlContacts =
[Select e.EntitlementId,e.ContactId,e.Entitlement.AssetId
From EntitlementContact e
Where e.ContactId in :contactIds
And e.Entitlement.EndDate >= Today
And e.Entitlement.StartDate <= Today];
if(entlContacts.isEmpty()==false){
for(Case c : Trigger.new){
if(c.EntitlementId == null && c.ContactId != null){
for(EntitlementContact ec:entlContacts){
if(ec.ContactId==c.ContactId){
c.EntitlementId = ec.EntitlementId;
if(c.AssetId==null && ec.Entitlement.AssetId!=null)
c.AssetId=ec.Entitlement.AssetId;
break;
}
}
}
}
} else{
List <Entitlement> entls = [Select e.StartDate, e.Id, e.EndDate,
e.AccountId, e.AssetId
From Entitlement e
Where e.AccountId in :acctIds And e.EndDate >= Today
And e.StartDate <= Today];
if(entls.isEmpty()==false){
for(Case c : Trigger.new){
if(c.EntitlementId == null && c.AccountId != null){
for(Entitlement e:entls){
if(e.AccountId==c.AccountId){
c.EntitlementId = e.Id;
if(c.AssetId==null && e.AssetId!=null)
c.AssetId=e.AssetId;
break;
}
}
}
}
}
}
}
}
I just need to match entitlement to a case based on the account in lighning. thanks in advance.
trigger DefaultEntitlement on Case (Before Insert, Before Update) {
List<Id> contactIds = new List<Id>();
List<Id> acctIds = new List<Id>();
for (Case c : Trigger.new){
if (c.EntitlementId == null && c.ContactId != null && c.AccountId != null){
contactIds.add(c.ContactId);
acctIds.add(c.AccountId);
}
}
if(contactIds.isEmpty()==false || acctIds.isEmpty()==false){
List <EntitlementContact> entlContacts =
[Select e.EntitlementId,e.ContactId,e.Entitlement.AssetId
From EntitlementContact e
Where e.ContactId in :contactIds
And e.Entitlement.EndDate >= Today
And e.Entitlement.StartDate <= Today];
if(entlContacts.isEmpty()==false){
for(Case c : Trigger.new){
if(c.EntitlementId == null && c.ContactId != null){
for(EntitlementContact ec:entlContacts){
if(ec.ContactId==c.ContactId){
c.EntitlementId = ec.EntitlementId;
if(c.AssetId==null && ec.Entitlement.AssetId!=null)
c.AssetId=ec.Entitlement.AssetId;
break;
}
}
}
}
} else{
List <Entitlement> entls = [Select e.StartDate, e.Id, e.EndDate,
e.AccountId, e.AssetId
From Entitlement e
Where e.AccountId in :acctIds And e.EndDate >= Today
And e.StartDate <= Today];
if(entls.isEmpty()==false){
for(Case c : Trigger.new){
if(c.EntitlementId == null && c.AccountId != null){
for(Entitlement e:entls){
if(e.AccountId==c.AccountId){
c.EntitlementId = e.Id;
if(c.AssetId==null && e.AssetId!=null)
c.AssetId=e.AssetId;
break;
}
}
}
}
}
}
}
}
Writting test class in salesforce is very easy . You need to understand the scenario and proper relationship between the objects and insert/update records as per the scenario .
Please check below links so that you can start writting test class .
http://manojjena20.blogspot.com/2015/06/tips-and-tricks-for-test-class.html (https://trailhead.salesforce.com/en/content/learn/modules/apex_testing/apex_testing_intro)
https://trailhead.salesforce.com/en/content/learn/modules/apex_testing/apex_testing_intro
Try to run the test class in you rorganisation , you can install code coverage calculator chrome extension which will help you to identify the line which covered .
Still you are facing problem ,please let us know we will help for sure .
Thanks ,
Manoj
Thanks for any insight
I am not very good with Apex code but looking at the trigger , I am not seeing where there is a lookup to get and set the account ID. I am thinking that acctIds is empty resulting in the trigger not grabbing the entitlement.
Is there a logging line that can be put in to check to see if acount ID is being set AND / OR is there a way for this to run after the matching that salesforce does on the email address to set the account..
Thanks in advance.