You need to sign in to do that
Don't have an account?
Assistance required to write test for Apex Trigger code
All,
We do not have an Sales Force developer on staff. We found sample code that does exactly what we need it to do, but we do not know how to get it tested, and implimented into our production environment. Is there sample test class for the following code?
trigger SupportPlanEntitlement on Case (Before Insert, Before Update) { /* When a case is created, auto-populate with the active support plan If the Entitlement Name is not set then, check to see if the Contact on the Case has an active Entitlement and select the first one. If not then check to see if the Account on the Case has an active Entitlement. */ 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){ /* Added check for active entitlement */ List <EntitlementContact> entlContacts = [Select e.EntitlementId,e.ContactId,e.Entitlement.AssetId From EntitlementContact e Where e.ContactId in:contactIds And e.Entitlement.Type = 'Support Plan' 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; } } // end for } } // end for } 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.Type = 'Support Plan' 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; } } // end for } } // end for } } } // end if(contactIds.isEmpty()==false) }
All Answers
Hi
try this code
@isTest
private class SupportPlanEntitlemen_test{
static testmethod void SupportPlanEntitlemen_test(){
Accoumt acc = new Account();
acc.name='test';
//insert other which are mandotory
.
.
insert acc;
like this insert contact and case and EntitlementContact
in case insert the values according to if conditions
}}
Hi SRS8,
I appreciate the code sample. Thank you very much for the reply.
Is this complete? It looks like there are sections where I need to add test cases.
That is the problem. I do not know what to test? We are not Sales Force developers, and have never seen Apex code before. We saw this code sample, and it accomplished exactly what we needed, but now we are dead in the water with trying to figure out what we need to do for testing.
Where do we add this code, in a new Apex trigger?
A new class?
HI
u have to go for new class i.e setup->develop->Apex classes->new and paste this code but this code is not enough to get the sufficient code coverage but as i said u follow and write more code in it.
Let me know if any queries.
Regards,
Rajesh.
Thank you. I will give it a try.
Once I have the test class created, I'm not sure how it tests the Apex Trigger code.
is the code I provided above supposed to be in an Apex trigger, or a class?
What should my test class be testing for, Creating an account, contact, entitlement?
Does it need to test all the classes that exist in our environment?
Thanks,
Scott
Hi
U just create the test class and after saving it run it, it will show which class/trigger are covered in it.And search ur trigger in that page and see the percentage beside it and click on it, it show which lines are covered that r in blue in color and the color red in color has not covered.
Let me know if any queries.
Regards,
Rajesh.
Hi,
That's just it, I do not seem my trigger listed when I run the test class. Does that mean my test code doesn't have any referense to the trigger?
I'm drowning here. Can you give me a sample of what my test code should include based on the apex trigger code I provided above.
I've searched for examples, but since I am not a developer, I do not understand the syntax or logic needed. I appreciate all your help so far. Thanks.
Hi
Try this code in seperate new class
@isTest
private class SupportPlanEntitlement_test{
static testmethod void SupportPlanEntitlement_test();
{
Account acc = new Account();
acc.name='test';
// insert required fields like this
insert acc;
Contact con= new Contact();
con.Lastname='test5';
//like this insert required fields
insert con;
Id contactIds;
Id acctIds;
Asset as = new Asset();
as.//insert required fields
insert as;
Case cs = new Case();
cs.contactIds=con.ContactId;
cs.acctIds=acc.AccontId;
cs.EntitlementId=here i dont what is EntitlementId u insert it.
insert cs;
try to insert fields in the above according to if conditions so that it will cover.
}}
Let me if any query.
Regards,
Rajesh.
Thanks, I thought your example would get me going in the right direction. I appreciate the help, but since I am not a developer, I do not understand what is missing, or what to add to make a valid test.
I had compile errors on a few lines, so I commented them out to see what would happen. I was able to save and run a test. It didn't run on my Apex Trigger. Frankly I dont understand how Sales Force would know this test was for my Trigger. This is so frustrating. I thank you for your time.
This is test test code I have so far. I don't even know if it's doing anything. I read the examples, and I can't seem to make the connection between what is explained, and what I need to do accomplish.
Hi
Can plz send ur gmail id so i can add to my google+ and can know what r custom fields u having and which r required
Regards,
Rajesh.