Record producer script is not populating short description properly when using substring modifier

Danny Rhoades
Kilo Guru

Here's the portion of the record producer script I'm having trouble with:

var gsi = producer.gaming_system_issue;
if (gsi == 'Yes') {
	current.category = 'gaming';
	current.short_description = 'Gaming Issue: ' + producer.business_service.name;
} else if (producer.description.length > 45) {
		current.short_description = producer.description.substring(0, 45);
	} else {
		current.short_description = producer.description;
}

The first if statement works perfectly for the category and short description. However, the next else if statement doesn't work.  It never truncates the description per the substring criteria. I still end up with most of the description becoming the short description.  I've tried using current.comments = producer.description.toString(); earlier in the script but that didn't work either. 

Not sure what I'm doing wrong.

1 ACCEPTED SOLUTION

try this:

var gsi = producer.gaming_system_issue;
var description1 = producer.description.toString();
if (gsi == 'Yes') {
current.category = 'gaming';
current.short_description = 'Gaming Issue: ' + producer.business_service.name;
} else if (description1.length > 45) {
current.short_description = description1.substring(0, 45);
} else {
current.short_description = description1;
}

View solution in original post

12 REPLIES 12

Hi Danny,

Please check if you are using correct fieldname i.e. description in this case. If the fieldname is correct then try converting producer.description into string by using producer.description.toString() and then use .length to check the length of string.

try this:

var gsi = producer.gaming_system_issue;
var description1 = producer.description.toString();
if (gsi == 'Yes') {
current.category = 'gaming';
current.short_description = 'Gaming Issue: ' + producer.business_service.name;
} else if (description1.length > 45) {
current.short_description = description1.substring(0, 45);
} else {
current.short_description = description1;
}

This worked perfectly!  I like this much better than my alternative solution which is below.  So I'm marking this as correct, since it's a more elegant solution.

Alternative option is by adding .toString() to both spots like this:

var gsi = producer.gaming_system_issue;
if (gsi == 'Yes') {
	current.category = 'gaming';
	current.short_description = 'Gaming Issue: ' + producer.business_service.name;
} else if (producer.description.toString().length > 45) {
		current.short_description = producer.description.toString().substring(0, 45);
	} else {
		current.short_description = producer.description;
	}