×

Please give details of the problem

Skip to content

How to deal with a Salesforce object

Get a field value from Salesforce

eg : Retrieving the values of a list from an SF object:

JS

<@script env="javascript">  
function get_sf_field(my_father, my_son, default_value) {
    if (typeof(my_father[my_son]) == "object") {  
        var my_result = default_value;  
    } else {  
        var my_result = my_father[my_son];  
    }  
    return my_result;  
}  

var my_field = P_result.Envelope.Body.describeSObjectResponse.result.fields;  
var a_nationality = new Array();  

for (var i = 0; i < my_field.length; i++) {  
    if (get_sf_field(my_field[i], "name") == "Nationality__c") {  
        for (var j = 0; j < my_field[i].picklistValues.length; j++) {  
            a_nationality.push({ 
                "label" : my_field[i].picklistValues[j].label,  
                "value" : my_field[i].picklistValues[j].value  
            });  
        }  
        break;  
    }  
}  
setVariable("a_nationality", a_nationality);  
</@script>  

Freemarker

<#function get_sf_field my_father my_son default_value>  
    <#if my_father[my_son]?exists>  
        <#if my_father[my_son]?is\_hash>  
            <#assign my_result = default_value>  
        <#else>  
            <#assign my_result = my_father[my_son]>  
        </#if>  
    </#if>  
    <#return my_result>  
</#function>  

<#assign a_nationality = []>  
<#list P_result.Envelope.Body.describeSObjectResponse.result.fields as x>  
    <#if (get_sf_field(x,"name","") = "Nationality__c")>  
        <#list x.picklistValues as y>  
            <#assign a_nationality = a_nationality + [{"label":y.label,"value":y.value}]>
        </#list>  
        <#break>  
    </#if>  
</#list>  
${a_nationality}