I had a javascript button that performed some validation and update via APEX in classic and when transitioning to Lightning I create a Lightning quick action (aura component) to replace it. A button opens the action on the Opportunity detail page in a Lightning console. The component opens fine but when trying to Confirm I got an error message "Uncaught Error in $A.getCallback() [Cannot read property 'setParams' of undefined].Callback failed:apex…".
Aura component:
<aura:component implements="flexipage:availableForAllPageTypes,force:lightningQuickActionWithoutHeader,force:hasRecordId" access="global" controller="ConvertAPEX">
<aura:attribute name="recordId" type="String" />
<aura:attribute name="opportunity" type="Opportunity" />
<div class="slds-hide">
<lightning:recordEditForm aura:id="editform"
recordId="{!v.recordId}"
objectApiName="Opportunity">
<lightning:outputField fieldName="Account_Status__c" aura:id="status"/>
<lightning:outputField fieldName="IsClosed_text__c" aura:id="closed"/>
</lightning:recordEditForm>
</div>
<div class="slds-modal__header">
<h2 class="slds-text-heading_medium slds-hyphenate">Convert</h2>
</div>
<div class="slds-modal__content slds-p-around_medium" id="modal-content-id-1">
<p>You are about to convert this record.</p>
<br/>
<p>Are you sure you want to continue?</p>
</div>
<div class="slds-modal__footer">
<lightning:button class="slds-button_brand" onclick="{!c.convertRegistration}" label="Confirm" />
<lightning:button class="slds-button_neutral" onclick="{!c.cancel}" label="Cancel"/>
</div>
Javascript Controller:
({
convertRegistration : function(component) {
var opp = component.get("v.opportunity");
var action = component.get("c.ConvertRegistrationData");
action.setParams({
OppId: component.get("v.recordId")
});
action.setCallback(this,
function(response) {
var state = response.getState();
if (state === "SUCCESS"){
component.set("v.opportunity", response.getReturnValue());
var accStatus = component.find("status").get("v.value");
var isClosed = component.find("closed").get("v.value");
if (isClosed === "Yes") {
$A.get("e.force:closeQuickAction").fire();
var toastEvent = $A.get("e.force:showToast");
toastEvent.setParams({
"title": "WARNING!",
"type": "warning",
"message": "Can only convert for OPEN opportunities."
});
toastEvent.fire();
$A.get('e.force:refreshView').fire();
}
else if(accStatus !== "Unregistered" || accStatus !== "Registered") {
$A.get("e.force:closeQuickAction").fire();
toastEvent.setParams({
"title": "WARNING!",
"type": "warning",
"message": "Can only convert for Unregistered, Registered accounts."
});
toastEvent.fire();
$A.get('e.force:refreshView').fire();
} else {
$A.get("e.force:closeQuickAction").fire();
toastEvent.setParams({
"title" : "Success",
"message" : "Merchant converted.",
"type" : "success"
});
toastEvent.fire();
$A.get('e.force:refreshView').fire();
}
}
else if (response.state === "ERROR") {
var errors = response.getError();
$A.get("e.force:closeQuickAction").fire();
toastEvent.setParams({
"title": "Error!",
"type": "error",
"message": errors[0].message
});
toastEvent.fire();
$A.get('e.force:refreshView').fire();
}
});
$A.enqueueAction(action);
},
cancel : function(){
$A.get("e.force:closeQuickAction").fire();
$A.get('e.force:refreshView').fire();
}
})
APEX Controller:
global class ConvertAPEX {
@AuraEnabled
Webservice static Boolean ConvertRegistrationData(Id OppId)
{
boolean retVal = false;
Savepoint sp = Database.setSavepoint();
try {
rego_Branch_Settings__c rbs = rego_Branch_Settings__c.getOrgDefaults();
Opportunity opp = [select Id
,AccountId
,Name
,Rego__c
,IsClosed_text__c
,Account_Status__c
from Opportunity where Id=: OppId];
Account acc = [select Id
,AccountStatus__c
from Account where Id =: opp.AccountId];
Rego__c r = getRego(acc.Id);
boolean registrationCreated = SetFacilitatorRegistration(acc, r, opp);
r.Provisioning_Supporting_Docs_Required__c = '';
//a serie of if statements
{
update acc;
update opp;
update r;
retVal = true;
}
else {
throw Exception('Product failed to add.');
}
}
else
{
throw Exception('No need to convert.');
}
}
}
catch (Exception ex)
{
Error.SendErrorEmail(ex, 'Unable to Convert Registration');
retVal = false;
Database.rollback( sp );
}
return retVal;
}
Thank you in advance for your help.
Sylvie
Best Answer
I'm guessing a bit here, but...
in your javascript function
convertRegistration
you initialize the variabletoastEvent
only if isClosed equals 'Yes':otherwise, 'toastEvent' is neither declared nor initialized:
Here, toastEvent is just undefined, just as it is further down, as it has not been initialized.
Either declare and assign
toastEvent
before the if-clause, or do that within each of the decision blocks.I assume, your current code would work, if IsClosed_text__c contains 'Yes'.