1

I have a button on the parent lwc. When called, it will call a method on the child lwc to perform data update via apex method. Once done, it calls refreshApex to refresh the data on the lightning datatable. RefreshApex will not trigger for some reason.

Now, if I use the child's save button to save the changes, refreshApex will trigger and data will update without any issues.

Appreciate your help!!!

enter image description here

PARENT COMPONENT

</div>
            <footer class="slds-modal__footer">
                <lightning-button label="Save" variant="brand" onclick={saveModal} class="slds-m-left_x-small"></lightning-button>
                <lightning-button label="Cancel" variant="neutral" onclick={closeModal} class="slds-m-left_x-small"></lightning-button>
            </footer>

PARENT JS

saveModal() {
    //firing a child method
    this.template.querySelector("c-reuse-license-key-table").testHandleAction();
}

CHILD COMPONENT

<template if:true={quotelinelist}>
        <div class="slds-grid_align-end" style="height: 500px; overflow: auto;">
            <lightning-datatable key-field="Id"
                                 data={quotelinelist}
                                 columns={columns}
                                 onsave= {testHandleAction}
                                 oncellchange= {handleRowAction}
                                 draft-values={fldsItemValues}
                                 hide-checkbox-column="true">  <!--suppress-bottom-bar="true"-->
            </lightning-datatable>
        </div>
    </template>

CHILD JS

@wire(getQuoteLinesList,{quoteId: '$parentRecordId', actionType: '$actionTypeForWire'})
cons(result) {
    console.log('check result 1 ', result)
    this.wiredDataResult = result;
    if(result.error) {
        console.log('inside wire error')
        //this.quotelineList = undefined;
    }
    else {
        console.log('inside else error')
        this.quotelinelist = result.data;
    }
}

handleRowAction(event) {
    lKeyDraftList.push(event.detail.draftValues);
}

@api async testHandleAction() {

    let strOriginalKeyValues = JSON.stringify(qlInList);
    let strlKeyDraftList = JSON.stringify(lKeyDraftList);
    let strDraftValue = [];

    lKeyDraftList.forEach(function(c){
        console.log('check c', c);
        c.forEach(function(c2){
            console.log('check c2', c2);
            strDraftValue.push(c2);
        });

    });

    await updateQuoteLines({IdToObjectForDraftValues : JSON.stringify(strDraftValue)})  //, IdToObjectForOriginalValues: strOriginalKeyValues
        .then(result => {
            const toast = new ShowToastEvent({
                title: 'Success',
                message: 'Records Updated Successfully!!',
                variant: 'success'
            });
            this.dispatchEvent(toast);
            this.actionTypeForWire = 'update';

            return refreshApex(this.wiredDataResult).then(() => {
                console.log('after refresh apex')
                this.fldsItemValues = [];
            });
        })
        .catch(error => {
            console.log('error');
            const toast = new ShowToastEvent({
                title: 'Error',
                message: 'An Error Occurred!!' + error.message,
                variant: 'error'
            });
            this.dispatchEvent(toast);
        }).finally(() => {
        console.log('inside final', this.fldsItemValues);
    });
}

APEX Controller

    @AuraEnabled(cacheable=true)
public static List<SBQQ__QuoteLine__c> getQuoteLinesList(Id quoteId, String actionType) {
    List<SBQQ__QuoteLine__c> qLineList = new List<SBQQ__QuoteLine__c>();

    if(quoteId != null){
        qLineList = [SELECT Id,Name,SBQQ__ProductName__c,SBQQ__Quantity__c,Reused_License_Key__c
                            FROM SBQQ__QuoteLine__c
                            WHERE SBQQ__RequiredBy__c != NULL
                            AND SBQQ__Quote__c = :quoteId
                            ORDER BY SBQQ__ProductName__c];
    }

    Boolean callLPUtility = true;

    for(SBQQ__QuoteLine__c ql : qLineList) {
        if(ql.Reused_License_Key__c != null) {
            callLPUtility = false;
            break;
        }
    }

    if(callLPUtility == true && actionType == 'begin') {
        LicenseProductUtility.toMapLicenseKeyForRenewal(qLineList, getSubscriptionsList(quoteId));
    }
    return qLineList;
}

@AuraEnabled
public static void updateQuoteLines(String IdToObjectForDraftValues) {  //String IdToObjectForOriginalValues

    List<Object> items = (List<Object>) JSON.deserializeUntyped(IdToObjectForDraftValues);
    List<SBQQ__QuoteLine__c> qlList = new List<SBQQ__QuoteLine__c>();
    List<Map<String, Object>> theJsonMapList = new List<Map<String, Object>>();

    for(Object itemObj : items) {
        theJsonMapList.add((Map<String, Object>) itemObj);
    }

    for(Object o : theJsonMapList){

        SBQQ__QuoteLine__c q = new SBQQ__QuoteLine__c();
        q.Id = ((Map<String, Object>) o).get('Id').toString();
        q.Reused_License_Key__c = ((Map<String, Object>) o).get('Reused_License_Key__c').toString();
        qlList.add(q);
    }

    Savepoint sp = Database.setSavepoint();

    if(!qlList.isEmpty()){
        try {
            update qlList;
        } catch (Exception e) {
            Database.rollback(sp);
            System.debug('An exception occurred updateQuoteLines: ' + e.getMessage());
        }
    }
}

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.