Best answer

Problems with Splitting with javascript

  • 14 November 2022
  • 9 replies
  • 56 views

Userlevel 1

Im trying to fix this split of a string into 5 different values. The separators are unique (but I am not very good with code). Only get this error when I try this:

 



var content = inputData.content;
var Customer = content.split("*Customer: *")[1].split(" *M")[0].trim();
var MRR = content.split("RR: * ")[1].split("*Cancellation ")[0].trim();
var Termination = content.split("date: * ")[1].split(" *Order en")[0].trim();
var Churn = content.split("d date: * ")[1].split(" *Rea")[0].trim();
var Reason = content.split("son: * ")[1].split("")[0].trim();

output = [{content, Customer, MRR, Termination, Churn, Reason}];

This is the input string im trying to split:
 

*Churn* submission from @xx.xxx *Customer: * Name of customer *MRR: * 59390 *Cancellation date: * 02/11/22 *Order end date: * 14/11/23 *Reason: * Low activity / Never Active *Other: * "Reason for termination"

How do I define the parts of the string?

icon

Best answer by Todd Harper 21 November 2022, 16:59

View original

9 replies

Userlevel 4
Badge +3

I just tested this and it works fine. Can you confirm you actually included the input data in the input data field as seen in the screenshot below?

Also, a couple of recommendations for your code:

  1. I’d use full words for the split strings (ie. “Reason: * ” instead of “son: * ”. This will make it much easier to read.
  2. In your “Reason” variable, you made the second split double quotes adjacent to one another (ie. “”). In this test case, that returns “L”. If you want it to read “Low activity / Never Active”, be sure to make that last split read, “Other: *”. Or if it should be the rest of the content block all the way through “termination”, you can just get rid of it and leave the line as:
    var Reason = content.split("Reason: * ")[1].trim();
  3. For consistency, I would recommend either including a space after the asterisk in “Customer: *” for your first variable or removing that extra space in the others. Using .trim() makes it irrelevant whichever you choose, but it’s nice and pretty to have consistent code.

     

 

Userlevel 1

The input data is collected from slack, so it has that content, but is a dynamic variable. It currently looks like this: 

 


The reason I am splitting the text is because I need to use both ends (unique). And it still doesnt work for me.

Userlevel 4
Badge +3

@adam hjort That looks right to me, and so it’s pretty bizarre that it’s working for me and not for you. Try following my instructions in the video below and hopefully we can isolate the root of the issue.

 

Userlevel 1

@Todd Harper that is crazy. I have the exact same code and I get the same error also when using the text instead of the dynamic content.

Userlevel 1

@Todd Harper Seems like the only split that is working for me is the MRR-split. The other ones gives me that error

Userlevel 4
Badge +3

@adam hjort After commenting out each variable, using the static content was going to be my next suggestion. Did you already comment out each variable and get the error each time?

Userlevel 1

@Todd Harper , yes, the only one that is working is the MRR-split 

 

Userlevel 4
Badge +3

@adam hjort In your copy/paste code above, you had “var Customer” with a capital ‘C’, but in your screenshot here, you have a lower case ‘c’. Did you change this recently? Had you matched the case in the output statement?

 

Regardless, this would have yielded a reference error rather than a type error, so it shouldn’t be related…

Userlevel 4
Badge +3

For anybody encountering this issue in the future and searching the Zapier Community for the answer, @adam hjort and I took this to a Zoom meeting to resolve and it turns out that the input data being sent to the step had some additional spaces between some of the colons and the text, so the split functions were not correctly identifying the segments to split.

 

There are a handful of ways to fix this issue. The method we used was to simply add those additional spaces into the code. You could also remove any instances of double spaces with regex prior to performing the split functions by typing:

content = content.replace(/  +/g, ' ');

(Of course, you may run into issues here if you ever have an instance where a double space is intentional and necessary.)

Reply