Rest API Callout from LWC Salesforce- using row action in Lightning data table
ฝัง
- เผยแพร่เมื่อ 16 ก.ค. 2023
- This video is the demo of making callout from lightning data table of salesforce LWC where callout will happen after clicking the row action button and then toast message will render.
#salesforce #lwc #restapi #callouts #salesforcetrailhead #salesforceinterviewquestions #usedcase #tutorial #tutorials #salesforceapex
Great session. Thank you.
In case of any query you can reach out to me at linkedin.com/in/gagan-anand-328b8ba2
Can you share the code please if possible?
give me your email id , I will share the code
import { LightningElement,wire } from 'lwc';
import retrieveAccounts from '@salesforce/apex/DataController.retrieveAccounts';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import { NavigationMixin } from 'lightning/navigation';
import postToSAP from '@salesforce/apex/DataController.callSAPSystem';
const actions = [
{ label: 'View', name: 'view' , initialWidth:300},
{ label: 'Edit', name: 'edit' , initialWidth:300},
];
const columns = [
{ label: 'Id', fieldName: 'Id',initialWidth:100 },
{ label: 'Name', fieldName: 'Name', sortable: true ,initialWidth:150 },
{ label: 'Type', fieldName: 'Type', sortable: true , initialWidth:150
},
{
label: 'Outside Action',
type: 'button',
initialWidth:150,
typeAttributes: {
label: 'Send to SAP',
name: 'sendtosap',
disabled: {fieldName : 'iscloneDisabled'},
value: 'viewBtn' }
},
{ label: 'Action',
type: 'action',
initialWidth:300,
typeAttributes: { rowActions: actions },
},
];
export default class Datatablepostaction extends NavigationMixin(LightningElement) {
columns = columns;
items;
error;
@wire(retrieveAccounts)
wiredAccounts({ error, data }) {
if (data) {
this.items = data;
this.columns = columns;
this.error = undefined;
} else if (error) {
this.error = error;
this.items = undefined;
this.showToast(this.error, 'Error', 'Error'); //show toast for error
}
}
handleRowAction(event) {
console.log('testinsidehandler');
const actionName = event.detail.action.name;
const row = event.detail.row;
console.log('actionaname'+actionName);
console.log('row'+row.Id);
console.log('row'+this.row);
switch ( actionName ) {
case 'view':
this[NavigationMixin.Navigate]({
type: 'standard__recordPage',
attributes: {
objectApiName: 'Account',
recordId: row.Id,
actionName: 'view'
}
});
break;
case 'edit':
this[NavigationMixin.Navigate]({
type: 'standard__recordPage',
attributes: {
recordId: row.Id,
objectApiName: 'Account',
actionName: 'edit'
}
});
break;
}
if(actionName=='sendtosap'){
console.log('inside sendtosap');
postToSAP({ AccountId: row.Id })
.then(result => {
this.error = undefined;
})
.catch(error => {
this.error = error;
})
this.showToast();
}
}
showToast() {
const event = new ShowToastEvent({
title: 'Callout succesfull !!',
message: 'Data has been sent to the SAP',
variant: 'success',
mode: 'dismissable'
});
this.dispatchEvent(event);
}
}
Can you share code pls in git