4

Short version: Can I return my own messages in the javascript remote event.message object?

Long version: I use a javascript remote call to perform an ajax search.

This is the code (pretty much just use the standard SF provided example):

function ajaxSearch() {
    var searchBoxValue = document.getElementById('searchValue_sharing_search').value;

    Visualforce.remoting.Manager.invokeAction(
        '{!$RemoteAction.TestExtension.ajaxGetRoles}',
        searchBoxValue,
        function(result, event){
            if (event.status) {
                buildRoleMap(results);
            } else if (event.type === 'exception') {
                document.getElementById("responseErrors").innerHTML = 
                    event.message + "<br/>\n<pre>" + event.where + "</pre>";
            } else {
                document.getElementById("responseErrors").innerHTML = event.message;
            }
        }
    );
}

This works great. However, sometimes a search comes back empty. I want to have a message appear on my Visualforce page telling the user why nothing comes back. I could just use Javascript to inspect the result. Something like this:

function(result, event){
    if (event.status) {
        if (result === '[]') {
            //create and display message
        }

However, Javascript remoting already returns an event object which has messages, and I already have a way to display them on the page. Like so (taken directly from the SF example):

else {
    document.getElementById("responseErrors").innerHTML = event.message;
} 

Maybe this is a rookie question, but can I return my own messages in the event object? All I can find on the subject from SF docs are that 'The event object is typically used only in debugging and sophisticated error management'.

Is it possible? Or is there a better way I am unaware of?

2

What you can do is create a wrapper class for your actual result, together with the error message (if there is one). So in your controller you'd have something like:

public with sharing class TestExtension
{
    public class CustomResult
    {
        public List <Contact> contacts; // I guess your result is a list of some sObject
        public String errorMessage;

        public CustomResult()
        {
            this.errorMessage = '';
        }
    }

    // then in your remote method
    @RemoteAction
    public static String ajaxGetRoles(String searchBoxValue)
    {
        CustomResult customResult = new CustomResult ();
        customResult.contacts = [SELECT Id FROM Contact WHERE Name = :searchBoxValue]; // just an example
        if (customResult.contacts == null)
        {
            customResult.errorMessage = 'There are no contacts that match your search criteria';
        }

        return JSON.serialize(customResult);
    }
}

And then in your page deserialise the JSON response and get the error message:

if (event.status) {
    if (result === '[]') {
        var resultObject = JSON.parse(result);
        // your error message is : resultObject.errorMessage
    }
}

This has never been compiled or tested, so you need to tweak it and adjust it according to your existing code.

1

I know this is very late to answer this question. However, I just want to share my finding which someone may find useful.

To be very clear, we can not edit the event object which is returned as the result of remoting call directly.

Whatever run-time exception occurs, the platform updates the event object by assigning appropriate error message to the attribute event.message.

Considering this, if we manually throw an exception from the apex controller, we can change the event.message attribute. Here is an example,

@RemoteAction
public static boolean someMethod()
{
    DMLException exp = new DMLException();
    if( 1 == 1 )
    {
        exp.setMessage('This is a system thrown exception'); // this is how we can modify the event.message text
        throw exp;
    }
    else
        return null;
}

Hope this helps.

Thanks.

0

I was also searching this answer and this is what came up to my mind.

I do think the correct answer will be to extend the Exception class of Salesforce and then throw an Exception of that type.

So your class might look like something of that type :

class MyRemoteActions {

    class MyException extends Exception {}

    global static void myMethod() {
        // Do some stuff

        // If something went wrong
        throw new MyException('My custom message')
    }
}

This will allow your code to work as expected in front-end.

function ajaxSearch() {
    var searchBoxValue = document.getElementById('searchValue_sharing_search').value;

    Visualforce.remoting.Manager.invokeAction(
        '{!$RemoteAction.TestExtension.ajaxGetRoles}',
        searchBoxValue,
        function(result, event){
            if (event.statusCode == 200) {
                buildRoleMap(result);
            } else {
                // event.message contains "My custom message"
                document.getElementById("responseErrors").innerHTML = event.message;
            }
        }
    );
}

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.