You need to sign in to do that
Don't have an account?
VPrakash
Test method data question?
Hi all,
I am writing a test method for a trigger below
trigger PartnumberCheck on FAQPartNumber__c (before insert, before update,after insert,after update) { if(!PriceForceUtility.isUpdateFromclass){ if(Trigger.isBefore){ set<String> partnumber = new set<String>(); Agreement_Line_Item__c[] ali2check = [select id,name,part_number__c,product__c,Product_Class__c from Agreement_Line_Item__c where Agreement_Line_Item__c.Agreement__c=:trigger.new[0].agreement__c]; system.debug(ali2check); for(Agreement_Line_Item__c ali:ali2check){ if(ali.Product_Class__c == 'Instrument') partnumber.add(ali.part_number__c); } for (Integer i=0; i<Trigger.size; i++) { system.debug(trigger.new[i]); if(!(partnumber.contains(trigger.new[i].part_number__c))){ trigger.new[i].adderror('Cannot Add the part number not on the agreements instrument list'); } } } FAQ__c[] faqstoupdate = new FAQ__c[]{}; if(Trigger.isAfter){ FAQ__c faq = [select id,name,part_number__c from FAQ__c where id=:trigger.new[0].FAQ__c]; string partnumber = ''; FAQPartNumber__c[] faqparts = [select id,name,part_number__c from FAQPartNumber__c where FAQPartNumber__c.FAQ__c=:faq.Id]; integer i=0; for(FAQPartNumber__c faqpart:faqparts){ if(i==0){ partnumber = faqpart.part_number__c; }else{ partnumber = partnumber+'^'+faqpart.part_number__c; } i++; } faq.Part_Number__c= partnumber; faqstoupdate.add(faq); } update faqstoupdate; } }
My test code is as follows
@isTest private class PartNumberCheckTest { static testmethod void testfaqpartnumber(){ Agreement__c agreement = new Agreement__c(); agreement.Name = 'Test Agreement'; agreement.Agreement_Type__c = 'GPO'; agreement.Agreement_Code__c = 'AMERINET'; agreement.Start_Date__c = date.newInstance(2007,1,1); agreement.End_Date__c = date.newInstance(2012,12,31); insert agreement; Agreement_Line_Item__c ALI2 = new Agreement_Line_Item__c(); ALI2.Agreement__c = agreement.id; ALI2.Part_Number__c = '6E119'; insert ALI2; id recordtypeid = [select id from RecordType where name='Part Number Record Type' and SobjectType ='Agreement_Line_Item__c'].id; Agreement_Line_Item__c ALI1 = new Agreement_Line_Item__c(); ALI1.Agreement__c = agreement.id; ALI1.Part_Number__c = 'A71456' ; ALI1.RecordTypeId = recordtypeid; ALI1.Agreement_Price__c = 12345; insert ALI1; FAQ__c faq = new FAQ__c(Agreement__c = agreement.id,Water__c = 'Yes',Sonicator__c = 'Yes',AirfareIncluded__c = 'No',Ups__c = 'No',TradeInAllowed__c='No',AdditionalTraining__c='0',Linearity__c='No'); insert faq; system.debug(ALI1); PriceForceUtility.isUpdateFromclass = false; FAQPartNumber__c faqpart = new FAQPartNumber__c(FAQ__C = faq.Id,product__c = '01t30000001V0hg'); insert faqpart; }
My trigger is invoked from the test method and the trigger works on the data from different objects (Agreement_Line_Item__c), which I retrieved using a query in the trigger. This trigger works fine in the UI but when trying to write a test class for this trigger I am not getting test data(back to the trigger) to validate in the trigger using the query.
Please look in to this and let me know where I am doing wrong
Thanks In advance
VPrakash
Those variables in your test method are in-memory copies of the records. When they pass through the trigger, they are passed by copy, which means that when you return from the trigger, those changes aren't reflected in the memory variables in your test method. You have to retrieve them from the database to see the results.
This generally takes the form of:
thank you for the reply, but my question was can I retrieve the inserted test data in the trigger from the query?
I apologize, as I must have mis-read your question. Yes, you may query any data that you insert as part of the test, since it exists as a part of the transaction until the end of the test. You simply need to query for it as you would any other record. Obviously, you'll need to know which record you're supposed to be querying, which looks like it would be a query that depends on the ID of record in your agreement variable. Aside from my previous statement, it is true that the "ID" field will be populated at the end of the insert call, so you can query using that ID value to find the relevant test data.
Thanks again for the clarification, but when I use this particular query in this trigger I can retrieve only certain fields such as record id and name and I am not able to get the other fields such as part_number__c,product__c etc., for the test method data. Is there any particular reason for this scenario. Details below:
the query from the trigger
the debug statement above shows
15:29:45.412 (5412312000)|METHOD_ENTRY|[9]|System.debug(ANY)
15:29:45.412 (5412562000)|USER_DEBUG|[9]|DEBUG|(Agreement_Line_Item__c:{Name=ALI-0016931, Id=a0mQ0000002Sz8OIAS}, Agreement_Line_Item__c:{Name=ALI-0016932, Id=a0mQ0000002Sz8PIAS})
15:29:45.412 (5412602000)|METHOD_EXIT|[9]|System.debug(ANY)
you can see only record id and name are retrieved. But if I debug the same agreement line item record in the test method will show as
15:29:45.295 (5295708000)|METHOD_ENTRY|[29]|System.debug(ANY)
15:29:45.295 (5295865000)|USER_DEBUG|[29]|DEBUG|Agreement_Line_Item__c:{RecordTypeId=012Q00000008i8dIAA, Agreement_Price__c=12345,, Agreement__c=a0kQ0000000yVjBIAU, Part_Number__c=A71456}
15:29:45.295 (5295894000)|METHOD_EXIT|[29]|System.debug(ANY)
As you see here the data is inserted in the test method but not retrieved in the trigger, What might be the reason?
Thanks,
VPrakash
Hi all,
Can you please look into this issue and let me know where I am doing wrong.
Thanks in advance
VPrakash