function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Can APEX return messages to the output panel in Standard Controllers (not VisualForce)?

Hi everyone,


I have a button on a custom object that runs the following javascript


{!REQUIRESCRIPT("/soap/ajax/14.0/connection.js")} {!REQUIRESCRIPT("/soap/ajax/14.0/apex.js")} try { document.getElementById("errorDiv_ep").style.display = "none"; document.getElementById("errorDiv_ep").style.textAlign = "center"; var id = sforce.apex.execute( "InvoiceWebService","CreateInvoiceFromProductUsagePlan",{ProductUsagePlanId:"{!ASP_Product_Usage_Plan__c.Id}" } ); window.location = "/" + id; } catch (e) { document.getElementById("errorDiv_ep").style.display = ""; document.getElementById("errorDiv_ep").innerHTML = "<table style='text-Align:left'><tr><td><font color=red>" + e.faultstring.replace(/\n/g,"<br />").replace( /:/g, ":<br />" ) + "</font></td></tr></table>"; }



Essentially what it is doing is calling an APEX class/method, and checking to see if the call is going to return an error via try/catch.

In my try/catch statement, I take the error returned and display it in the errorDiv_ep element that is standard on each Salesforce page.


I know this isn't the most elegant solution for displaying errors on screen, but I'm wondering if there is a better method (without the use of VisualForce). Is it also possible to return confirmation messages rather than error messages as well?


Excluding Visualforce, it really depends on how your Apex web service returns confirmations and errors.

The try/catch logic can catch and display the exceptions which looks like you already enabled. 

But if the Apex method is successful, the return type needs to determine that and the javascript can then check the return value in order to display a confirmation message.