[SalesForce] how to get status and category value based on zone name in idea object salesforce

This question only for users, who have knowledge about Idea object in salesforce
For user who don't know about Idea object, I can't help .

how to get status and category value based on zone name in idea object salesforce

enter image description here

Requirement
I have vf page.
there I have three picklist

  • Zone
  • Category
  • Status

But I am not getting filtered value when I am selecting A zone name from Zone picklist. It gives us all category and Status values.

But in standard sf page Idea detail page, It do these filtering. I want to do the same.

I have tried getting dependent field value using MetaData Api as well. but no luck because Zone(CommunityId-Apiname) is not dependent field of Status and Category field in Idea object, but behave in that manner(Magically).

ZoneId is label name and api name is CommunityId, is reference field in Idea obejct

Status and Category are picklist type of field and can have different-2 value depends upon Zone selection.

ZoneId is not controller field but behave like controller field for Status and Category field.
This all are standard field(Category, Status, CommunityId(label as Zone)) and standard object(Idea)

In the end I would say,Zone is behaving as RecordType.

Now Based on selected Zone, Status and Category values in picklist will be visible
first step to create idea, Select a zone

Idea object Fields

Zone Object detail pageZone Object detail page

Here you can choose which status and cateogry to be shown for each zone

Best Answer

Let me answer my question. It is possible using meta data describe pagelayout method.

<apex:outputPanel id="zoneFilter" layout="none">                          
    <h2 class="zoneLabel">Zone </h2> 
    <select title="Choose a Zone" style="width:22%;"  onchange="$('#zoneHidden input:first-child').val(this.value); changeDependentList(this.options[this.selectedIndex].innerHTML);">
            <option value="{!strZoneId}">{!strZoneName}</option>
                <apex:repeat id="repZone" value="{!lstZone}" var="objZone"  >                  
                    <apex:outputPanel id="optionZone" rendered="{!LOWER(objZone['label']) != LOWER(strZoneName)}" layout="none">
                        <option value="{!objZone['value']}">{!objZone['label']}</option>
                    </apex:outputPanel>
                </apex:repeat>
            </option>
    </select> 
    <span id="zoneHidden"><apex:inputHidden value="{!strZoneId}" /></span>         
</apex:outputPanel>

<apex:outputPanel id="categoryFilter" layout="none">  
    <h2>Category </h2>
    <select title="Choose a Categories" id="category" style="width:22%;"  onchange="$('#categoryHidden input:first-child').val(this.value);">

    </select>  
    <span id="categoryHidden"><apex:inputHidden value="{!strCategoryName}" /></span>  
</apex:outputPanel>              

<apex:outputPanel id="statusFilter" layout="none">           
    <h2 class="statusLabel">Status </h2>      
    <select title="Choose a Status"  id="status" style="width:22%;" onchange="$('#statusHidden input:first-child').val(this.value);"  >

    </select>  
    <span id="statusHidden"><apex:inputHidden value="{!strStatusName}" /></span>
</apex:outputPanel>  

<script>

    recordTypeMap = new Array();
    var recordTypeToStatus = [];
    var recordTypeToCategory = [];
    onloadPage();           

    function onloadPage()
    {
        sforce.connection.sessionId = '{!$Api.Session_ID}';
        // here in array we can send Record type Ids but it was giving some time wrong result , so blank array give result for all available //RecordType detail
        //result =  sforce.connection.describeLayout('Idea', new Array('099394343400034'));
        result =  sforce.connection.describeLayout('Idea', new Array());

        recordTypeName = new Array();
        recordTypeMap = result.recordTypeMappings;

        recordTypeMap.forEach(function(obj){
            recordTypeName.push(obj.name.substring(0,obj.name.indexOf(':')).toUpperCase());
        });

        recordTypeName.pop();
        recordTypeName.push('ALL');

        for(i=0;i<recordTypeMap.length;i++)
            recordTypeToStatus.push({'name':recordTypeName[i],'status': getName(recordTypeMap[i].picklistsForRecordType[1])});

        for(i=0;i<recordTypeMap.length;i++)
            recordTypeToCategory.push({'name':recordTypeName[i],'categories': getName(recordTypeMap[i].picklistsForRecordType[0])});

        changeDependentList('{!strZoneName}');

    }

    function getName(MainList)
    {
        var modiflist = new Array();
        if(MainList.picklistValues.length == undefined)
            modiflist.push(MainList.picklistValues.label);
        else
        {
            MainList.picklistValues.forEach(function(obj){
                modiflist.push(obj.label);
            });
        }
        return modiflist;
    }

    function changeDependentList(zoneName)
    {
        console.log(recordTypeName);
        console.log(zoneName.toUpperCase());

        indexToChange = recordTypeName.indexOf(zoneName.toUpperCase());

        if(indexToChange != -1){

            var statusList = recordTypeToStatus[indexToChange];

            console.log($('#statusHidden input:first-child').val());
            var currentStatus = $('#statusHidden input:first-child').val();
            console.log(statusList.status);

            $('#status').empty(); //removing old childs
            var $ele1 = $('#status');



            if(statusList.status.indexOf(currentStatus) == -1)  
            {
                $ele1.append($('<option value="All">All</option>'));
            }
            else
                $ele1.append($('<option value='+currentStatus+'>'+currentStatus+'</option>'));


            statusList.status.forEach(function(statusToAdd){

                $ele1                          
                   .append($("<option></option>")
                    .attr("value", statusToAdd)
                    .text(statusToAdd)
                   );
            });


            var categoryList = recordTypeToCategory[indexToChange];

            $('#category{!id}').empty(); //removing old childs
            var $ele2 = $('#category');
            $ele2.append($('<option value="All">All</option>'));

            categoryList.categories.forEach(function(statusToAdd){
                $ele2                          
                   .append($("<option></option>")
                    .attr("value", statusToAdd)
                    .text(statusToAdd)
                   );
            });
        }

    }


</script>  
Related Topic