2

Is it possible to have logic on a parsed JSON list? I have my entire list saved into a variable 'info' and and want to look at the parsed information and write a conditional for it. My end goal is to put a 'hr' line break in between the changed priority numbers. Because it is dynamically creating the list as the Agent Emails are updated...I am not sure where or how to put the line break logic. I hope I am just overlooking something super simple.

enter image description here

My thought was that I can loop through the 'info' list and write an if statement in a method. Then in my VFP, I could loop through the method. But the code I was trying does not know what what PriorityNumber is. This leads me to believe that because of the formatting of the data, I can not put conditional statements on it.

for (Integer i = 0; i < Info.AgentInfoList; i++) 
    {
        if (Info.AgentInfoList.PriorityNumber == 0)
        {
            return Info.AgentInfoList.PriorityNumber;      
        }               

My Controller:

//https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_json_jsonparser.htm
public class WSController {
    public JSON2Apex info { get; set; }


String jsonStr = '{"DateModifiedUtc":"2016-09-07T20:12:47.1519119Z",' + 
    '"AgentInfoList":[{"Username":"[email protected]","PriorityNumber":0}, ' +
    '{"Username":"[email protected]","PriorityNumber":0},{"Username":"[email protected]","PriorityNumber":0},' +
    '{"Username":"[email protected]","PriorityNumber":0},{"Username":"[email protected]","PriorityNumber":0},' +
    '{"Username":"[email protected]","PriorityNumber":0},{"Username":"[email protected]","PriorityNumber":0},' +
    '{"Username":"[email protected]","PriorityNumber":2}]}';


//wrapper class
public class JSON2Apex 
{
    public String DateModifiedUtc { get; set; }
    public List<AgentInfoList> AgentInfoList { get; set; }
}
public class AgentInfoList 
{
    public String Username { get; set; }
    public String PriorityNumber { get; set; }
}
public static JSON2Apex parse(String json) 
{
    return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
}

public WSController() 
{
    info = (JSON2Apex)parse(jsonStr);

}

public void priorityFilter()
{
    //loop through the info.AgentInfoList and pull out all of the records with a Priority number of 0.
    for (Integer i = 0; i < Info.AgentInfoList + 1; i++) 
    {
        if (Info.AgentInfoList.PriorityNumber == 0)
        {
            return AgentInfoList.PriorityNumber;

        }           
    }
}
}

visualforce page pulling out the priority and email:

<apex:page controller="WSController" >
<!--TO DO:: if Priority Number changes, put a <hr/> break-->

<style type="text/css">
    p { font-style: italic; }
</style>


<h1>Agent Call Distribution</h1>
<br/>
<p>Below is the current priority that Five9 will attempt to distribute new inbound phone mail leads. Loan Specialists high on the list are more likely to be offered a new sales call.</p>
<p>List last updated: {!info.DateModifiedUtc}</p>
<br/>

<apex:pageBlock >
    <apex:pageBlockTable value="{!info.AgentInfoList}" var="AgentInfo">
        <apex:column headerValue="Priority">
            <!--apex:outputtext value="!IF(info.AgentInfoList.PriorityNumber == '0', '0', ''))}"/-->
            <apex:outputField value="{!AgentInfo.priorityNumber}"/>
        </apex:column>
        <apex:column headerValue="Agent Email">
            <apex:outputText value="{!AgentInfo.Username}" />
        </apex:column>
    </apex:pageBlockTable>
</apex:pageBlock>

4
  • Are you just trying to visually distinguish? You could add somewhere styleClass="{!IF(MOD(agentInfo.priorityNumber, 2) = 0, 'grey', '')}" and then add this style to the class: background-color: LightGray.
    – Adrian Larson
    Commented Sep 12, 2016 at 22:58
  • @AdrianLarson that's a good idea, the original requirement was to put a line break between the priorities when they changed numbers but this could be a good work around
    – Olivia
    Commented Sep 12, 2016 at 23:19
  • 2
    You could sort by priority number then place them in a collection with the priority number as a key. Use two repeats and have separate Pageblocktables for each priority....It really all comes down to how you want it to look. Key is get the data like you need it then display like you want it. Don't modify the data to fit a rogue requirement. If the requirement changes its easier to change the UI than to change the data
    – Eric
    Commented Sep 13, 2016 at 0:40
  • @AdrianLarson would you like to post your answer so I can mark it correct?
    – Olivia
    Commented Sep 14, 2016 at 14:12

1 Answer 1

2

My suggestion here is to add a styleClass conditionally to your <apex:outputField> tags, and define a class that sets the background-color. Something like:

<style>
    .grey { background-color: LightGrey; }
</style>

<!--intermediate markup-->
<apex:outputText styleClass="{!IF(MOD(agentInfo.priorityNumber, 2) = 0, 'grey', '')}" />

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .