Visualforce Remoting Exception: Unexpected type for MyController.myMethod(CustomObject)
What happened in Background :
This error mainly happened when the javascript object set incorrect type value to the object fields.So we can check the assigned values. but every body normally check the value but not the field type with the assigned value
we need to check both of that.
After check again the error display means we need to check the date field value. because mainly the javascript remoting can set the default value as null.
What kind of value set in date field it shows what kind of error happening is given below as deep,
1. null
when using the null to assign the value than java script remoting throws the Visualforce Remoting Exception: Unexpected type for MobileTripController.myMethod(CustomObject).
2. 0
it doesnot throw any error. it save the data if the date filed is not mandatory. it assign the starting date of the calendar as default
3. undefiend
The remote scripting not throw any error. it save data but not set any value in date field
4. nan
It throws the error for Visualforce Remoting Exception: Unexpected type for MobileTripController.myMethod(CustomObject)
5. "null"
it throws the same error for Visualforce Remoting Exception: Unexpected type for MobileTripController.myMethod(CustomObject)
so we can check this conditions when the exception occur while using javascript remoting.
Error code sample:
java script:
function CustomObj__c() {
this.Id=null;
this.name__c = null;
this.date__c=null;
}
Here we used the date as null. so we can change it .
Solution :
We can get string parameter in apex class. then convert it as date object then set that filed.
Example:
function CustomObj__c() {
this.Id=null;
this.name__c = null;
// this.date__c=null; Here removed the date field
}
then create object
var obj = new CustomObj__c();
obj.name__c = 'Rama';
Visualforce.remoting.Manager.invokeAction(
'{!$RemoteAction.CustomController.CustomMethod}',
obj,$('#date').val(),
function(result, event) {
if(event.type === 'exception') {
console.log(event.message);
console.log(event);
}
//console.log(result);
});
CustomController apex class code :
public with sharing class CustomController {
public CustomController(ApexPages.StandardController controller) {
}
public CustomController() {
}
@RemoteAction
public static void CustomMethod(CustomObj__c obj, String dayOfTrip){
try{
obj.date__c = date.valueOf(dayOfTrip); // Here to change and assign the value
insert obj;
} catch(Exception ex) {
System.debug(ex);
}
}
}
Comments
Post a Comment