Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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