Notes
HTML
<template>
<!-- Success or Error message -->
<template if:true={message}>
<p class="slds-text-color_success">{message}</p>
</template>
<template if:true={errorMessage}>
<p class="slds-text-color_error">{errorMessage}</p>
</template>
</template>
JavaScript
import { LightningElement, api } from 'lwc';
import { updateRecord } from 'lightning/uiRecordApi';
import ACCOUNT_OBJECT from '@salesforce/schema/Account';
import ACTIVE_FIELD from '@salesforce/schema/Account.Active__c';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
export default class ActivateAccount extends LightningElement {
@api recordId; // The record ID passed from the Quick Action
// Upon component load, update the Account's Active__c field
connectedCallback() {
this.activateAccount();
}
activateAccount() {
const fields = {};
fields[ACTIVE_FIELD.fieldApiName] = 'Yes'; // Setting Active__c to 'Yes'
fields['Id'] = this.recordId; // Account record ID
const recordInput = { fields };
updateRecord(recordInput)
.then(() => {
// Show success message
this.dispatchEvent(
new ShowToastEvent({
title: 'Success',
message: 'Account activated successfully!',
variant: 'success',
})
);
})
.catch((error) => {
// Show error message
this.dispatchEvent(
new ShowToastEvent({
title: 'Error',
message: error.body.message,
variant: 'error',
})
);
});
}
}
Meta XML
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>56.0</apiVersion> <!-- Make sure the API version matches the Salesforce version you're using -->
<isExposed>true</isExposed>
<targets>
<target>lightning__RecordAction</target> <!-- This makes it available as a Quick Action -->
</targets>
<targetConfigs>
<targetConfig targets="lightning__RecordAction">
<objects>
<object>*</object> <!-- Or specify Account if you want it to be specific -->
</objects>
</targetConfig>
</targetConfigs>
</LightningComponentBundle>