function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
nicksnicks 

custom button on list view which will update status to completed for selected(multiple) task.

I want to create a button which will allow me to update task as completed directly from the list view without opening individual task.

This button will directly update status without having to refresh entire page. This task list view will be on home page.

SubratSubrat (Salesforce Developers) 
Hello Nicks ,

To create a button that allows you to update a task's status directly from a list view without opening individual tasks, you can use the Salesforce Lightning Web Component (LWC) framework. Here's an example of how you can implement this functionality:

Create a new Lightning Web Component:

Create a new Lightning Web Component using the Salesforce CLI or Developer Console. Let's name it "UpdateTaskButton".
HTML Markup (updateTaskButton.html):
<template>
    <lightning-button label="Mark Completed" onclick={handleUpdateTask} variant="brand"></lightning-button>
</template>
JavaScript Controller (updateTaskButton.js):
import { LightningElement, api } from 'lwc';
import { updateRecord } from 'lightning/uiRecordApi';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';

export default class UpdateTaskButton extends LightningElement {
    @api taskId;

    handleUpdateTask() {
        const fields = {
            Id: this.taskId,
            Status: 'Completed' // Change the field API name if needed
        };

        updateRecord({ fields })
            .then(() => {
                this.dispatchEvent(
                    new ShowToastEvent({
                        title: 'Success',
                        message: 'Task marked as completed.',
                        variant: 'success'
                    })
                );
            })
            .catch(error => {
                this.dispatchEvent(
                    new ShowToastEvent({
                        title: 'Error',
                        message: error.body.message,
                        variant: 'error'
                    })
                );
            });
    }
}
Add the Component to the List View:

Go to the Lightning Page where the task list view is displayed (e.g., Home Page).

Edit the page layout and add the "UpdateTaskButton" component to the appropriate section of the page.

Set the taskId attribute of the component with the ID of the task you want to update.

With this setup, the button will trigger the handleUpdateTask() method when clicked. It uses the lightning/uiRecordApi module's updateRecord() function to update the task's status to "Completed". The result is displayed as a toast message using the lightning/platformShowToastEvent module.

If this helps , please mark this as Best Answer.
Thank you.
nicksnicks
@Subrat will this work if i select multiple tasks from list view and click on mark complete?