[SalesForce] Lightning Web Component LWC Quick Action button Lead Ownership

Hope to get some help resolving my issue. I have a quick action button linked to a lightning component (aura). The button needs to change the lead owner to the user.

Basically there is no need for the window to open. probably a success message would be good but first is to get it working and change the owner.

Issue 1: When I click on the button it opens an empty window and it doesn't do anything.

Answer: Use the code below and it works.

Issue 2: I am getting both in debug logs and dev console => "List has no rows for assignment to SObject".
It looks like it's having issues passing the id to the apex controller. I tried with both leadId: 'recordId' and leadId: '$recordId' but both giving the same error. any ideas?

Answer: need to remove (cacheable=true) from the ChangeLeadOwner method.can't do DML inside a cacheable method.

Issue 3: how to update the owner without refreshing the page?

Answer: I managed to fix it by using getRecordNotifyChange([{recordId: this.recordId}]);

Update: Below code works perfectly. The button changes the owner to the user, success toast message is shown and the owner gets updated on the same page without a need to refresh.

Aura Component linked to button:

<aura:component implements="flexipage:availableForAllPageTypes, force:lightningQuickActionWithoutHeader,force:hasRecordId" access="global" >
    <aura:html tag="style">
        .slds-modal {
        visibility: hidden;
        display: none;
        }
        .slds-backdrop {
        display: none;
        }        
    </aura:html>
    <c:leadTakeOwnership recordId="{!v.recordId}" onclose="{!c.closeModal}"></c:leadTakeOwnership>
</aura:component>   

Aura controller Js:

({
    closeModal : function(component, event, helper) {
        $A.get("e.force:closeQuickAction").fire();
    }
})

Apex Controller:

public with sharing class LeadTakeOwnershipController {
    @AuraEnabled
    public static Lead ChangeLeadOwner(Id leadId) {
        Lead l    = [SELECT Id, OwnerId FROM Lead WHERE Id = :leadId];
        l.OwnerId = UserInfo.getUserId();
        Update l;
        Return l;
    }
}

LWC js:

import { LightningElement , api} from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import {  getRecordNotifyChange } from 'lightning/uiRecordApi';
import ChangeLeadOwner from '@salesforce/apex/LeadTakeOwnershipController.ChangeLeadOwner';


export default class LeadTakeOwnership extends LightningElement {
    @api recordId ;
    
    renderedCallback() {
    
        ChangeLeadOwner({
            leadId: this.recordId  
        }) 

        .then((lead) => {
           
            const event = new ShowToastEvent({
                title: 'Success',
                message: 'Lead Ownersip changed successfully',
                variant: 'success',
            });
            this.dispatchEvent(event);

            getRecordNotifyChange([{recordId: this.recordId}]);

        })
     
        .catch((error) => {
            
            const event = new ShowToastEvent({
                title: "Error on update",
                message: error.body.message,
                variant: "error",
                mode: 'sticky',
            });
            this.dispatchEvent(event);
        });
    }
  
}

LWC html:

<template>
    
</template>

Best Answer

Aura Component linked to button:

<aura:component implements="flexipage:availableForAllPageTypes, force:lightningQuickActionWithoutHeader,force:hasRecordId" access="global" >
    <aura:html tag="style">
        .slds-modal {
        visibility: hidden;
        display: none;
        }
        .slds-backdrop {
        display: none;
        }        
    </aura:html>
    <c:leadTakeOwnership recordId="{!v.recordId}" onclose="{!c.closeModal}"></c:leadTakeOwnership>
</aura:component>   

Aura controller Js:

({
    closeModal : function(component, event, helper) {
        $A.get("e.force:closeQuickAction").fire();
    }
})

Apex Controller:

public with sharing class LeadTakeOwnershipController {
    @AuraEnabled
    public static Lead ChangeLeadOwner(Id leadId) {
        Lead l    = [SELECT Id, OwnerId FROM Lead WHERE Id = :leadId];
        l.OwnerId = UserInfo.getUserId();
        Update l;
        Return l;
    }
}

LWC js:

import { LightningElement , api} from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import {  getRecordNotifyChange } from 'lightning/uiRecordApi';
import ChangeLeadOwner from '@salesforce/apex/LeadTakeOwnershipController.ChangeLeadOwner';


export default class LeadTakeOwnership extends LightningElement {
    @api recordId ;
    
    renderedCallback() {
    
        ChangeLeadOwner({
            leadId: this.recordId  
        }) 

        .then((lead) => {
           
            const event = new ShowToastEvent({
                title: 'Success',
                message: 'Lead Ownersip changed successfully',
                variant: 'success',
            });
            this.dispatchEvent(event);

            getRecordNotifyChange([{recordId: this.recordId}]);

        })
     
        .catch((error) => {
            
            const event = new ShowToastEvent({
                title: "Error on update",
                message: error.body.message,
                variant: "error",
                mode: 'sticky',
            });
            this.dispatchEvent(event);
        });
    }
  
}

LWC html:

<template>
    
</template>