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
Convert
method, do you mean the body ofConvert_Contact_to_AccountController.Convert()
is never entered? I see no issues with the code up to that point, but youraction
will fail. The name of the method in your Apex controller isconvertToAccount
- it is notConvert_Contact_to_Account
. Does yourConsole.log('HIiiiiiiiiiiiiiiiiiii');
get logged? Look at adding adebugger;
statement at the start ofConvert
so you can walk through the code in your browser developer console.