[SalesForce] Toast event in Lightning not working

Hello I am trying to make a Lightning component common to Community and SF1
. This component has been added as quick action on one of the object detail page.

Now when i am using the toast event, it gives me error as

Something has gone wrong. [TypeError: Cannot read property 'setParams' of undefined] . Please try again.

Code for same –

action.setCallback(this, function(response) {
            var state = response.getState();
            console.log('state--->1', state);
            if (state === 'SUCCESS') {
                jobApplicantId = response.getReturnValue();
                fromPos = toPos;
                console.log('fileContents.length -->', fileContents.length);
                toPos = Math.min(fileContents.length, fromPos + CHUNK_SIZE);
                console.log('fromPos -->' , fromPos);
                console.log('toPos -->' , toPos);

                if (fromPos < toPos) {
                    this.uploadChunk(component, file, fileContents, fromPos, toPos, attachId);  
                } else if (fromPos == toPos) {
                    //alert('Job Application Successful');
                    var toastEvent = $A.get("e.force:showToast");
                    console.log('toastEvent -->', toastEvent);
                    toastEvent.setParams({
                        "title": "Success!",
                        "message": "The record has been updated successfully."
                    });
                    toastEvent.fire();
                    component.find("comments").set("v.value", "");
                    component.find("file").getElement().value = "";
                    this.displayJobAppliedButton(component);
                    //component.find("file").getElement().removeAttribute('value');
                }
            } else {
                var errors = response.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        alert("Error message: " + 
                                 errors[0].message);
                    }
                } else {
                    console.log("Unknown error");
                }
            }

        });

Snapshot –
enter image description here
This whole thing is in a helper class? can this be the issue?

Best Answer

From the description and image ,I see that you are using Lightning components inside the Visualforce page . Having lightning components inside the visualforce ,you will loose certain events as documented in the guide .The events that are not supported are as below

1.force:createRecord

2.force:editRecord

3.force:navigateToList

4.force:navigateToObjectHome

5.force:navigateToRelatedList

6.force:showToast

Official Documentation On Toast event

I think you should open an Idea on Idea Exchange to support lightning components directly from publisher actions .

Related Topic