I’m referencing this topic to try to format a template field. My template field displays child records - Case Comments, in a Cases table - and I’d like to limit the number of characters displayed in each case comment. I’m using the formatDateTime function in the snippet that Zach provided, but need a javascript function that will limit the characters in my case comments field. Here’s what I have - maybe this is an easy fix for someone who is good at javascript. I’ve bolded the parts of the code that I need to adjust.
var field = arguments 0], row = field.row, $ = skuid.$, userLocale = skuid.utils.userLocale;
var childData = row.CaseComments;
var DELIMITER = ’
';
var DATE_FORMAT = ‘m/d/yy’;
var TIME_FORMAT = ‘h:mm a’;
if (childData && childData.totalSize) {
// Run the default text renderer with nothing,
// just to get our field element
skuid.ui.fieldRenderers.TEXT.read(field,'');
var formatDateTime = function(sfDateTime,dateFormat,timeFormat){
if (sfDateTime) {
var jsDate = skuid.time.parseSFDateTime(sfDateTime),
localDate = skuid.time.getLocalDateTime(jsDate);
if (localDate) {
var formattedTime = skuid.time.formatTime(timeFormat,localDate),
formattedDate = $.datepicker.formatDate(dateFormat,localDate);
return userLocale.showTimeBeforeDate
? formattedTime + ' ' + formattedDate
: formattedDate + ' ' + formattedTime;
}
}
return '';
};
// How do I write a function to limit characters to 20? Below is my attempt…
var formatComment = function(){
var string = ;
var length = 20;
var trimmedString = string.substring(0,length);
};
var output = '';
$.each(childData.records,function(i,comment){
output +=
// Format the DateTime in our desired time and date formats
formatDateTime(
comment.CreatedDate,
DATE_FORMAT,TIME_FORMAT
)
+ ": "
// Here’s where I need to reference formatComment; not sure if I’m doing it correctly
+ formatComment(
comment.CommentBody
);
// Append delimiters as long as we're not on the first record
if ((i+1) !== childData.totalSize) {
output += '<span>' + DELIMITER + '</span>';
}
});
field.element.html(output);
} else {
skuid.ui.fieldRenderers.TEXT.read(field,'-');
}