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.
<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>
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
// 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.
var action = component.get("c.Convert_Contact_to_Account");
By
var action = component.get("c.convertToAccount");
Apart from that, please try to use let instead of var, though this using var is not the root cause here. You need to call the Apex method name instead of the Apex class name.
Hi Prosenjoy, Thank you so much for your instant reply. I've made the changes as you suggested to call aura enabled method instead of class. But still it's yes button not invoking the convert method. II'll be very helpful if you could identify the root cause.
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
Try this out,
replace By Apart from that, please try to use let instead of var, though this using var is not the root cause here. You need to call the Apex method name instead of the Apex class name.
Regards,
Prosenjit
Thank you so much for your instant reply. I've made the changes as you suggested to call aura enabled method instead of class. But still it's yes button not invoking the convert method.
II'll be very helpful if you could identify the root cause.
Warm Regards,
Sumit