How to do DML when using Lightning Web Component for Quick Action in Salesforce?

How to do DML when using Lightning Web Component for Quick Action in Salesforce?

Sample Code:

Apex Class:

public with sharing class AccountController {  
    
    @AuraEnabled  
    public static String createAttachment( Id accountId ) {  
        
        String result = 'Success';
        
        try {
            
            Attachment a = new Attachment();
            a.ParentId = accountId;
            a.Name = String.valueOf( System.now() );
            a.ContentType = 'text/plain';
            a.Body = Blob.valueOf( 'Testing' );
            insert a;
            
        } Catch ( Exception e ) {
            
            result = e.getMessage();
            
        }
        
        return result;
        
    }  
    
}

Lightning Web Component:

HTML:

<template>    
</template>

JavaScript:

import { LightningElement, api, wire } from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import createAttachment from '@salesforce/apex/AccountController.createAttachment';  

export default class AccountQuickAction extends LightningElement {

    @api recordId;

    @api async invoke() {

        console.log( "Inside Invoke Method" );
        console.log( "Record Id is " + this.recordId );

        await createAttachment( { accountId: this.recordId } )    
            .then( result => {  
                
                if ( result == 'Success' ) {

                    this.dispatchEvent(
                        new ShowToastEvent({
                            title: 'Attachment Created',
                            message: 'Attachment Created Successfully!!!',
                            variant: 'success'
                        }),
                    );

                } else {

                    this.dispatchEvent(
                        new ShowToastEvent({
                            title: 'Attachment Creation Failed',
                            message: result,
                            variant: 'error'
                        }),
                    );

                }
    
            } )  
            .catch( error => {  

                this.dispatchEvent(
                    new ShowToastEvent({
                        title: 'Attachment Creation Failed',
                        message: 'System Error Occurred',
                        variant: 'error'
                    }),
                );
    
            } );      

    }

}

JS-meta.xml:

<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>52.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
        <target>lightning__RecordAction</target>
        <target>lightning__RecordPage</target>
    </targets>
    <targetConfigs>
        <targetConfig targets="lightning__RecordAction">
            <actionType>Action</actionType>
            <objects>
                <object>Account</object>
            </objects>
        </targetConfig>
    </targetConfigs>
</LightningComponentBundle>

Leave a Reply