You need to sign in to do that
Don't have an account?
Error with attachment and missing fields
Greetings!
I have a trouble with my class but i don't know how to handle the error...
Here is the scenario... if the user fill all the fields but if don't upload file i get a message error that body and header are missing and are not required, next if they select the files but some field is wrong the page refresh and now the files are missing so if the user try to upload them again we get a error message about the id
I'll rally appreciate your help!
public class myArcoExtension
{
private final Arco__c webarco;
public myArcoExtension(ApexPages.StandardController stdController)
{
webarco = (Arco__c)stdController.getRecord();
attach = new Attachment();
attach2 = new Attachment();
}
public myArcoExtension(){}
public PageReference saveArco()
{
try {
insert(webarco);
upload();
upload2();
}
catch(System.DMLException e)
{
ApexPages.addMessages(e);
return null;
}
PageReference p = Page.informacion_enviada;
p.setRedirect(true);
return p;
}
public String parentId {get;set;}
public Attachment attach {get;set;}
public ApexPages.Pagereference upload()
{
attach.ParentId = webarco.id;
insert attach;
return new ApexPages.Standardcontroller(attach).view();
}
public String parentId2 {get;set;}
public Attachment attach2 {get;set;}
public ApexPages.Pagereference upload2()
{
attach2.ParentId = webarco.id;
insert attach2;
return new ApexPages.Standardcontroller(attach2).view();
}
/* Clase testMethod para validar el código y cubrir el porcentaje de cobertura */
public static testMethod void myTest() {
Arco__c arco = new Arco__c();
arco.Nombre__c = 'Emmanuel';
arco.Apellidos__c = 'Corona';
arco.Email__c = 'ecorona@mcmtelecom.com.mx';
insert arco;
//Create the controller
ApexPages.StandardController sc = new ApexPages.StandardController(arco);
//Create the instances of the controller
myArcoExtension myPageTest = new myArcoExtension();
myArcoExtension myPageTestSC = new myArcoExtension(sc);
myPageTestSC.saveArco();
Attachment attachment = new Attachment();
attachment.Name = 'Unit Test Attachment';
attachment.Body = Blob.valueOf('Unit Test Attachment Body');
myPageTestSC.attach = attachment;
myPageTestSC.upload();
Attachment attachment2 = new Attachment();
attachment2.Name = 'Unit Test Attachment 2';
attachment2.Body = Blob.valueOf('Unit Test Attachment Body 2');
myPageTestSC.attach2 = attachment2;
myPageTestSC.upload2();
}
}
1.Well your code doesnt check whether user has uploaded a file or not and directly does and insert hence you get an error that is catched by try catch "body and header are missing and are not required". You can have a null check in saveAcro method and give a relevant error message.
2."next if they select the files but some field is wrong the page refresh and now the files are missing so if the user try to upload them again we get a error message about the id": this is because you have try catch inplace and hence records get inserted to the point where exception occurs. TO control this you have to add rollback support which I already did in your code
What do you think could be the problem?
I'm trying to review it, but maybe we can do something different, maybe we can launch a pop-up window with the error and a button to go back and reload the page to clear all content and let the user fill it again...
But i think that the focus is to handle the upload, i mean... i think that the save method on a error refresh the page and maybe on that refresh is the trouble... i don´t know where SFD is setting the attachemnt fields as required.