1

I am trying to create a map in javascript using apex:repeat tag but it's not working as per expectations.

Below is the VF page and controller I have created :

<apex:page controller="ProductFieldsTestController">
    <script type="text/javascript">
        var productIdMap = {};
        <apex:repeat value="{!listProducts}" var="prods">    
            var newprod = new product();
            <apex:repeat value="{!productFields}" var="fieldName">                                     
                var fieldName = '{!fieldName}';
                var fieldValue = '{!prods[fieldName]}';
                console.log('-------- FieldName = ' + fieldName)
                if(isNaN(fieldValue)) {
                    console.log('String = ' + fieldValue);
                    newprod[fieldName] = '{!JSENCODE(prods[fieldName])}';   
                }else{
                    console.log('Number = ' + fieldValue);
                    newprod[fieldName] = '';
                }     
            </apex:repeat>
            productIdMap['{!prods.Id}'] = newprod;
        </apex:repeat> 

        function product() {}
   </script>
</apex:page>



public class ProductFieldsTestController {

    public static Set<String> productFields = new Set<String>{'Id', 'Name', 'ProductCode', 'CreatedById', 'Test_Number__c' };

    public ProductFieldsTestController() {

    } 

    public List<Product2> getListProducts() {
        List<Product2> products = [Select id, name, ProductCode, CreatedById, Test_Number__c from Product2 limit 1];

        return products ;
    }

    public List<String> getProductFields () {
        return new List<String>(productFields); 
    }

}

When I am trying to access the VF page , below exception is occurring:

Incorrect parameter type for function 'JSENCODE()'. Expected Text, received Number
Error is in expression '{!JSENCODE(prods[fieldName])}' in component <apex:repeat> in page productfieldstest

What I have noticed is that salesforce is trying to execute the JSENCODE line irrespective of the IF conditions. I have tried every possible way to make this happen, but no success. Can anyone please suggest what issue is this ?

  • try this - not tried it but may get around your exception newprod[fieldName] = '{!JSENCODE(''+prods[fieldName])}'; – Richard Durrant Jul 10 '14 at 13:30
3

I think you have somewhat of a misunderstanding of client-side vs server-side technologies. Based on your code snippet, the IF conditions that you mentioned in the original post are just simple JavaScript logic. JavaScript is executed on the client-side, i.e. in the browser window when the html output from the server is delivered to the browser and rendered in it.

The binding expression {!JSENCODE('somestring')}, as the name indicates binds the expression to the property of the underlying controller of the current VF page.

To simplify, imagine you have this javascript snippet in your VF page:

var str = '';
if (true) {
    alert('HERE IS NAME#1 {!name1}');
} else {
    alert('HERE IS NAME#2: {!name2}');
}


...and controller with following methods:

public string getName1() { return 'John'; }
pulbic string getName2() { return 'Jane'; }


Regardless of the fact that during JS execution, only first block will ever get called, the fact remains that during VF page processing (while HTML to be delivered is being created), the VF page will access both controller properties, name1 and name2 (or getName1() and getName2() cotnroller methods), resulting in rendering of the following html snippet:

var str = '';
if (true) {
    alert('HERE IS NAME#1 John');
} else {
    alert('HERE IS NAME#2: Jane');
}


To circumvent your issue, you could add a custom method to your controller which would get invoked in either of the cases (regardless whether the isNaN(fieldValue) condition is true or false), and then do a check on server-side whether JESNCODE should be invoked at all.

I hope you'll manage to pick it up from here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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