Can you share the code. As the onew which you shared were screenshots it would be difficult to try the same code and provide the test class for you. If you share the code as the answer I can try the test class and share the same with you.
<script> function test(){ var inputFile = document.querySelectorAll("[id*='inputFile']")[0]; if(inputFile.value.length === 0) return false;
return true; } </script>
<script> function checkFileSize(event , id ) { if(event.target.files[0].size > 2**19) { alert('File must be less than 500kb'); //**Clear input** document.getElementById(id).value =''; } } </script>
find your test class with 80% coverage. In this test class, we covered all the methods and we don't need to cover catch the part.
don't forget to mark it as the best answer.
Thank you
All Answers
Can you share the code. As the onew which you shared were screenshots it would be difficult to try the same code and provide the test class for you. If you share the code as the answer I can try the test class and share the same with you.
Thanks,
Apex Class
public class CaseControllerExt {
private final Case acase;
public Blob afile {get; set;}
public String contentType {get; set;}
public String fileName {get; set;}
public CaseControllerExt(ApexPages.StandardController stdController){
this.acase=(Case)stdController.getRecord();
acase.Status = 'New';
acase.Origin = 'IT Request Form';
}
public PageReference createCaseWithAttachment() {
try{
insert(acase);
}catch(System.DMLException e){
ApexPages.addMessages(e);
}
if(afile!=null){
Attachment attach=new Attachment();
attach.Body=afile;
attach.Name=filename;
attach.ContentType=contentType;
attach.ParentID=acase.id;
try {
insert(attach);
} catch(System.DMLException e) {
ApexPages.addMessages(e);
return null;
}
}
PageReference p = Page.Confirmpage;
p.setRedirect(true);
return p;
}
}
-------------------------------------------------------------------
Test Class
@isTest public
class CaseControllerExtTest{
testMethod static void insertCase(){
Case cs = new Case(Status='New',Origin = 'IT Request Form');
caseList.add(cs);
ApexPages.StandardController sc = new ApexPages.StandardController(cs);
CaseControllerExt testCase = new CaseControllerExt(sc); testCase.createCaseWithAttachment();
testCase.afile = Blob.valueOf('Test Data');
testCase.filename = test;
testCase.ContentType='text/plain';
testCase.ParentID = cs.Id;
}
}
Can you also paste the code for Confirmpage page as well. Because there is reference for that page in apex class and test class.
Thanks,
<apex:page standardController="Case" extensions="CaseControllerExt" > <apex:form >
<apex:pageBlock >
<apex:selectList styleClass="bloc" style="color:crimson;width:0px;font-size:0;border:0px;background:#f2f2f2;" size="0" value="{!Case.Subject}">
<apex:selectOption itemValue="Attached File Regarding My Case" itemLabel="I Need An Import"/>
</apex:selectList>
<label>Your Name</label> <apex:inputField value="{!Case.Description}" html-rows="1" html-cols="20" style="width: 200px !important;" required="true" />
<div style="width: 500px; background: "> </div>
<apex:inputFile accept=".xlsx,.xls,image/*,.doc, .docx,.ppt, .pptx,.txt,.pdf" filename="{!fileName}" contentType="{!contentType}" value="{!afile}" id="inputFile" onchange="checkFileSize(event, id)"/>
<div style="width: 500px; background: "> </div>
<apex:commandButton value="Submit" id="submit" action="{!createCaseWithAttachment}" onclick="return test();" />
<script>
function test(){
var inputFile = document.querySelectorAll("[id*='inputFile']")[0];
if(inputFile.value.length === 0)
return false;
return true;
}
</script>
<script>
function checkFileSize(event , id ) {
if(event.target.files[0].size > 2**19) {
alert('File must be less than 500kb');
//**Clear input**
document.getElementById(id).value ='';
}
}
</script>
</apex:pageBlock>
</apex:form>
</apex:page>
find your test class with 80% coverage. In this test class, we covered all the methods and we don't need to cover catch the part.
don't forget to mark it as the best answer.
Thank you
Can you try the below test class. It gives you 88% code coverage.
If this solution helps, Please mark it as best answer.
Thanks,