Question

I am new to APEX and visualforce so I am a bit sketchy with this one... I have created the following class, but I am unsure how to truncate the output... would someone be able to explain to me as to how I would go about truncating the CommentBody field so I display this as an output field on my visualforce page that uses this class

Class:

public class displaycase {
    public string getcasetype{get;set;}
    public  void displaycase() {}
    public list<case> caselst{get;set;}

public list<selectoption>getcasestatus() {
    list<selectoption>selectopt=new list<selectoption>();
        selectopt.add(new selectoption('All' ,'All'));
        selectopt.add(new selectoption('All Open' ,'All Open'));
        selectopt.add(new selectoption('All Closed' ,'All Closed'));
        selectopt.add(new selectoption('New' ,'New'));
        selectopt.add(new selectoption('Re-open' ,'Re-open'));
        selectopt.add(new selectoption('Pending' ,'Pending'));
        selectopt.add(new selectoption('In Progress' ,'In Progress'));
        selectopt.add(new selectoption('Approved' ,'Approved'));
        selectopt.add(new selectoption('Created Story in PT' ,'Created Story in PT'));
        selectopt.add(new selectoption('On Hold' ,'On Hold'));
        selectopt.add(new selectoption('Resolved' ,'Resolved'));
        selectopt.add(new selectoption('Closed' ,'Closed'));
    return selectopt;
}
public void displaycaselist(){
    caselst=new list<case>();
    if(getcasetype == 'All'){
    caselst=[SELECT Id, (SELECT CommentBody, LastModifiedDate, CreatedBy.Name FROM Case.CaseComments ORDER BY LastModifiedDate DESC), CreatedBy.Name, CaseNumber, ClosedDate, IsClosed, IsEscalated, Priority, Reason, Status, Subject, Type FROM Case WHERE (CreatedBy.ManagerId=:UserInfo.getUserId() OR CreatedBy.Id=:UserInfo.getUserId()) AND RecordTypeId = '012C00000007l5V'];
    }
    else if(getcasetype == 'All Open') {
    caselst=[SELECT Id, (SELECT CommentBody, LastModifiedDate, CreatedBy.Name FROM Case.CaseComments ORDER BY LastModifiedDate DESC), CreatedBy.Name, CaseNumber, ClosedDate, IsClosed, IsEscalated, Priority, Reason, Status, Subject, Type FROM Case WHERE (status = 'New' OR status = 'Re-Open' OR status = 'Pending' OR status = 'In Progress' OR status = 'On Hold' OR status = 'Created Story in PT' OR status = 'Approved') AND (CreatedBy.ManagerId=:UserInfo.getUserId() OR CreatedBy.Id=:UserInfo.getUserId()) AND RecordTypeId = '012C00000007l5V'];
    }
    else if(getcasetype == 'All Closed') {
    caselst=[SELECT Id, (SELECT CommentBody, LastModifiedDate, CreatedBy.Name FROM Case.CaseComments ORDER BY LastModifiedDate DESC), CreatedBy.Name, CaseNumber, ClosedDate, IsClosed, IsEscalated, Priority, Reason, Status, Subject, Type FROM Case WHERE (status = 'Closed' OR status = 'Created Story in PT' OR status = 'Resolved') AND (CreatedBy.ManagerId=:UserInfo.getUserId() OR CreatedBy.Id=:UserInfo.getUserId()) AND RecordTypeId = '012C00000007l5V'];
    }
    else {
    caselst=[SELECT Id, (SELECT CommentBody, LastModifiedDate, CreatedBy.Name FROM Case.CaseComments ORDER BY LastModifiedDate DESC), CreatedBy.Name, CaseNumber, ClosedDate, IsClosed, IsEscalated, Priority, Reason, Status, Subject, Type FROM Case WHERE status=:getcasetype AND (CreatedBy.ManagerId=:UserInfo.getUserId() OR CreatedBy.Id=:UserInfo.getUserId()) AND RecordTypeId = '012C00000007l5V'];
    }
}
}

Thanks in advance!!!

Visualforce code:

<apex:page controller="displaycase">
<apex:includeScript value="{!$Resource.HistoryJS}"/>
<apex:stylesheet value="{!$Resource.HistoryCSS}" />
     <apex:form id="frm"><div>Please select a Case Status</div>
         <apex:selectList size="1" value="{!getcasetype}" title="Please Select a Case Status" multiselect="false">
             <apex:selectOptions value="{!casestatus}"></apex:selectOptions>
             <apex:actionSupport event="onselect" action="{!displaycaselist}"/>
         </apex:selectList>
        <apex:commandButton action="{!displaycaselist}" value="Go" id="theButton"/>
         <apex:outputLabel id="pnl1" >
             <apex:pageBlock >
                 <apex:pageblocktable value="{!caselst}" var="cse" styleClass="table" rowClasses="odd,even" cellpadding="5" border="1">
                         <apex:column >
                            <apex:facet name="header">Case Number</apex:facet>
                            <apex:outputPanel onclick="toggle_visibility('{!cse.CaseNumber}');" layout="block" styleClass="column1">{!cse.CaseNumber}</apex:outputPanel>
                            <div id="{!cse.CaseNumber}" class="list" style="display: none; cursor: pointer;" onclick="showHistory('{!cse.Id}');"><p><b>Most recent Case Comment:</b><br />{!cse.CaseComments[0].CommentBody}</p></div>
                        </apex:column>
                        <apex:column >
                            <apex:facet name="header">Created By</apex:facet>
                            <apex:outputfield value="{!cse.CreatedBy.Name}"/>
                            </apex:column>
                        <apex:column >
                            <apex:facet name="header">Case Subject</apex:facet>
                            <apex:outputfield value="{!cse.Subject}"/>
                            </apex:column>
                         <apex:column >
                            <apex:facet name="header">Case Type</apex:facet>
                            <apex:outputfield value="{!cse.Type}"/>
                         </apex:column>
                         <apex:column >
                            <apex:facet name="header">Case Priority</apex:facet>
                            <apex:outputfield value="{!cse.Priority}"/>
                         </apex:column>
                         <apex:column >
                            <apex:facet name="header">Case Reason</apex:facet>
                            <apex:outputfield value="{!cse.Reason}"/>
                         </apex:column>
                         <apex:column >
                            <apex:facet name="header">Escalated</apex:facet>
                            <apex:outputfield value="{!cse.IsEscalated}"/>
                         </apex:column>
                         <apex:column >
                            <apex:facet name="header">Closed</apex:facet>
                            <apex:outputfield value="{!cse.IsClosed}"/>
                         </apex:column>
                         <apex:column >
                            <apex:facet name="header">Case Close Date</apex:facet>
                            <apex:outputfield value="{!cse.ClosedDate}"/>
                            </apex:column>
                 </apex:pageblocktable>
            </apex:pageBlock>
         </apex:outputLabel>
     </apex:form>
</apex:page>

Javascript used:

function showHistory(cid) {
  var content = '';
    content += '<div id="frameParent">';
    content += '<div id="blackoutDiv" onclick="hideHistory()"></div>';
    content += '<div id="frameHolder"><a class="boxclose" id="boxclose" onclick="hideHistory()"></a>';
    content += '<iframe src="https://c.cs7.visual.force.com/apex/casecomments?cid=' + cid + '" width="60%" height="60%" id="historyFrame"></iframe>';
    content += '</div>';
    content += '</div>';
    console.log(content);
    document.getElementById('contentWrapper').innerHTML += content;
}
function toggle_visibility(id) {
    var e = document.getElementById(id);
    if (e.style.display === 'block') {
        e.style.display = 'none';
    } else {
        e.style.display = 'block';
        hideAllBut(id);
    }
}
function hideAllBut(id) {
    var lists = document.querySelectorAll('div.list');
    for (var i = lists.length; i--;) {
        if (lists[i].id != id) {
            lists[i].style.display = 'none';
        }
    }
}
function hideHistory() {
    var el = document.getElementById('frameParent');
    el.parentNode.removeChild(el);
}
document.onkeydown = function(evt) {
    evt = evt || window.event;
    if (evt.keyCode === 27) {
        hideHistory();
    }
}
document.onkeyup = function(evt) {
    evt = evt || window.event;
    if (evt.keyCode === 27) {
        hideHistory();
    }
};

The above is the code that I am using, I know how to create basic Visualforce pages, it's just the APEX Class that I am a little stuck with and I am unsure how I can truncate the CommentBody so if someone post a comment that is a couple of thousand characters long it will only display the first 100 or so.

Was it helpful?

Solution

You can do something like

caselst=[SELECT Id, (SELECT Id,CommentBody FROM CaseComments ORDER BY LastModifiedDate DESC limit 1) FROM Case ];
for(Case c : caselst){
  for(CaseComment  cComment : c.CaseComments ){
      integer i = cComment.CommentBody.length();
      if(i>100)
         cComment.CommentBody = cComment.CommentBody.substring(0,100);
  }
}

OTHER TIPS

I think the below code should help you out.

<apex:page controller="displaycase">
    <apex:form>
        <!--display the picklist value-->
        <apex:select value="{!getcaseType}" onchange="refreshList">
            <apex:selectOptions value="{!casestatus}"/>
        </apex:select>
        <apex:actionFunction action="{!displaycaselist}" name="refreshList" status="statusCheck" reRender="pageBlock"/>
        <apex:actionStatus startText="Searching..." stopText="" id="statusCheck"/>
        <apex:pageBlock id="pageBlock">
        <!-- Display the list of case. iterate thru the case list-->
            <apex:pageBlockTable value="{!caselst}" var="cList">
                <apex:column headerValue="caseNumber">
                    <apex:outputText value="{!cList.caseNumber}"/>
                </apex:column>
                <apex:column headerValue="Comments">
                <!--Iterate thru the list of the case comments for each case.-->
                    <apex:repeat value="{!cList.CaseComments}" var="comment">
                        {!comment.commentBody} <br/>
                    </apex:repeat>
                </apex:column>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

You should also read thru the VF documentation for more information.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top