You need to sign in to do that
Don't have an account?
Susan Connor
Field Integrity Exception error
I am getting the following error when trying to use a multiple attachment upload controller. Any ideas? Am I missing some obvious place where I need to be referring to the Parent ID? The Parent is the custom object Notes__c (objct ID 01IF0000001xT2j)
Thank you in advance! Finally stepping away at 4 in the morning before I hurt myself or someone else ... :)
Here is my controller:
And this is the test class:
Thank you in advance! Finally stepping away at 4 in the morning before I hurt myself or someone else ... :)
Here is my controller:
/* * Controller for multi attachment component */ public with sharing class MultiAttachmentController { public MultiAttachmentController(ApexPages.StandardController controller) { } // the parent object it public Id sobjId {get; set;} // list of existing attachments - populated on demand public List<Attachment> attachments; // list of new attachments to add public List<Attachment> newAttachments {get; set;} // the number of new attachments to add to the list when the user clicks 'Add More' public static final Integer NUM_ATTACHMENTS_TO_ADD=5; // constructor public MultiAttachmentController() { // instantiate the list with a single attachment newAttachments=new List<Attachment>{new Attachment()}; } // retrieve the existing attachments public List<Attachment> getAttachments() { // only execute the SOQL if the list hasn't been initialised if (null==attachments) { attachments=[select Id, ParentId, Name, Description from Attachment where parentId='01IF0000001xT2j']; } return attachments; } // Add more attachments action method public void addMore() { // append NUM_ATTACHMENTS_TO_ADD to the new attachments list for (Integer idx=0; idx<NUM_ATTACHMENTS_TO_ADD; idx++) { newAttachments.add(new Attachment()); } } // Save action method public void save() { List<Attachment> toInsert=new List<Attachment>(); for (Attachment newAtt : newAttachments) { if (newAtt.Body!=null) { newAtt.parentId='01IF0000001xT2j'; toInsert.add(newAtt); } } insert toInsert; newAttachments.clear(); newAttachments.add(new Attachment()); // null the list of existing attachments - this will be rebuilt when the page is refreshed attachments=null; } // Action method when the user is done public PageReference done() { // send the user to the detail page for the sobject return new PageReference('/' + sobjId); } }
And this is the test class:
@isTest private class MultiAttachmentController_TEST { private static testMethod void testController() { Account acc=new Account(Name='Unit Test'); insert acc; MultiAttachmentController controller=new MultiAttachmentController(); controller.sobjId='01IF0000001xT2j'; System.assertEquals(0, controller.getAttachments().size()); System.assertEquals(1, controller.newAttachments.size()); controller.addMore(); System.assertEquals(6 , controller.newAttachments.size()); controller.save(); System.assertEquals(0, controller.getAttachments().size()); System.assertNotEquals(null, controller.done()); } }
Please mark this as the best answer if this helped.
You are correct, the 01IF0000001xT2j is what I find in the URL when I go to the Create > Objects > Notes page.
Am I pulling that ID from the wrong place?
A couple of things you might want to look at (common errors that I make when I'm bleary eyed):
-
Can I ask why you're hard-coding the parent record id? There's a chance that this could be a mis-key, or that "Notes__c" record id has since been deleted.
-
Is the "sobjId" variable in your controller the record id? How are you passing that into the controller and could you alter your code to dynamically assign that variable to the parentId?
-
I can't find in your controller where you are populating your "NewAttachments" list with the following fields for insert: Body, Name
I don't know if this will fix your issue, but I'd want to cover those off.