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
SS KarthickSS Karthick 

Trigger and Validation Error in Visualforce page using JavaScript Remoting

Hi folks,
          Can anyone tell me how to catch the trigger and validation error in visualforce page using javascript remoting?
I wanna sample code to catch the trigger or validation error in visualforce using java script remoting.



Thanks in advance
Karthick
Best Answer chosen by SS Karthick
sharathchandra thukkanisharathchandra thukkani
When you validation rules will be firing before you insert the record and trigger validation errors are also in the before event.
in java script remoting you would be calling @RemoteAction method which would return you error string and there you can use try catch block and return a error string, based on that error string you can show the error in java script.

//controller
public class ErrorHandler{

@RemoteAction
public static String errorWhileInserting(){
String error = '';
try{
//insert statement;
}
catch(Exception e){
error=e.getMessage();
}
return e;
}
}

//page
<apex:page controller="ErrorHandler" >
<script>
//call it on some event
function onclickFunction(){
ErrorHandler.errorWhileInserting(function(result, event){ if(event.status) {
document.getElementById("result").innerHTML = result(your error message); }
});
}
</script>
</apex:page>
 

All Answers

Karthik@TMCKarthik@TMC
You can find one here https://www.salesforce.com/us/developer/docs/pages/Content/pages_js_remoting_example.htm
SS KarthickSS Karthick
@Karthik,
    I have seen this link..
I have to know about how to handle the trigger error in javascript remoting

Please help!
Karthik@TMCKarthik@TMC
To catch the validation errors use trigger.addError('your message'); Since the save event fails, it will get listed in exception block
i.e.

} else if (event.type === 'exception') { document.getElementById("responseErrors").innerHTML = event.message + "<br/>\n<pre>" + event.where + "</pre>"; }
 
SS KarthickSS Karthick
@Karthick,
   thanks for your response.
Can you share your mailid,skype id to my mail(pskarthick.sonacse23@gmail.com)
Because i've lot of doubts in salesfoce


Thanks in advance
Karthick
sharathchandra thukkanisharathchandra thukkani
When you validation rules will be firing before you insert the record and trigger validation errors are also in the before event.
in java script remoting you would be calling @RemoteAction method which would return you error string and there you can use try catch block and return a error string, based on that error string you can show the error in java script.

//controller
public class ErrorHandler{

@RemoteAction
public static String errorWhileInserting(){
String error = '';
try{
//insert statement;
}
catch(Exception e){
error=e.getMessage();
}
return e;
}
}

//page
<apex:page controller="ErrorHandler" >
<script>
//call it on some event
function onclickFunction(){
ErrorHandler.errorWhileInserting(function(result, event){ if(event.status) {
document.getElementById("result").innerHTML = result(your error message); }
});
}
</script>
</apex:page>
 
This was selected as the best answer