0

I have table with checkbox which display records. I want to update the comment field for the records which i have selected. It was working and updated in the database. But the component is not refreshed after using refresh apex. If i use eval("$A.get('e.force:refreshView').fire();"); checkbox values are unchecked but data is not reflected in the table(but when i do query it shows the updated value also with window.location.reload). Without reloading page how can my table gets refresh

Lwc.html

    <template>
    <lightning-card title="Contact Info">
        <div class="slds-p-around_x-small">
        <template if:true={condata}>
            <div class="slds">

            <table class = "slds-table slds-table_cell-buffer slds-table_bordered">
                <thead>
                    <tr class="">
                        <th class="slds-size_3-of-10" scope="col">  
                            <lightning-input onchange={hanldeSelectAll} type="checkbox"></lightning-input>
                        </th>
                        <th class="slds-size_3-of-10" scope="col">  
                            <div class="slds-truncate" title="Contact">Contact</div>
                        </th>
                        <th class="slds-size_3-of-10" scope="col">  
                            <div class="slds-truncate" title="Contact Owner">Contact Owner</div>
                        </th>                       
                        <th class="slds-size_3-of-10" scope="col">  
                            <div class="slds-truncate" title="Comment ">Comment </div>
                        </th>
                    </tr>
                </thead>
                <tbody>
                    <template for:each = {condata} for:item = "item" for:index = "indexVar">
                        <tr key = {item.conRecord} class="slds-hint-parent" data-my-id="claimId2">
                            <td data-label="Select All">
                                <div  title="Select All">
                                    <lightning-input type="checkbox" value={item.ContactComment} data-id={item.Id}></lightning-input>
                                  </div>
                            </td>
                            <td class="slds-size_3-of-10" data-label="Contact">
                                <div  title="Contact">
                                        <lightning-formatted-url value={item.conRecord} label={item.Name} target="_self" ></lightning-formatted-url>
                                </div>
                            </td>
                            <td class="slds-size_3-of-10" data-label="Contact Owner">
                                <div  title="Contact Owner">
                                    <lightning-formatted-text value={item.Owner} ></lightning-formatted-text>
                                </div>
                            </td>
                            
                            <td class="slds-size_3-of-10" data-label="Comment">
                                <div  title="Comment">
                                    <lightning-input key = {item.Id} type="text" value={item.ContactComment} data-id={item.Id} onchange={commentOnChange}
                                    message-when-value-missing ="Please enter comment" required="true"></lightning-input>
                                </div>
                            </td>
                        </tr>
                    </template>
                </tbody>
            </table>
            <lightning-button variant="brand" label="Save" title="Save" onclick={handleClick} class="slds-m-left_x-small"></lightning-button>
        </div>
        </template>
        </div>
    </lightning-card>
</template>

lwc.js

import { LightningElement, wire, track } from 'lwc';
import getContacts from '@salesforce/apex/ContactController.getContacts';
import saveConRecords from '@salesforce/apex/ContactController.saveConRecords';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import { refreshApex } from '@salesforce/apex';

export default class contactUpdate extends LightningElement {

    
    @track condata = [];
    @track showSpinner = false;
    @wire(getContacts)
    opp({error, data}) {
        if(data) {
            let conList = [];
            data.forEach((conVal) => 
                let con = {};
                con.Id =  conVal.Id;
                con.Name =  conVal.Name;
                con.conRecord = '/' + conVal.Id;
                con.ContactComment  = conVal.Comment__c;            

                conList.push(con);
            });

            this.condata = conList;
            //alert('onload@@'+JSON.stringify(this.olidata));
        }
        else if(error) {
            window.console.log(error);
        }
    }
    
    handleClick(event) {
        this.showSpinner = true;
        this.selectedCons = [];
        let selectedRows = this.template.querySelectorAll('lightning-input');
        for(let i = 0; i < selectedRows.length; i++) {
            if(selectedRows[i].checked && selectedRows[i].type === 'checkbox' && selectedRows[i].dataset.id!=null) {
                this.selectedCons.push({
                    sObjectType: 'ContactRole__c',
                    Comment__c: selectedRows[i].value,
                    Id: selectedRows[i].dataset.id
                })
            }
        }

        saveConRecords({ conrecords: this.selectedCons})
        .then(() => {
            this.showSpinner = false;
            refreshApex(this.condata);
            eval("$A.get('e.force:refreshView').fire();");
        })
        .catch(error => {
            
        });
       
    }

     
    hanldeSelectAll(event) {
        alert('Called hanldeSelectAll');
        let selectedRows = this.template.querySelectorAll('lightning-input');
        
        for(let i = 0; i < selectedRows.length; i++) {
            if(selectedRows[i].type === 'checkbox') {
                selectedRows[i].checked = event.target.checked;
            }
        }
    }

  
    commentOnChange(event){
        let element = this.condata.find(ele  => ele.Id === event.target.dataset.id);
        element.ContactComment = event.target.value;
        this.condata = [...this.condata];
    }
}

Please help me !!

Your Answer

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

Browse other questions tagged or ask your own question.