+ Start a Discussion
ForceRookieForceRookie 

How to fix: Error in $A.getCallback() [Cannot read property 'setParams' of undefined]

Help me with my Save function, please?

Component

<aura:component controller="MyCustomSettingsController">
	<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
	<aura:attribute name="CustSettings" type="MyCustomSettings__c"/>
	<div class="divform">
		<form>
			<table>
				<tbody>
					<tr>
						<td>
							<span class="slds-text-heading--small">Client Key</span>
							<lightning:input type="text" aura:id="clientKey"
											 name="client"
											 value="{!v.CustSettings.Client_Key__c}"/>
						</td>
						<td>
							<span class="slds-text-heading--small">Secret Key</span>
							<lightning:input type="text" aura:id="secretKey"
											 name="secret"
											 value="{!v.CustSettings.Secret_Key__c}"/>
						</td>
					</tr>
				</tbody>
			</table>
			<div class="slds-grid">
				<lightning:button aura:id="save"
								  label="Save"
								  class="slds-m-top--medium"
								  variant="brand"
								  onclick="{!c.btnSave}"/>
			</div>
		</form>
	</div>
</aura:component>

JS Controller

({
	doInit : function(component, event, helper) {
		var action = component.get("c.getCustomSettings");
		action.setCallback(this, function(response) {
			var state = response.getState();
			console.log(state);
			if (state === "SUCCESS") {
				var returnVal = response.getReturnValue();
				component.set("v.CustSettings", returnVal);
			}
		});
		$A.enqueueAction(action);
	},
 
	btnSave : function(component, event, helper) {
        var CustSettings = component.get("v.CustSettings");
		var action = component.get("c.saveCustSettings");
        action.setParams({"Cust_setting" : CustSettings});
        action.setCallback(this,function(response){
			var status = response.getState();
            if(status === "SUCCESS"){
				var msg = 'Changes saved!';
                var toastEvent = $A.get("e.force:showToast");
                toastEvent.setParams({
                    title	: "Success!",
                    mode	: 'pester',
                    duration: '3000',
                    message	: msg,
                    type	: 'success'
                });
                toastEvent.fire();
			}
		});
		$A.enqueueAction(action);
    }
})
Apex controller
public with sharing class MyCustomSettingsController {
	@AuraEnabled
    public static MyCustomSettings__c getCustomSettings() {
       MyCustomSettings__c query = [SELECT Id, Client_Key__c, Secret_Key__c
                FROM MyCustomSettings__c];
        return query;
    }
    @AuraEnabled
    public static void saveCustSettings(MyCustomSettings__c Cust_setting){
        
        MyCustomSettings__c Custsetting = new MyCustomSettings__c();
        Custsetting = Cust_setting;
        update Custsetting;
    }
}

 
Best Answer chosen by ForceRookie
Khan AnasKhan Anas (Salesforce Developers) 
Hi,

Greetings to you!

When you try to launch the component via a lightning app, standard events like showToast, force:createRecord, etc will be undefined. 

According to Salesforce doc:
This event is handled by the one.app container. It’s supported in Lightning Experience, Salesforce app, and Lightning communities.

https://developer.salesforce.com/docs/component-library/bundle/force:showToast/documentation


So, launch your component in LEX via app builder or lightning quick-action or drag to the detail page.

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi,

Greetings to you!

When you try to launch the component via a lightning app, standard events like showToast, force:createRecord, etc will be undefined. 

According to Salesforce doc:
This event is handled by the one.app container. It’s supported in Lightning Experience, Salesforce app, and Lightning communities.

https://developer.salesforce.com/docs/component-library/bundle/force:showToast/documentation


So, launch your component in LEX via app builder or lightning quick-action or drag to the detail page.

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer
Gabriel C FerreiraGabriel C Ferreira
Hi,

The problem is caused when you try to show the toast component. Try the following:

1. Add this on your component:
<lightning:notificationsLibrary aura:id="toastSection" />

2. Replace the lines 23 to 31 of your js controller with:
component.find('toastSection').showToast({
    mode: 'dismissible',
    title: 'Sucess!',
    variant: 'success'
});

Hope it helps.
Best Regards
ForceRookieForceRookie

Hi Khan Anas, thanks for that info. It helped me!

Hi Gabriel, it also doesn't work on lightning app, so I just created a custom toast. Thanks anyway!