Find your content:

Search form

You are here

How to pass a fieldset to javascript?

 
Share

When my user clicks a button, I want to access the fieldset by javascript. Can i do that?

  <apex:commandbutton value="Send"  onClick="check('{$ObjectType.Customization__c.FieldSets.Fields_for_fieldChooser}')" />

javascript

<script>
    function check(apiNameList) {

        for (var i = 0; i < apiNameList.length; i++) {
            var apiName = apiNameList[i];
            console.log(apiNameList[i]);

            var updateVal = apiName + "_update";
            var deleteVal = apiName + "_delete";
            var dontaddVal = apiName + "_do_not_add";
            //    if ( document.getElementById(updateVal).checked === false && document.getElementById(deleteVal).checked === false && document.getElementById(dontaddVal).checked === false){
            alert("Please choose an option for each field");
            break;

            //     }

        }
    }
</script>

Attribution to: PartOfTheOhana

Possible Suggestion/Solution #1

To get quotes round the field names so you have a JavaScript array of strings, you need to turn the Apex array into a JavaScript array by for example using an apex:repeat:

<apex:page>
<script>
var fields = [
<apex:repeat var="f" value="{!$ObjectType.Customization__c.FieldSets.Fields_for_fieldChooser}">
    '{!f}',
</apex:repeat>
];
</script>
</apex:page>

The above Visualforce produces this:

<script>
var fields = [
    'Field1__c',
    'Field2__c',
   ' Field3__c',
];
</script>

Then adapt you other JavaScript to reference this array. (Note that the spurious trailing comma is allowed in JavaScript).

Or if you need the JavaScript arrays to be inline, this will work:

... onClick="check([<apex:repeat var="f" value="{!$ObjectType.TestRelationship__c.FieldSets.TestFieldSet}">'{!f}', </apex:repeat>])" ...

Attribution to: Keith C
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/31800

My Block Status

My Block Content