0

I have basically a simple custom made button that needs to auto submit the account for approval like standard functionality button but with specific comments added.

import { LightningElement, track, api, wire } from 'lwc';
import { getRecord, getFieldValue } from 'lightning/uiRecordApi';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import { updateRecord } from 'lightning/uiRecordApi';

import startApproval from '@salesforce/apex/LWCSubmitForApproval.approvalInit';
import getApprovalComments from '@salesforce/apex/LWCSubmitForApproval.getApprovalComments';

export default class lwcSubmitForApproval extends LightningElement {

    @track startApproval;
    @track disabled = false;
    @track error;

    @api recordId; // Account Id

    @wire(getApprovalComments, { accId: '$recordId' })
    approvalCommentsWired;

    get approvalComments(){
        return this.approvalCommentsWired && this.approvalCommentsWired.data;
    }

    // onclick button handler
    submitForApproval() {
        console.log(this.approvalCommentsWired.data);
        startApproval({ accId: this.recordId, comment: this.approvalCommentsWired.data }).then(result =>{
            console.log(result);
            if(result){
                this.showToast("Success");
                this.showSpinner = false;
            }
            this.showSpinner = false;
        }).catch(error => {
            this.showToast("Error");
            console.log(error);
            this.error = error;
            this.showSpinner = false;
        })

    }
}

+++ apex class

/**
 * Created by 13ent on 8/13/2020.
 */

public with sharing class LWCSubmitForApproval {

    /**
    ** This method initiates an approval process for the given object
    */
    @AuraEnabled(cacheable=false)
    public static void approvalInit(String accId, String comment){
        Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
        req.setComments(comment);
        req.setObjectId(accId);
        Approval.ProcessResult result;
        try{
            // submit the approval request for processing
            result = Approval.process(req);
        }catch(Exception e){
            System.debug('No approval process has been setup yet.');
        }
    }
}

I am not getting any response from clicking the button calling the submitForApproval() method. Any idea why? How to call my apex class so that it works?

3

Your method returns void (no value), and you're swallowing errors (never do this!). You should throw a new exception to invoke the error handler if you have an error:

}catch(Exception e){
    throw new AuraHandledException(e.getMessage());
}

This will allow you to detect if there's an error.

Note that if then is called, then there was no fatal exception. There's no need to check if(result) for a void return type, as there will never be a result.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.