Skip to main content
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
improved formatting
Source Link
RubenDG
  • 10.3k
  • 2
  • 15
  • 24

ApexApex Class:

public with sharing class CaseList { public CaseList() { } @AuraEnabled public static List<Case> getAllCases(){ return [select Id, CaseNumber, Subject, Priority, Status from Case where Status != 'Closed']; } @AuraEnabled public static void updateStatus(String recordId){ try { Case caseObj = new Case(Id = recordId); caseObj.Status = 'New'; update caseObj; } catch (Exception e) { throw new AuraHandledException(e.getMessage()); } } } 
public with sharing class CaseList { public CaseList() { } @AuraEnabled public static List<Case> getAllCases(){ return [select Id, CaseNumber, Subject, Priority, Status from Case where Status != 'Closed']; } @AuraEnabled public static void updateStatus(String recordId){ try { Case caseObj = new Case(Id = recordId); caseObj.Status = 'New'; update caseObj; } catch (Exception e) { throw new AuraHandledException(e.getMessage()); } } } 

JSJS:

import { LightningElement } from 'lwc'; import getAllCases from '@salesforce/apex/CaseList.getAllCases'; import updateStatus from '@salesforce/apex/CaseList.updateStatus'; import {refreshApex} from '@salesforce/apex'; const columns = [ {label : 'Case Number', fieldName : 'CaseNumber', type : 'String'}, {label : 'Case Subject', fieldName : 'Subject', type : 'String'}, {label : 'Case Priority', fieldName : 'Priority', type : 'String'}, {label : 'Case Status', fieldName : 'Status', type : 'String'}, ]; export default class CaseList extends LightningElement { cols = columns; casesList = []; errorEnc = []; selectedRecord; caseListFinal = []; connectedCallback(){ getAllCases() .then(result => { this.casesList = result; }) .catch(error => { this.errorEnc = error; }) } handleRowAction(event){ if(event.detail.selectedRows.length > 0){ this.selectedRecord = event.detail.selectedRows[0].Id; alert(this.selectedRecord); } } handleClick(){ updateStatus({ recordId : this.selectedRecord }) .then(result => { alert('Success'); refreshApex(this.casesList); }) .catch(error => { alert('Error'); }) } } 
import { LightningElement } from 'lwc'; import getAllCases from '@salesforce/apex/CaseList.getAllCases'; import updateStatus from '@salesforce/apex/CaseList.updateStatus'; import {refreshApex} from '@salesforce/apex'; const columns = [ {label : 'Case Number', fieldName : 'CaseNumber', type : 'String'}, {label : 'Case Subject', fieldName : 'Subject', type : 'String'}, {label : 'Case Priority', fieldName : 'Priority', type : 'String'}, {label : 'Case Status', fieldName : 'Status', type : 'String'}, ]; export default class CaseList extends LightningElement { cols = columns; casesList = []; errorEnc = []; selectedRecord; caseListFinal = []; connectedCallback(){ getAllCases() .then(result => { this.casesList = result; }) .catch(error => { this.errorEnc = error; }) } handleRowAction(event){ if(event.detail.selectedRows.length > 0){ this.selectedRecord = event.detail.selectedRows[0].Id; alert(this.selectedRecord); } } handleClick(){ updateStatus({ recordId : this.selectedRecord }) .then(result => { alert('Success'); refreshApex(this.casesList); }) .catch(error => { alert('Error'); }) } } 

HTMLHTML:

<template> <div class="slds-m-around_medium"> <lightning-card variant="Narrow" title="Case Details" icon-name="standard:case"> <lightning-datatable key-field="id" data={casesList} show-row-number-column columns={cols} onrowselection={handleRowAction}> </lightning-datatable> <br/> <lightning-button variant="brand" label="Update Status" title="updateStatus" onclick={handleClick}></lightning-button> </lightning-card> </div> </template> 
<template> <div class="slds-m-around_medium"> <lightning-card variant="Narrow" title="Case Details" icon-name="standard:case"> <lightning-datatable key-field="id" data={casesList} show-row-number-column columns={cols} onrowselection={handleRowAction}> </lightning-datatable> <br/> <lightning-button variant="brand" label="Update Status" title="updateStatus" onclick={handleClick}></lightning-button> </lightning-card> </div> </template> 

Apex Class:

public with sharing class CaseList { public CaseList() { } @AuraEnabled public static List<Case> getAllCases(){ return [select Id, CaseNumber, Subject, Priority, Status from Case where Status != 'Closed']; } @AuraEnabled public static void updateStatus(String recordId){ try { Case caseObj = new Case(Id = recordId); caseObj.Status = 'New'; update caseObj; } catch (Exception e) { throw new AuraHandledException(e.getMessage()); } } } 

JS:

import { LightningElement } from 'lwc'; import getAllCases from '@salesforce/apex/CaseList.getAllCases'; import updateStatus from '@salesforce/apex/CaseList.updateStatus'; import {refreshApex} from '@salesforce/apex'; const columns = [ {label : 'Case Number', fieldName : 'CaseNumber', type : 'String'}, {label : 'Case Subject', fieldName : 'Subject', type : 'String'}, {label : 'Case Priority', fieldName : 'Priority', type : 'String'}, {label : 'Case Status', fieldName : 'Status', type : 'String'}, ]; export default class CaseList extends LightningElement { cols = columns; casesList = []; errorEnc = []; selectedRecord; caseListFinal = []; connectedCallback(){ getAllCases() .then(result => { this.casesList = result; }) .catch(error => { this.errorEnc = error; }) } handleRowAction(event){ if(event.detail.selectedRows.length > 0){ this.selectedRecord = event.detail.selectedRows[0].Id; alert(this.selectedRecord); } } handleClick(){ updateStatus({ recordId : this.selectedRecord }) .then(result => { alert('Success'); refreshApex(this.casesList); }) .catch(error => { alert('Error'); }) } } 

HTML:

<template> <div class="slds-m-around_medium"> <lightning-card variant="Narrow" title="Case Details" icon-name="standard:case"> <lightning-datatable key-field="id" data={casesList} show-row-number-column columns={cols} onrowselection={handleRowAction}> </lightning-datatable> <br/> <lightning-button variant="brand" label="Update Status" title="updateStatus" onclick={handleClick}></lightning-button> </lightning-card> </div> </template> 

Apex Class:

public with sharing class CaseList { public CaseList() { } @AuraEnabled public static List<Case> getAllCases(){ return [select Id, CaseNumber, Subject, Priority, Status from Case where Status != 'Closed']; } @AuraEnabled public static void updateStatus(String recordId){ try { Case caseObj = new Case(Id = recordId); caseObj.Status = 'New'; update caseObj; } catch (Exception e) { throw new AuraHandledException(e.getMessage()); } } } 

JS:

import { LightningElement } from 'lwc'; import getAllCases from '@salesforce/apex/CaseList.getAllCases'; import updateStatus from '@salesforce/apex/CaseList.updateStatus'; import {refreshApex} from '@salesforce/apex'; const columns = [ {label : 'Case Number', fieldName : 'CaseNumber', type : 'String'}, {label : 'Case Subject', fieldName : 'Subject', type : 'String'}, {label : 'Case Priority', fieldName : 'Priority', type : 'String'}, {label : 'Case Status', fieldName : 'Status', type : 'String'}, ]; export default class CaseList extends LightningElement { cols = columns; casesList = []; errorEnc = []; selectedRecord; caseListFinal = []; connectedCallback(){ getAllCases() .then(result => { this.casesList = result; }) .catch(error => { this.errorEnc = error; }) } handleRowAction(event){ if(event.detail.selectedRows.length > 0){ this.selectedRecord = event.detail.selectedRows[0].Id; alert(this.selectedRecord); } } handleClick(){ updateStatus({ recordId : this.selectedRecord }) .then(result => { alert('Success'); refreshApex(this.casesList); }) .catch(error => { alert('Error'); }) } } 

HTML:

<template> <div class="slds-m-around_medium"> <lightning-card variant="Narrow" title="Case Details" icon-name="standard:case"> <lightning-datatable key-field="id" data={casesList} show-row-number-column columns={cols} onrowselection={handleRowAction}> </lightning-datatable> <br/> <lightning-button variant="brand" label="Update Status" title="updateStatus" onclick={handleClick}></lightning-button> </lightning-card> </div> </template> 

public with sharing class CaseList { public CaseList() {

public with sharing class CaseList { public CaseList() { }  @AuraEnabled  public static List<Case> getAllCases(){   return [select Id, CaseNumber, Subject, Priority, Status from Case where Status != 'Closed'];  }  @AuraEnabled  public static void updateStatus(String recordId){   try {   Case caseObj = new Case(Id = recordId);   caseObj.Status = 'New';   update caseObj;     } catch (Exception e) {   throw new AuraHandledException(e.getMessage()); } } } 

}

JS:

import { LightningElement } from 'lwc'; import getAllCases from '@salesforce/apex/CaseList.getAllCases'; import updateStatus from '@salesforce/apex/CaseList.updateStatus'; import {refreshApex} from '@salesforce/apex';

const columns = [ {label : 'Case Number', fieldName : 'CaseNumber', type : 'String'}, {label : 'Case Subject', fieldName : 'Subject', type : 'String'}, {label : 'Case Priority', fieldName : 'Priority', type : 'String'}, {label : 'Case Status', fieldName : 'Status', type : 'String'}, ];

export default class CaseList extends LightningElement {

import { LightningElement } from 'lwc'; import getAllCases from '@salesforce/apex/CaseList.getAllCases'; import updateStatus from '@salesforce/apex/CaseList.updateStatus'; import {refreshApex} from '@salesforce/apex'; const columns = [ {label : 'Case Number', fieldName : 'CaseNumber', type : 'String'}, {label : 'Case Subject', fieldName : 'Subject', type : 'String'}, {label : 'Case Priority', fieldName : 'Priority', type : 'String'}, {label : 'Case Status', fieldName : 'Status', type : 'String'}, ]; export default class CaseList extends LightningElement { cols = columns;  casesList = [];  errorEnc = [];  selectedRecord;  caseListFinal = [];  connectedCallback(){   getAllCases()   .then(result => {   this.casesList = result;   })   .catch(error => {   this.errorEnc = error;   })  }  handleRowAction(event){   if(event.detail.selectedRows.length > 0){   this.selectedRecord = event.detail.selectedRows[0].Id;   alert(this.selectedRecord);   }  }  handleClick(){   updateStatus({   recordId : this.selectedRecord   })   .then(result => {   alert('Success');   refreshApex(this.casesList);   })   .catch(error => {   alert('Error');   }) } } 

}

HTML:


 
<template> <div class="slds-m-around_medium"> <lightning-card variant="Narrow" title="Case Details" icon-name="standard:case"> <lightning-datatable key-field="id" data={casesList} show-row-number-column columns={cols} onrowselection={handleRowAction}> </lightning-datatable> <br/> <lightning-button variant="brand" label="Update Status" title="updateStatus" onclick={handleClick}></lightning-button> </lightning-card> </div> </template> 

public with sharing class CaseList { public CaseList() {

} @AuraEnabled public static List<Case> getAllCases(){ return [select Id, CaseNumber, Subject, Priority, Status from Case where Status != 'Closed']; } @AuraEnabled public static void updateStatus(String recordId){ try { Case caseObj = new Case(Id = recordId); caseObj.Status = 'New'; update caseObj; } catch (Exception e) { throw new AuraHandledException(e.getMessage()); } } 

}

JS:

import { LightningElement } from 'lwc'; import getAllCases from '@salesforce/apex/CaseList.getAllCases'; import updateStatus from '@salesforce/apex/CaseList.updateStatus'; import {refreshApex} from '@salesforce/apex';

const columns = [ {label : 'Case Number', fieldName : 'CaseNumber', type : 'String'}, {label : 'Case Subject', fieldName : 'Subject', type : 'String'}, {label : 'Case Priority', fieldName : 'Priority', type : 'String'}, {label : 'Case Status', fieldName : 'Status', type : 'String'}, ];

export default class CaseList extends LightningElement {

cols = columns; casesList = []; errorEnc = []; selectedRecord; caseListFinal = []; connectedCallback(){ getAllCases() .then(result => { this.casesList = result; }) .catch(error => { this.errorEnc = error; }) } handleRowAction(event){ if(event.detail.selectedRows.length > 0){ this.selectedRecord = event.detail.selectedRows[0].Id; alert(this.selectedRecord); } } handleClick(){ updateStatus({ recordId : this.selectedRecord }) .then(result => { alert('Success'); refreshApex(this.casesList); }) .catch(error => { alert('Error'); }) } 

}

HTML:


 
public with sharing class CaseList { public CaseList() { }  @AuraEnabled  public static List<Case> getAllCases(){   return [select Id, CaseNumber, Subject, Priority, Status from Case where Status != 'Closed'];  }  @AuraEnabled  public static void updateStatus(String recordId){   try {   Case caseObj = new Case(Id = recordId);   caseObj.Status = 'New';   update caseObj;     } catch (Exception e) {   throw new AuraHandledException(e.getMessage()); } } } 

JS:

import { LightningElement } from 'lwc'; import getAllCases from '@salesforce/apex/CaseList.getAllCases'; import updateStatus from '@salesforce/apex/CaseList.updateStatus'; import {refreshApex} from '@salesforce/apex'; const columns = [ {label : 'Case Number', fieldName : 'CaseNumber', type : 'String'}, {label : 'Case Subject', fieldName : 'Subject', type : 'String'}, {label : 'Case Priority', fieldName : 'Priority', type : 'String'}, {label : 'Case Status', fieldName : 'Status', type : 'String'}, ]; export default class CaseList extends LightningElement { cols = columns;  casesList = [];  errorEnc = [];  selectedRecord;  caseListFinal = [];  connectedCallback(){   getAllCases()   .then(result => {   this.casesList = result;   })   .catch(error => {   this.errorEnc = error;   })  }  handleRowAction(event){   if(event.detail.selectedRows.length > 0){   this.selectedRecord = event.detail.selectedRows[0].Id;   alert(this.selectedRecord);   }  }  handleClick(){   updateStatus({   recordId : this.selectedRecord   })   .then(result => {   alert('Success');   refreshApex(this.casesList);   })   .catch(error => {   alert('Error');   }) } } 

HTML:

<template> <div class="slds-m-around_medium"> <lightning-card variant="Narrow" title="Case Details" icon-name="standard:case"> <lightning-datatable key-field="id" data={casesList} show-row-number-column columns={cols} onrowselection={handleRowAction}> </lightning-datatable> <br/> <lightning-button variant="brand" label="Update Status" title="updateStatus" onclick={handleClick}></lightning-button> </lightning-card> </div> </template> 
Source Link

How does refreshApex work alongside ImperativeApex

I used wiredApex to work alongside the update function coupled with refreshApex - it throws too many SOQL errors as expected. I changed the approach to imperative Apex but the refreshApex isn't working.

Please find the below code snippets:

Apex Class:

public with sharing class CaseList { public CaseList() {

} @AuraEnabled public static List<Case> getAllCases(){ return [select Id, CaseNumber, Subject, Priority, Status from Case where Status != 'Closed']; } @AuraEnabled public static void updateStatus(String recordId){ try { Case caseObj = new Case(Id = recordId); caseObj.Status = 'New'; update caseObj; } catch (Exception e) { throw new AuraHandledException(e.getMessage()); } } 

}

JS:

import { LightningElement } from 'lwc'; import getAllCases from '@salesforce/apex/CaseList.getAllCases'; import updateStatus from '@salesforce/apex/CaseList.updateStatus'; import {refreshApex} from '@salesforce/apex';

const columns = [ {label : 'Case Number', fieldName : 'CaseNumber', type : 'String'}, {label : 'Case Subject', fieldName : 'Subject', type : 'String'}, {label : 'Case Priority', fieldName : 'Priority', type : 'String'}, {label : 'Case Status', fieldName : 'Status', type : 'String'}, ];

export default class CaseList extends LightningElement {

cols = columns; casesList = []; errorEnc = []; selectedRecord; caseListFinal = []; connectedCallback(){ getAllCases() .then(result => { this.casesList = result; }) .catch(error => { this.errorEnc = error; }) } handleRowAction(event){ if(event.detail.selectedRows.length > 0){ this.selectedRecord = event.detail.selectedRows[0].Id; alert(this.selectedRecord); } } handleClick(){ updateStatus({ recordId : this.selectedRecord }) .then(result => { alert('Success'); refreshApex(this.casesList); }) .catch(error => { alert('Error'); }) } 

}

HTML:


Any bright ideas here?