1

When I click a button in LWC to update a picklist value, I get this error https://i.stack.imgur.com/JDk93.jpg. How can I fix this?

HTML

<template>
    <lightning-card>
        <template if:true={opportunities.data}>
            <lightning-datatable
                key-field="Id"
                data={opportunities.data}
                columns={columns}
                onrowaction={handleRowAction}
            >
           </lightning-datatable>
       </template>
    </lightning-card>
</template>

APEX

  public with sharing class OpportunityController {
@AuraEnabled(cacheable=true)
        public static void closeOpportunity(Id closeId){
            Opportunity opp = new Opportunity(Id = closeId);
            opp.StageName = 'Closed Won';
            List<Opportunity> res = new List<Opportunity>();
            res.add(opp);
            insert res;
        }
}

  

JS

        import { LightningElement, api, wire } from 'lwc';
        import NAME_FIELD from '@salesforce/schema/Opportunity.Name';
        import AMOUNT_FIELD from '@salesforce/schema/Opportunity.Amount';
        import closeOpportunity from '@salesforce/apex/OpportunityController.closeOpportunity';
        import { NavigationMixin } from "lightning/navigation";
        
        const COLUMNS = [
            {
              label: "Edit",
              type: "button",
              typeAttributes: {
                  label: "Edit",
                  name: "editOpportunity",
                  variant: "brand"
              }
          }
        ];
        
        export default class MiscRecord extends NavigationMixin(LightningElement) {
            columns = COLUMNS;
//Used another Apex call to fetch details into the opportunities variable.

                handleRowAction(event) {
                  if (event.detail.action.name === "editOpportunity") {
                    closeOpportunity(closeId= event.detail.row.Id).then(data=>{
                      this.error = null;
                    } 
                    ).catch(error =>{
                      this.error = error;
                    }
                    )
                }
              }
        }
5
  • when you open developer tools, is there showing an error? also, could you add the code for the picklist? Nov 25, 2022 at 13:00
  • @EliveltonWalter no error in developer tools. the picklist is a default in-built one.
    – user275494
    Nov 25, 2022 at 13:09
  • you cannot perform DML in cacheable = true methods. And you have to pass parameter from js like this closeId : event.detail.row.Id Nov 25, 2022 at 13:12
  • @KaranSharma making these two changes removes the error message, but the object does not get updated, old value being displayed
    – user275494
    Nov 25, 2022 at 13:21
  • 1
    If you get data from wire method then you have to use refreshApex to update the data in datatable when your operation in done. Nov 25, 2022 at 13:22

1 Answer 1

3

The issue is you cannot perform dml operation in

@AuraEnabled(cacheable=true) methods

so change it to

@AuraEnabled

And calling of your method will look like

closeOpportunity(closeId:event.detail.row.Id).then(data=>{
                      this.error = null;
                    } 
1
  • 1
    {closeId:event.detail.row.Id}
    – user275494
    Nov 28, 2022 at 10:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .