Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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>
share|improve this question
    
If you are a stickler for delimiters or have an IE userbase, this snippet plays nicely inside the repeat: <apex:outputText rendered="{!fields[0] != field}">,</apex:outputText> –  bigassforce Aug 21 at 12:21

1 Answer 1

up vote 3 down vote accepted

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>])" ...
share|improve this answer
    
If you are a stickler for delimiters or have an IE userbase, this snippet plays nicely inside the repeat: <apex:outputText rendered="{!fields[0] != field}">,</apex:outputText> –  bigassforce Aug 21 at 12:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.