0

I'm learning Lightning and in the process I'm creating a simple component that would take Name and Description of an Account and insert the same into the database and would also give a Success Message on completion. Following is the code I've written but I'm getting "Callback Failed" error in console.

P.S - My toast part seems not to be working, i've commented all the lines concerned with it then code is running fine. Can someone help me what wrong I'm doing here?

EDIT: I've created a standanlone app using app builder to run this on lightning experience but i'm getting this error message which I'm not able to understand. Uncaught Error in $A.getCallback() [toastEvent is not defined]

Component:

<aura:component controller="AccountCreate" implements="force:appHostable">
<aura:attribute name="account" type="Account" default="{'sobjectType':'Account',
                                                        'Name':'',
                                                        'Description':''}"/>
<ui:inputText label="Account Name" value="{!v.account.Name}"/>
<ui:inputText label="Description" value="{!v.account.Description}"/>
<ui:button label="create" press="{!c.createAccount}"/>

JS Controller

({
createAccount : function(component, event, helper) {
    var action = component.get("c.createNewAccount");
    var account = component.get("v.account");
    console.log(account);
    action.setParams({
        "acc":account
    });
    action.setCallback(this,function(response){
        var state = response.getState();
        console.log(state);
        var toastEvent = $A.get("e.force:showToast");
        if(component.isValid() && state === "SUCCESS"){
            toastEvent.setParams({
                "title": "Success!",
                "message": "The record has been created successfully."
            });
            toastEvent.fire();
            component.set("v.account",{'sobjectType':'Account',
                                        'Name':'',
                                        'Description':''});
        }
    });

    $A.enqueueAction(action);
  }
})

Apex Controller

public with sharing class AccountCreate {
@AuraEnabled
public static void createNewAccount(Account acc){
    upsert acc;
 }
}
  • 1
    Check the apex debug logs. What do they say. Or what does response.getError()[0].message; output in the callback – Eric Feb 20 '17 at 13:57
  • 1
    Make sure you running in lightning Experiance.Toast event is handled by the one.app container. It’s supported in Lightning Experience and Salesforce1 only. – vignesh Feb 20 '17 at 14:36
  • @Eric thanks! Problem was on my js code on case sensitivity. Still trying to figure out why I'm not getting any toast message. – Sound of Silence Feb 20 '17 at 16:09
  • I'm running this by the preview of the Lightning Application i created. Am I doing any wrong there? Getting the following error: This page has an error. You might just need to refresh it. Error in $A.getCallback() [toastEvent is not defined] Callback failed: apex://AccountCreate/ACTION$createNewAccount Failing descriptor: {markup://c:createAccount} – Sound of Silence Feb 20 '17 at 16:12
1

As pointed out by Vignesh Toast Event would run only on Lightning Experience/SF1 I created a SPA using Lightning App Builder with the above component with some minor modification like "flexipage:availableForAllPageTypes" on the component so that it can be used in App Builder. Another mistake I did was regarding case sensitivity of sobjectType . It'was previously sObjectType and so i was getting error. Rest of the code works fine!

1

The event "e.force:showToast" works in one.app container only.Hence you need to drag and drop this custom lightning component in the lightning app builder post which you can use it.Please refer to below documentation stating the limitation and details of this event

https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/ref_force_showToast.htm

0

I think the issue is with your upsert statement. Not sure why you are using upsert and it should be Insert. With upsert, you need to specify an External Id field OR salesforce excepts ID which you don't have in this case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.