0

I am currently stuck. All I have is a simple component that is supposed to submit the record for approval on button click. I am having trouble on load of the component ( it is being called as a quick action button ) to get the value of approvalComments not only that, but I have no idea how to call my apex function to fire to actually submit the record.

Here is my js:

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';

// Retrieve all start approval checkboxes
import START_ADDRESS from '@salesforce/schema/Account.Start_Approval__c';
import START_SSB from '@salesforce/schema/Account.Start_SSB_Approval__c';
import START_FINANCIAL from '@salesforce/schema/Account.Start_Financial_Approval__c';
import START_PAYMETHOD from '@salesforce/schema/Account.Payment_Method_Changed__c';
import START_PAYTERMS from '@salesforce/schema/Account.SAP_Payment_Terms_Changed__c';
import START_ORDERDEL from '@salesforce/schema/Account.Start_Approval__c';

// Retrieve all description fields
import DESC_ADDRESS from '@salesforce/schema/Account.Start_Address_Approval_Description__c';
import DESC_SSB from '@salesforce/schema/Account.Start_SSB_Approval_Description__c';
import DESC_FINANCIAL from '@salesforce/schema/Account.Start_Financial_Approval_Description__c';
import DESC_PAYMETHOD from '@salesforce/schema/Account.Start_PM_Approval_Description__c';
import DESC_PAYTERMS from '@salesforce/schema/Account.Start_SAP_Payment_Terms_Description__c';
import DESC_ORDERDEL from '@salesforce/schema/Account.Start_OD_Approval_Description__c';

import SAP_ID from '@salesforce/schema/Account.SAP_Id__c';
import ID_FIELD from '@salesforce/schema/Account.Id';

const fields = [ID_FIELD, START_ADDRESS, START_SSB, START_FINANCIAL, START_PAYMETHOD, START_PAYTERMS, START_ORDERDEL,
                DESC_ADDRESS, DESC_FINANCIAL, DESC_SSB, DESC_PAYMETHOD, DESC_PAYTERMS, DESC_ORDERDEL];


export default class lwcSubmitForApproval extends LightningElement {

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

    @api recordId; // Account Id

    @wire(getRecord, { recordId: '$recordId', fields })
    Account;

    approvalComments = '';


    renderedCallback(){
        this.assignApproval();
    }
    assignApproval(){
        if(getFieldValue(this.Account.data, START_ADDRESS) === true){
                this.approvalComments = getFieldValue(this.Account.data, DESC_ADDRESS);
        }

        else if(getFieldValue(this.Account.data, START_SSB) === true){
                this.approvalComments = getFieldValue(this.Account.data, DESC_SSB);
        }

        else if(getFieldValue(this.Account.data, START_FINANCIAL) === true){
                this.approvalComments = getFieldValue(this.Account.data, DESC_FINANCIAL);
        }

        else if(getFieldValue(this.Account.data, START_PAYMETHOD) === true){
               this.approvalComments = getFieldValue(this.Account.data, DESC_PAYMETHOD);
        }

        else if(getFieldValue(this.Account.data, START_PAYTERMS) === true){
                this.approvalComments = getFieldValue(this.Account.data, DESC_PAYTERMS);
        }

        else if(getFieldValue(this.Account.data, START_ORDERDEL) === true){
               this.approvalComments = getFieldValue(this.Account.data, DESC_ORDERDEL);
        }

    }
    // onclick button handler
    submitForApproval() {
        this.assignApproval();

        startApproval({ accId: this.recordId, comments: this.approvalComments });



    }
}

And here is the apex method:

public with sharing class LWCSubmitForApproval {

    /**
    ** This method initiates an approval process for the given object
    */
    @AuraEnabled(cacheable=true)
    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 need:

  1. get the value of approvalComments on load of component
  2. get the apex method to fire on the button click

please help!

1 Answer 1

0

get the value of approvalComments on load of component

Use a wire service:

import { LightningElement, wire } from 'lwc';
import getApprovalComments from '@salesforce/apex/LWCSubmitForApproval.getApprovalComments';
...
@wire(getApprovalComments,{})
approvalCommentsWired;
get approvalComments() {
    return this.approvalCommentsWired && this.approvalCommentsWired.data;
}

And for the Apex:

@AuraEnabled(cacheable=true)
public static String getApprovalComments() {
    // Get the string here and return
}

the apex method to fire on the button click

It already is (or should be). You do need to set the cacheable attribute to false, however:

@AuraEnabled(cacheable=false)
public static void approvalInit(String accId, String comment){

Otherwise you'll get odd behavior in some cases.

2
  • Thanks sfdc fox, the wiring and adding the result through apex worked great. I am however still stuck on the Submit For Approval part, as it is just lingering with nothing. submitForApproval() { startApproval({ accId: this.recordId, comment: this.approvalCommentsWired.data }) on click it is not doing anything, any way I need to wire this apex method as well at the top first? Aug 13, 2020 at 15:28
  • @TomJacobson You didn't actually handle the response from the server (startApproval({ accId:this.recordId, comment:this.approvalCommentsWired.data}).then(()=>{ ... }).catch(error=>{ alert(error) }) or something like that.
    – sfdcfox
    Aug 13, 2020 at 15:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .