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 attachments 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

Please dont create dupe posts!

 

Here is what you need to do

Replace the 

 

attach = new Attachment();
attach2 = new Attachment();

 With

 

 if(attach!=null && attach.body!= null )upload();
if(attach2!=null && attach2.body!= null )upload2();

 I already commented on this on your earlier post, although that was a description, but here is the code

Emmanuel CoronaEmmanuel Corona
Greetings Avidev!

When i tried to test i get two errors:

Class.myArcoExtension.upload: line 44, column 1
Class.myArcoExtension.saveArco: line 24, column 1

:(