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,

 

It means that your length is not getting calculated that is why you are not able to get inside the else if.

So can you put three logs statement and check whether the values are coming or not

first one is

gs.log(producer.description); //Description

gs.log(producer.description.toString().length); //Description Length

gs.log(producer.description.toString());//Description in strin

 

Please let me know the output of the logs.

Thanks,

CB

 

Those all returned as expected.  The length was returned as 364 while the other two just output that description itself. 

OK, I figured it out.  I had to add .toString() in both places.  Here's my updated script that is fully functional:

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 > 65){
		current.short_description = producer.description.toString().substring(0, 65);
	} else {
		current.short_description = producer.description;
	}

Nikhil Bahurup1
Tera Expert

Hi Danny,

Please check if else if condition gets executed here.

You can put gs.info("description length " +producer.description.length) before the if statement and check the output in logs if its greater than 45 or not.

Hi Nikhil,

The log returned "description length undefined" as the message.  The length of text is more than double 45 characters.  I picked a large text block to test with.  The short description get's populated but with a longer version of the text, which means it's clearly failing the length check but I'm not sure why.