You need to sign in to do that
Don't have an account?
Testing Error: Parent ID not getting set
I'm currently working to create a test class and am running into a brick wall. The test is returning an error after the first run and not iterating through. My error is:
System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [ParentId]: [ParentId]
Class.saleIO.ioJJInit: line 336, column 9 Class.saleIOTest.IOTest: line 38, column 15 External entry point
The relevant code is below. What am I doing wrong?
Opportunity a = [SELECT Id, Account.Id, Account.Owner.Name, Contact__r.Name, Account.BillingStreet, Account.Name,
Account.BillingCity, Contact__r.Email, Contact__r.Fax, Contact__r.Phone,
Account.BillingState, Account.BillingPostalCode, Category__c,
Notes__c, Display_Ad_Size_JJ__c, Display_Ad_Size_TR__c, Display_Ad_Size__c,
Art_Date__c, Art_Info__c, Bill_To__c, Insertion_Dates__c, Insertion_Dates_TR__c,
Amount, Color__c, New_Advertiser__c
FROM Opportunity
WHERE id = :ApexPages.currentPage().getParameters().get('id')];
public PageReference ioJJInit()
{
String xmlContent = getJJXmlString(a);
Attachment attachment = new Attachment();
attachment.Name = 'JJ_' + a.Account.Name + '_' + formatted + '.xfdf';
attachment.ParentId = a.Account.Id;
attachment.Body = Blob.valueof(xmlContent);
insert attachment; /* line 336, location of the error */
PageReference orgPage = new PageReference('/' + a.Account.id);
orgPage.setRedirect(true);
return orgPage;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | @isTest private class saleIOTest { static testMethod void IOTest() { String [] jjSizes = new String[] {'1/16', '1/2 Horizontal', '1/2 Vertical', '1/4 Horizontal', '1/4 Vertical', '1/8 Horizontal', '1/8 Vertical', '3/4', '3/8', 'Full', 'Jr.', 'TOC', null}; String [] trSizes = new String[] {'1/6', '1/3 Vertical', '1/2 Horizontal', '1/2 Vertical', '1/4 Vertical', '2/3', 'Full', 'Double Truck', null}; String [] mpSizes = new String[] {'1/2 Horizontal', '1/2 Vertical', '1/3 Vertical', '1/4 Vertical', '1/8 Horizontal', 'Full', '1x1', '1x2', '1x3', '2x1', '2x2', '2x3', '3x1', '3x2', '3x3', null}; Date testDay = Date.parse('11/10/2011'); User u = [SELECT Id FROM User WHERE name = 'Jared Baker']; Account testOrg = new Account (Name = 'Test', BillingStreet = '3580 Wilshire', BillingState = 'CA', BillingPostalCode = '90010'); insert testOrg; Contact testCon = new Contact (FirstName = 'Joe', LastName = 'Tester', Email = 'jotes@jewishjournal.com', Fax = '(213) 555-0789', Phone = '(213) 555-0790', Account = testOrg); insert testCon; Opportunity testOpp = new Opportunity (Name = 'Test Op', Owner = u, Account = testOrg, Contact__c = testCon.ID, Category__c = 'Rockin Test', Art_Date__c = testDay, Notes__c = 'notes test', New_Advertiser__c = true, Bill_to__c = 'Agency', StageName = 'Won', CloseDate = testDay, Color__c = 'One'); insert testOpp; System.debug('Organization ID=' + testOpp.Account.ID); for(Integer i = 0; i<13; i++) { testOpp.Display_Ad_Size_JJ__c = jjSizes[i]; update testOpp; if(testOpp.Display_Ad_Size_JJ__c != null) { System.debug('Organization ID=' + testOpp.Account.ID); PageReference jjRef = new PageReference('/apex/saleIO?id=' + testOpp.id); Test.setCurrentPage(jjRef); saleIO testJJIO = new saleIO(); testJJIO.ioJJInit(); } } testOpp.Notes__c = null; testOpp.New_Advertiser__c = false; testOpp.Bill_to__c = 'Advertiser'; testOpp.Insertion_Dates__c = '11/10'; update testOpp; } } |
UPDATE: Found a partial solution...unfortunately, this new addition doesn't allow testing of "insert attachment;", and I'm capped at 96%. Any way I can test that part of the code, or is 96% coverage going to just have to do (I know it is technically enough)?
if (!Test.isRunningTest()){//requires proper blob to insert an Attachment, unable to generate during testing
insert attachment;
}
Hi @jaredbaker, I receive the same error! Here are snippets of both my Class and Test Class. My Class fails to insert the attachment because "Required fields are missing: [ParentId]". Does anyone know how I can alter/change my Test Class so that I can Insert Attachments with ParentID?
Snippet from my Class