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
Shruthi MN 88Shruthi MN 88 

LWC component not working

Develop a Lightning component that allows users to mass update the close date on opportunities. 
Integrate this component into the Opportunity List View page and allow for bulk updates. The opportunities should be selected from the standard list view and on clikc of a butoon iit should navigate to another page where the opportunity close date gets updated and display the result . The code is not working as expected. Opportunity close date is not egtting udpated
This is the controller:
public with sharing class OpportunityUpdateController {
 @AuraEnabled(cacheable=true)
    public static void updateOpportunities(List<Id> opportunityIds, String newCloseDate) {
        List<Opportunity> opportunitiesToUpdate = new List<Opportunity>();

        for (Id oppId : opportunityIds) {
            Opportunity opp = new Opportunity(Id = oppId);
            opp.CloseDate = Date.valueOf(newCloseDate);
            opportunitiesToUpdate.add(opp);
            system.debug('opportunitiesToUpdate:' +opportunitiesToUpdate);
        }

        update opportunitiesToUpdate;
    }
}
This is the html code

<template>
    <lightning-card title="Mass Update Close Date">
        <div class="slds-p-around_medium">
            <lightning-input type="date" label="New Close Date" value={newCloseDate} onchange={handleDateChange}></lightning-input>
            <lightning-button label="Update Close Date" onclick={updateCloseDate} variant="brand"></lightning-button>
        </div>
        <lightning-datatable
            key-field="Id"
            data={opportunities}
            columns={columns}
            selected-rows={selectedOpportunities}
            onrowselection={handleRowSelection}
        >
        </lightning-datatable>
    </lightning-card>
</template>
this is the js code
import { LightningElement, api, track } from 'lwc';
import updateOpportunities from '@salesforce/apex/OpportunityUpdateController.updateOpportunities';
const columns = [
    { label: 'Opportunity Name', fieldName: 'Name' },
    { label: 'Close Date', fieldName: 'CloseDate', type: 'date' }
];
export default class massUpdateOpportunityCloseDate extends LightningElement {
    @api opportunities;
    @track newCloseDate;
    @track columns = columns;
    @track selectedOpportunities = [];
    @track updateMessage = '';
    handleDateChange(event) {
        this.newCloseDate = event.target.value;
        console.log('New Close Date:', this.newCloseDate);
    }
    handleRowSelection(event) {
        this.selectedOpportunities = event.detail.selectedRows;
        console.log('Selected Opportunities:', this.selectedOpportunities);
    }
    updateCloseDate() {
        if (!this.newCloseDate || this.selectedOpportunities.length === 0) {
            this.updateMessage = 'Please select opportunities and provide a valid new close date.';
            return;
        }
        const opportunityIds = this.selectedOpportunities.map(opp => opp.Id);
        console.log('Opportunity IDs:', opportunityIds);
        updateOpportunities({ opportunityIds: opportunityIds, newCloseDate: this.newCloseDate })
            .then(result => {
               
                this.updateMessage = 'Opportunities updated successfully.';
                console.log('Update Result:', result);
            })
            .catch(error => {
               
                this.updateMessage = 'Error updating opportunities: ' + JSON.stringify(error);
                console.error('Update Error:', error);
            });
    }
}
 
Pallavi Soni 32Pallavi Soni 32
Hi @Shruthi MN 88,

You can do this even more easily using mass quick action instead of using custom LWC.
A mass quick action is a quick action that we can add to the search layout of an object. After creating a mass quick action, we can perform mass updates on up to 100 records in a list view.

Steps to do:-
1. From Setup, click the Object Manager tab.
2. Search and select the Opportunity object.
3. Click on Button, Links, and Actions
4. Click on the New Action button
5. Enter Action Information
  • Select “Update a Record” in Action Type
  • Enter action label and name - Update Close Date
  • Enter description
  • Enter success message
  • Click on the Save button
6. After saving you will see the Action layout, add the fields that you want users to update.
7. Click on Save.

To set up mass quick action:-
1. Click on the List View Button Layout
2. Select 'Update Close Date' (Name of action) quick action in the List View Action in the Lightning Experience field.
3. Click on Save.
Now you will see the  'Update Close Date' button on the list view page,
Here you can select the opportunities for which you want to update the close date ---> after selecting the opportunities click on 'Update Close Date' button  ---> you will see one popup modal and there you can select a date to update the 'Close Date' field -> Click on Save, you will see all the selected Opportunity's Close Date will update. 

For more info -> https://sfdclesson.com/2022/09/30/update-bulk-records-with-mass-quick-action-button/ (https://sfdclesson.com/2022/09/30/update-bulk-records-with-mass-quick-action-button/)

Let me know if it helps you.
Thanks