Find your content:

Search form

You are here

javascript object creation for js remoting

 
Share

Hi I am trying to use a RemoteAction function from js remoting.The remote function accepts a custom object,so I need to pass it from javascript.For this I m creating a javascript object with all fields of custom object.

function my_object__c(){
   this.id = null;
   this.name = null
   ------------
   more fields here
   ------------
}

The values of id, name and all other fields are binded to vf page's inputFields like this :

<apex:inputfield value="{!myObj.Name}"></apex:inputfield>

is there a easier way to access these values and use them in js object my_object__c.I can only think of travesering the dom and get these based on ids.

Second,can the RemoteFunction accept multiple parameters ?


Attribution to: vishesh

Possible Suggestion/Solution #1

Your method can take Apex primitives, collections, typed and generic sObjects, and user-defined Apex classes and interfaces as arguments. Generic sObjects must have an ID or sobjectType value to identify actual type. Interface parameters must have an apexType to identify actual type. Your method can return Apex primitives, sObjects, collections, user-defined Apex classes and enums, SaveResult, UpsertResult, DeleteResult, SelectOption, or PageReference.

So answering your question

1)Yes remoting can take generic Sobjects with ID or sobjectType Value 2)Yes you can have multiple parameters without overloading 3)Yes i guess acessing dom will be the best practice to get those field values Document link

http://www.salesforce.com/us/developer/docs/pages/Content/pages_js_remoting.htm


Attribution to: Mohith Shrivastava

Possible Suggestion/Solution #2

Mohith did a good job pointing you to the documentation for JavaScript Remoting for what is supported.

To get the values into your JavaScript object you can use jQuery to make it very easy.

Either put jQuery in a static resource or hotlink to one of the CDN hosted copies. Here's how to do use jQuery with the static resources:

Put jQuery in your static resource, for example, in a zip called res in a subfolder called js and be sure to include it prior to using it.

Zip file with the structure /res/js/jquery-1.8.1.min.js is referenced from VF as follows:

<apex:includeScript value="{!URLFOR($Resource.res, 'js/jquery-1.8.1.min.js')}"/>

Then the JavaScript snippet:

<script type="text/javascript">
    var j$ = jQuery.noConflict();

    // Whatever function you end up calling to get the data from the page
    function doItToIt() {
        // Get the value of the name field
        yourObj.Name = j$('.name').val();
    }
</script>

<!-- somewhere on your page is your input with styleClass from above jQuery selector -->
<apex:inputfield value="{!myObj.Name}" styleClass="name" />

Here you can use styleClass instead of ID since the ID that you specify is not the ID that will get generated by Visualforce and then use the jQuery val function. Just make sure that your styleClass is unique so that the jQuery selector works the way that you want it to.


Attribution to: Peter Knolle
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/2292

My Block Status

My Block Content