Hire a web Developer and Designer to upgrade and boost your online presence with cutting edge Technologies

Wednesday, April 1, 2015

Javascript Remoting : Pass parameters from javascript to Salesforce standard object field

 Here we are going to pass the sobject from the java script to apex using javascript remoting to insert the record in the database.
            Here we are going to use our custom object to pass parameter to apex and insert the record.

I am using the custom object name as CustomObj__c . Then the controller name is MyCustomController.
The code is given below

Step 1:
        Need to write javascript code to create the customobject mannually.
 the customobject in javascript is given below
           
 function CustomObj__c(){
this.Id= null;
this.name__c = null;
this.address__c = null;             
}



Then create the instance for the function. ie object
var custObj = new CustomObj__c();

step 2:

    Then assign the calue what you want to store.

custObj.name__c = 'RamaKavanan';
custObj.address__c = '3/14, North street, India';

Step 3: Then invoke the controller method 

Visualforce.remoting.Manager.invokeAction(
        '{!$RemoteAction.MyCustomController.AddCustObj}',
        custObj,
        function(result, event) {
          if(event.type === 'exception') {
console.log("Exception occured");
} else if(event.status) {
console.log(result);
}
});

Step 4: The controller access the method parameter and process it.

public with sharing class MyCustomController {
 
    @RemoteAction
    public static void AddCustObj(CustomObj__c obj){
        insert obj;
    }
 
}


Thats it. it will send the data and store the database. same way we can do CRUD opration in javascript remoting.


The full code of the visualforce page is

<apex:page controller="MyCustomController">
<script>
function CustomObj__c(){
this.Id= null;
this.name__c = null;
this.address__c = null;             
}
var custObj = new CustomObj__c();
custObj.name__c = 'RamaKavanan';
custObj.address__c = '3/14, North street, India';
console.log(custObj);
Visualforce.remoting.Manager.invokeAction(
        '{!$RemoteAction.MyCustomController.AddCustObj}',
        custObj,
        function(result, event) {
          if(event.type === 'exception') {
console.log("Exception occured");
} else if(event.status) {
console.log(result);
}
        });
</script>
<body></body>
</apex:page>

The controller full code is given the above..

No comments:

Post a Comment