0

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>
7
  • Welcome to SE! You might need to remove (cacheable=true) from the ChangeLeadOwner method. I don't believe you can do DML inside a cacheable method – Scott McClung Oct 29 '20 at 6:38
  • Probably also need to close the quick action panel after the work is done. developer.salesforce.com/docs/atlas.en-us.lightning.meta/… – Scott McClung Oct 29 '20 at 6:42
  • @ScottMcClung thanks! Yes, I figured that (cacheable=true) can't be used as I got an error in the debug logs (System.LimitException: Too many DML statements: 1) – Golnaz H Oct 30 '20 at 0:41
  • Try leadId: this.recordId – Scott McClung Oct 30 '20 at 6:00
  • 1
    I managed to fix it by using getRecordNotifyChange([{recordId: this.recordId}]); – Golnaz H Nov 2 '20 at 21:56
1

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>
0

I think you should be doing this logic in renderedCallback which is where according to documentation is where logic should be executed on load of lwc and not use a wire whose intent is to provide an immutable stream of data to the component. I suggest you do the following.

  1. add force:hasRecordId to your aura component implementation attribute
  2. add recordId parameter leadTakeOwnership component like this
  3. <c:leadTakeOwnership recordId="{!v.recordId}"></c:leadTakeOwnership>
  4. Remove wire then add below renderedcallback
renderedCallback() {
     //  the required business logic to be executed when component is rendered
     ChangeLeadOwner({
      leadId: '$recordId'
    })
      .then((lead) => {
        //do something with lead. Your Apex returns it so I assume you want to do something with it
      })
      .catch((error) => {
        console.log(error);
      });
    }

Hope this helps!

3
  • thanks you! I updated the code but now I am getting en error. updated the question. – Golnaz H Oct 30 '20 at 3:05
  • Are you getting the recordId at the aura component? Can you add an init handler to aura component and console.log(component.get("v.recordId")) to see if you are getting it? See this link on how to set aura handler. developer.salesforce.com/docs/atlas.en-us.lightning.meta/… – GotStu Oct 30 '20 at 15:22
  • I updated the code in the questions and it works now. The issue I am having is that the owner update without refresh is hit and miss. I'm really confused, sometime it does update the owner without a need to refresh the page but sometimes you have to update the page to see the update. I also tried in incognito and it's hit and miss there too. Any ideas of the root cause of this behavior? – Golnaz H Nov 2 '20 at 4:22
0

You should be able to hide the modal with the following markup. You will also need to close it after the LWC has successfully completed its operation.

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

Adding this to your Aura component controller will allow you to call the close QA from the LWC.

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

In your LWC JS file you can then use a renderedCallback to call you apex method and pass the current record id. On success you can display your toast message and close your Quick Action Modal.

const closeQA = new CustomEvent('close')
this.dispatchEvent(closeQA);

Also when @AuraEnabled(cacheable=true) the method must only get data. It can’t mutate data.

1
  • thanks you! I updated the code but now I am getting en error. updated the question. – Golnaz H Oct 30 '20 at 3:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.