You need to sign in to do that
Don't have an account?
Sai Subhash 6
Test class for apex controller
public class UploadAttachment
{
@AuraEnabled
public static Id saveChunk(Id parentId, String fileName, String base64Data, String contentType, String fileId) {
// check if fileId id ''(Always blank in first chunk), then call the saveTheFile method,
// which is save the check data and return the attachemnt Id after insert,
// next time (in else) we are call the appentTOFile() method
// for update the attachment with reamins chunks
if (fileId == '') {
fileId = saveTheFile(parentId, fileName, base64Data, contentType);
} else {
appendToFile(fileId, base64Data);
}
return Id.valueOf(fileId);
}
public static Id saveTheFile(Id parentId, String fileName, String base64Data, String contentType) {
base64Data = EncodingUtil.urlDecode(base64Data, 'UTF-8');
ServiceAppointment sa = [SELECT id,ParentRecordId,Work_Order__c FROM ServiceAppointment WHERE id =:parentId];
workorder wo=[select id,Uploaded_Invoice__c from workorder where id=:sa.Work_Order__c];
Attachment oAttachment = new Attachment();
oAttachment.parentId = sa.ParentRecordId;
oAttachment.Body = EncodingUtil.base64Decode(base64Data);
oAttachment.Name = 'Uploaded Image_' +''+fileName;
oAttachment.ContentType = contentType;
wo.Uploaded_Invoice__c =true;
update wo;
insert oAttachment;
return oAttachment.Id;
}
private static void appendToFile(Id fileId, String base64Data) {
base64Data = EncodingUtil.urlDecode(base64Data, 'UTF-8');
Attachment a = [
SELECT Id, Body
FROM Attachment
WHERE Id =: fileId
];
String existingBody = EncodingUtil.base64Encode(a.Body);
a.Body = EncodingUtil.base64Decode(existingBody + base64Data);
update a;
}
@AuraEnabled
public static String return_ServiceAppointment(String parentId)
{
/*Schema.DescribeSObjectResult result = ServiceAppointment.SObjectType.getDescribe();
String objectIdPrefix = result.getKeyPrefix();
String returnprtab ='/'+objectIdPrefix+'/o';*/
ServiceAppointment sa = [SELECT id,ParentRecordId,Work_Order__c FROM ServiceAppointment WHERE id =:parentId];
workorder wo=[select id,Uploaded_Invoice__c from workorder where id=:sa.Work_Order__c];
return sa.Work_Order__c;
}
}
{
@AuraEnabled
public static Id saveChunk(Id parentId, String fileName, String base64Data, String contentType, String fileId) {
// check if fileId id ''(Always blank in first chunk), then call the saveTheFile method,
// which is save the check data and return the attachemnt Id after insert,
// next time (in else) we are call the appentTOFile() method
// for update the attachment with reamins chunks
if (fileId == '') {
fileId = saveTheFile(parentId, fileName, base64Data, contentType);
} else {
appendToFile(fileId, base64Data);
}
return Id.valueOf(fileId);
}
public static Id saveTheFile(Id parentId, String fileName, String base64Data, String contentType) {
base64Data = EncodingUtil.urlDecode(base64Data, 'UTF-8');
ServiceAppointment sa = [SELECT id,ParentRecordId,Work_Order__c FROM ServiceAppointment WHERE id =:parentId];
workorder wo=[select id,Uploaded_Invoice__c from workorder where id=:sa.Work_Order__c];
Attachment oAttachment = new Attachment();
oAttachment.parentId = sa.ParentRecordId;
oAttachment.Body = EncodingUtil.base64Decode(base64Data);
oAttachment.Name = 'Uploaded Image_' +''+fileName;
oAttachment.ContentType = contentType;
wo.Uploaded_Invoice__c =true;
update wo;
insert oAttachment;
return oAttachment.Id;
}
private static void appendToFile(Id fileId, String base64Data) {
base64Data = EncodingUtil.urlDecode(base64Data, 'UTF-8');
Attachment a = [
SELECT Id, Body
FROM Attachment
WHERE Id =: fileId
];
String existingBody = EncodingUtil.base64Encode(a.Body);
a.Body = EncodingUtil.base64Decode(existingBody + base64Data);
update a;
}
@AuraEnabled
public static String return_ServiceAppointment(String parentId)
{
/*Schema.DescribeSObjectResult result = ServiceAppointment.SObjectType.getDescribe();
String objectIdPrefix = result.getKeyPrefix();
String returnprtab ='/'+objectIdPrefix+'/o';*/
ServiceAppointment sa = [SELECT id,ParentRecordId,Work_Order__c FROM ServiceAppointment WHERE id =:parentId];
workorder wo=[select id,Uploaded_Invoice__c from workorder where id=:sa.Work_Order__c];
return sa.Work_Order__c;
}
}
@isTest
public class UploadAttachmentTest{
public static testmethod void saveChunk(){
Account acc= new Account(Name='test account');
insert acc;
workorder wo=new workorder(StartDate=system.today(),EndDate=system.today()+8,Accountid=acc.id);
insert wo;
ServiceAppointment sa=new ServiceAppointment(ParentRecordId=wo.id,Work_Order__c=wo.id,EarliestStartTime=datetime.newInstance(2018, 5, 15, 12, 30, 0),DueDate=datetime.newInstance(2018, 9, 15, 12, 30, 0));
Insert sa;
blob file= Blob.valueOf('sample text');
Attachment objAttachment=new Attachment();
objAttachment.Body= file;
objAttachment.Name = 'test Attachment';
objAttachment.ParentId = sa.ParentRecordId;
insert objAttachment;
UploadAttachment.saveChunk(sa.ParentRecordId,'testAttachment',file.toString(),'jpg',objAttachment.Id);
//UploadAttachment.saveChunk(sa.ParentRecordId,'testAttachment',file.toString(),'jpg','');
}
}