Thanks, Khan. Actually I was looking for a lightning web component solution. I did find one, though!
Here's the html for my component which is just a lightning-datatable that lists out opportunties. What I was trying to accomplish was select one or more rows and then update each selected record with an apex method. Following the update, I wanted to refresh the datatable. This code does that.
import { LightningElement,track, wire} from 'lwc';
import getAllOpps from '@salesforce/apex/GetAllOpportunities.getAllOpps';
import updateOpportunityNames from '@salesforce/apex/UpdateOpportunities.updateOpportunityNames';
import { refreshApex } from '@salesforce/apex';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
export default class DatatableEx12 extends LightningElement {
@track columns = [{
label: 'Opportunity name',
fieldName: 'Name',
type: 'text',
sortable: true
},
{
label: 'Stage Name',
fieldName: 'StageName',
type: 'text',
sortable: true
},
{
label: 'Close date',
fieldName: 'CloseDate',
type: 'date',
sortable: true
}
];
wiredDataResult;
@track error;
@track data ;
@wire(getAllOpps)
wiredOpps(result) {
this.wiredDataResult = result;
if (result.data) {
this.data = result.data;
}
}
getSelectedName() {
const selectedRows = this.template.querySelector('lightning-datatable').getSelectedRows();
const selectedIds = [];
// Display that fieldName of the selected rows
for (let i = 0; i < selectedRows.length; i++){
selectedIds.push(selectedRows[i].Id);
console.log("You selected: " + selectedRows[i].Id);
}
updateOpportunityNames({ids: selectedIds}).then(result => {
console.log("Opps updated!" + result)
// Display fresh data in the datatable
this.selectedRows = [];
return refreshApex(this.wiredDataResult);
}).catch(error => {
this.dispatchEvent(
new ShowToastEvent({
title: 'Error creating record',
message: error.message,
variant: 'error'
})
);
});
}
}
GetAllOpportunities.cls
public with sharing class GetAllOpportunities {
@AuraEnabled(cacheable=true)
public static List<Opportunity> getAllOpps() {
return [SELECT Id, Name ,StageName, CloseDate ,Type ,Probability,Account.Name from Opportunity limit 10];
}
}
UpdateOpportunities.cls
public with sharing class UpdateOpportunities {
@AuraEnabled()
public static void updateOpportunityNames(String[] ids) {
List<Opportunity> opportunitiesToUpdate = new List<Opportunity>();
List<Opportunity> selectedOpportunities = [SELECT Id, Name FROM Opportunity WHERE Id in :ids];
Integer i = 0;
for (Opportunity opportunity : selectedOpportunities){
opportunity.StageName = 'Closed';
opportunitiesToUpdate.add(opportunity);
i++;
}
update opportunitiesToUpdate;
}
}
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.
Thanks, Khan. Actually I was looking for a lightning web component solution. I did find one, though!
Here's the html for my component which is just a lightning-datatable that lists out opportunties. What I was trying to accomplish was select one or more rows and then update each selected record with an apex method. Following the update, I wanted to refresh the datatable. This code does that.
import { LightningElement,track, wire} from 'lwc';
import getAllOpps from '@salesforce/apex/GetAllOpportunities.getAllOpps';
import updateOpportunityNames from '@salesforce/apex/UpdateOpportunities.updateOpportunityNames';
import { refreshApex } from '@salesforce/apex';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
export default class DatatableEx12 extends LightningElement {
@track columns = [{
label: 'Opportunity name',
fieldName: 'Name',
type: 'text',
sortable: true
},
{
label: 'Stage Name',
fieldName: 'StageName',
type: 'text',
sortable: true
},
{
label: 'Close date',
fieldName: 'CloseDate',
type: 'date',
sortable: true
}
];
wiredDataResult;
@track error;
@track data ;
@wire(getAllOpps)
wiredOpps(result) {
this.wiredDataResult = result;
if (result.data) {
this.data = result.data;
}
}
getSelectedName() {
const selectedRows = this.template.querySelector('lightning-datatable').getSelectedRows();
const selectedIds = [];
// Display that fieldName of the selected rows
for (let i = 0; i < selectedRows.length; i++){
selectedIds.push(selectedRows[i].Id);
console.log("You selected: " + selectedRows[i].Id);
}
updateOpportunityNames({ids: selectedIds}).then(result => {
console.log("Opps updated!" + result)
// Display fresh data in the datatable
this.selectedRows = [];
return refreshApex(this.wiredDataResult);
}).catch(error => {
this.dispatchEvent(
new ShowToastEvent({
title: 'Error creating record',
message: error.message,
variant: 'error'
})
);
});
}
}
GetAllOpportunities.cls
public with sharing class GetAllOpportunities {
@AuraEnabled(cacheable=true)
public static List<Opportunity> getAllOpps() {
return [SELECT Id, Name ,StageName, CloseDate ,Type ,Probability,Account.Name from Opportunity limit 10];
}
}
UpdateOpportunities.cls
public with sharing class UpdateOpportunities {
@AuraEnabled()
public static void updateOpportunityNames(String[] ids) {
List<Opportunity> opportunitiesToUpdate = new List<Opportunity>();
List<Opportunity> selectedOpportunities = [SELECT Id, Name FROM Opportunity WHERE Id in :ids];
Integer i = 0;
for (Opportunity opportunity : selectedOpportunities){
opportunity.StageName = 'Closed';
opportunitiesToUpdate.add(opportunity);
i++;
}
update opportunitiesToUpdate;
}
}
Is this good idea to use this script in content based website that hosted on WordPress? I want to use for my site of musical hunter (https://musicalhunter.com/). Kindly Guide me regarding this.
Here's the html for my component which is just a lightning-datatable that lists out opportunties. What I was trying to accomplish was select one or more rows and then update each selected record with an apex method. Following the update, I wanted to refresh the datatable. This code does that.
HTML
JS
GetAllOpportunities.cls
UpdateOpportunities.cls
All Answers
Greetings to you!
You can use force:refreshView to request a page refresh. Try like this:
And don't forget to add below handler in your component:
Or you can call init method in handler action: <aura:handler event="force:refreshView" action="{!c.doInit}" />
Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
Apex:
Component:
Controller:
CSS:
I hope it helps you.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.
Thanks and Regards,
Khan Anas
Here's the html for my component which is just a lightning-datatable that lists out opportunties. What I was trying to accomplish was select one or more rows and then update each selected record with an apex method. Following the update, I wanted to refresh the datatable. This code does that.
HTML
JS
GetAllOpportunities.cls
UpdateOpportunities.cls
After geting response from apex controller you have to call doInit method again inside that method in which you call apex controller.
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi