function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Emmanuel CoronaEmmanuel Corona 

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();
}
}

Avidev9Avidev9
 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() {
       Savepoint sp = Database.setSavePoint();  
       try {
             insert(webarco);
             upload();
             upload2();
         } catch (System.DMLException e) {
             ApexPages.addMessages(e);
/*reset the values*/
webarco.Id = null;
attach = new Attachment();
attach2 = new Attachment();
Database.rollback(sp); 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

Emmanuel CoronaEmmanuel Corona
Wow thank you avidev! let me test it :D
Avidev9Avidev9
I started having doubt on the code may not work :(
Emmanuel CoronaEmmanuel Corona
lol Avidev i tested it and keeps working equal.
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.
Avidev9Avidev9
You missed my comment I gave you a task : " 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(attachment body and name) in saveAcro method and give a relevant error message."
Jana ToddddJana Todddd
The app features a wide range of features including group chat, group video calling, direct messages to individuals or groups of people, calendar reminders for meetings or other events and even the ability to share photos and documents because now a days you need day 2 lateral flow test (https://www.covidtests.co.uk/products/certifly-day-2-lateral-flow-test) to travel anywhere so its online system which is best option. Users can also set up custom notifications so they don't miss any important message from their contacts. Connected also provides users with access to their contacts' social media accounts so they can stay up-to-date on what's happening in their lives.