0

Greetings of the day1

I'm stuck in a problem where my onclick on Lightning button is not invoking javascript function.

Functionality: I have created a quick action on account labled as "Convert to Account". Once a user is clicking the button a dilog box will appear and it'll ask to continue Yes or No. If a user clicks yes then the contact gets deleted and new account is created and new account record gets created. If a user clicks No the transaction gets cancelled.

Please refer the below code:

Convert_Contact_to_Account.cmp

<aura:component controller="Convert_Contact_to_Account" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
   <div class="c-container">
       
        <lightning:layout verticalAlign="center" class="x-large">
            <lightning:layoutItem flexibility="auto" padding="around-small">
                <div class="slds-align_absolute-center" style="height:5rem">
                    <h1> <b>Would you like to Convert this Contact to Account?</b></h1>
                </div>
               
                <div class="slds-align_absolute-center" style="height:5rem">
                   
                    <lightning:button variant="brand" label="Yes" title="Yes" onclick="{!c.Convert}" />
                    <lightning:button variant="brand" label="No" title="No" onclick="{!c.Cancel}" />
                </div>
            </lightning:layoutItem>
         </lightning:layout>
    </div>
</aura:component>

Convert_Contact_to_AccountController.js

({
    Convert : function(component, event, helper) {
        Console.log('HIiiiiiiiiiiiiiiiiiii');
        let recId = component.get("v.recordId");
        
        var action = component.get("c.Convert_Contact_to_Account");
        action.setParams({
            
            recId:recId
                
                });
        
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                let accid= response.getReturnValue();//Returns the inserted id from apex
                
                console.log('===>accid'+accid[0]);
                if(accid != ''){
                    var toastEvent = $A.get("e.force:showToast");
                    toastEvent.setParams({
                        "title": "Success!!",
                        "message": "Account Creation Successfull!",
                        "type":"Success"
                                    });
                    toastEvent.fire();
                }
                else if(state == "ERROR"){
                    var errors = response.getError();                      
                    component.set("v.showErrors",true);
                    component.set("v.errorMessage",errors[0].message);
                    
                }
                var navEvt = $A.get("e.force:navigateToSObject");
                navEvt.setParams({
                    "recordId":accid,
                    "slideDevName": "Detail"
                            });
                navEvt.fire();
                
            }
            
        });
        
        // Send action off to be executed
        $A.enqueueAction(action);
    },
        Cancel : function(component,event,helper){
            $A.get("e.force:closeQuickAction").fire();
        }
})

Apex class:

public class Convert_Contact_to_Account {
    @AuraEnabled
    public static String convertToAccount(String recId){
       
        Contact c=[select id,LastName from Contact where id=:recId];
        account a=new account();
        a.Name=c.LastName;
        insert a;
        system.debug('===a'+a);
        delete c;
        return a.Id;
       
    }
}

Line <lightning:button variant="brand" label="Yes" title="Yes" onclick="{!c.Convert}" /> is not invoking c.Convert function.

If anyone can review the code especially the javascript that'll be very helpful.Any lead will be appreciated.

Thanks & Regards, Sumit

2
  • When you say that the button isn't invoking your Convert method, do you mean the body of Convert_Contact_to_AccountController.Convert() is never entered? I see no issues with the code up to that point, but your action will fail. The name of the method in your Apex controller is convertToAccount - it is not Convert_Contact_to_Account. Does your Console.log('HIiiiiiiiiiiiiiiiiiii'); get logged? Look at adding a debugger; statement at the start of Convert so you can walk through the code in your browser developer console.
    – nbrown
    Jan 29, 2021 at 15:45
  • Hi, I've added a console.log() at starting to print HIiii but even that is not getting logged. And moreover I've changed the code to call aura enabled method instead of class. But still the function on yes button is not invoked. Jan 29, 2021 at 16:40

1 Answer 1

0

Issue in your code, you are calling apex class, which is wrong.

var action = component.get("c.Convert_Contact_to_Account");

This should be auraEnabled Method name

var action = component.get("c.convertToAccount");
1
  • Hi Sarvesh I've modified the code to call aura enabled method instead of apex class still button is not invoking the JavaScript function. Moreover at least it should print hi on console. Even that is not getting printed. Jan 29, 2021 at 16:38

You must log in to answer this question.

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