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
Edward Foreaker 2Edward Foreaker 2 

Could Anyone Assist Me With Unit Testing This? Thanks.

public with sharing class ehsSignatureExtensionController {

private final My_Work_Order__c ehs;

public ehsSignatureExtensionController(ApexPages.StandardController controller) {
    ehs = (My_Work_Order__c)controller.getRecord();
}

@RemoteAction public static RemoteSaveResult saveSignature(Id ehsId, String signatureBody) {
    Attachment a = new Attachment(ParentId=ehsId, name='Signature.png', ContentType='image/png', Body=EncodingUtil.base64Decode(signatureBody));
    Database.saveResult result = Database.insert(a,false);
    RemoteSaveResult newResult = new RemoteSaveResult();
    newResult.success = result.isSuccess();
    newResult.attachmentId = a.Id;
    newResult.errorMessage = result.isSuccess()?'':result.getErrors()[0].getMessage();
    return newResult;
}

public class RemoteSaveResult {
    public Boolean success;
    public Id attachmentId;
    public String errorMessage;
}

public pageReference saveSignature(){
    pageReference page = new PageReference('https://cunning-fox-3wdgx9-dev-ed.lightning.force.com/lightning/r/My_Work_Order__c/'+ehs.id +'/view');
    page.setRedirect(true);
    return page;

}
}
Best Answer chosen by Edward Foreaker 2
Maharajan CMaharajan C
Hi Edward,

Are you looking for test class ?  if yes please refer the below one.
 
@isTest
public class ehsSignatureExtensionControllerTest {
    
    @isTest static void verifyAccSave(){
        
        My_Work_Order__c wo = new My_Work_Order__c();
        wo.Name = 'Test Order';
		// Add all the mandatory fields for workorder creation.
        insert wo;
        Test.setCurrentPage(Page.ehsSignaturePage); // Use you VF page name here
        
        ApexPages.StandardController accountController = new ApexPages.StandardController(wo);
        
        ehsSignatureExtensionController ace = new ehsSignatureExtensionController(accountController);
        
        Test.startTest();
        ehsSignatureExtensionController.saveSignature(wo.Id,'Test');
        ace.saveSignature();
        Test.stopTest();
        
    }
}

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi Edward,

Are you looking for test class ?  if yes please refer the below one.
 
@isTest
public class ehsSignatureExtensionControllerTest {
    
    @isTest static void verifyAccSave(){
        
        My_Work_Order__c wo = new My_Work_Order__c();
        wo.Name = 'Test Order';
		// Add all the mandatory fields for workorder creation.
        insert wo;
        Test.setCurrentPage(Page.ehsSignaturePage); // Use you VF page name here
        
        ApexPages.StandardController accountController = new ApexPages.StandardController(wo);
        
        ehsSignatureExtensionController ace = new ehsSignatureExtensionController(accountController);
        
        Test.startTest();
        ehsSignatureExtensionController.saveSignature(wo.Id,'Test');
        ace.saveSignature();
        Test.stopTest();
        
    }
}

Thanks,
Maharajan.C
This was selected as the best answer
Edward Foreaker 2Edward Foreaker 2
Good Morning Maharajan, 

I just wanted to say thank you very much for the unit test, it validated and deployed all thanks to you. I marked your answer as the best. I really appreciate your help. The saveSignature method is working for desktop only. I am trying to get the saveSignature() redirect to work on the mobile lightning app also. Could you see below and advise if the visualforce script/apex method can be adjusted to redirect to the lightning mobile object(tablet/mobile form factor) as it properly does on desktop? I will Post a second question called "How To: Save button method for Desktop and Mobile.Thanks.", this way I can mark your answer as best for a second question if you know the answer. Nonetheless, thank you my friend.

ehsSignatureExtensionController Extension - Desktop Page Ref Part of the Apex 
public pageReference saveSignature(){
    pageReference page = new PageReference('https://company.lightning.force.com/lightning/r/work_order__c/'+ehs.id +'/view');
    page.setRedirect(true);
    return page;

}


Visualforce Page saveSignature() Script
function saveSignature() {

    var image = canvas.toDataURL().split(',')[1];
    ehsSignatureExtensionController.saveSignature(ehsId,image,handleResult);
}

function handleResult(result,event) {
    if(result.success) {
        window.top.location.href='/'+ehsId;
    } else {
        alert('Error: '+result.errorMessage);
    }
}