Go Back   MarcomCentral (PTI) and FusionPro User Communities > Software-Related Talk > The JavaScript Library > Getting Started, Learning, Etc.


Thread Tools Search this Thread Display Modes
Old March 16th, 2010, 01:33 PM
Tattoued Tattoued is offline
Junior Community Member
Join Date: Mar 2010
Posts: 30
Question Replacing text - complex?

I'm VERY new to FusionPro, and don't have much of a clue when it comes to javascript, so forgive me ahead of time. Here's what I'm trying to do:

The first few words of one paragraph is (John is used as a sample) "John, need to...". What I'm trying to do and keep getting a syntax error on is if the first name field is blank, replace the comma and word "need" with just the word "Need" with a capital N. I have another paragraph that's very similar with the same dilemma. Do I create a new rule using a "replacestring" command? I think that's right, but I'm not totally sure, and can't seem to find the answer in the manual.

Help? Thanks!
Reply With Quote
Old March 16th, 2010, 01:50 PM
esmith esmith is offline
Senior Contributor
Join Date: Oct 2008
Location: Charlotte, NC
Posts: 869
Default Re: Replacing text - complex?

It sounds like only the "<<name>>, N" is variable and the rest of the text is static, right? If so, you could have a rule that checks to see if the value of the name field is empty and adjust just the quoted portion above, followed by the rest of the text entered manually in the text editor window.
if (Field("Name") != "") {
   return Field("Name") + ", N";
else return "N";
There would, of course, be no space between the rule and the "eed to..." in your text editor.
Reply With Quote
Old March 17th, 2010, 06:13 AM
Tattoued Tattoued is offline
Junior Community Member
Join Date: Mar 2010
Posts: 30
Default Re: Replacing text - complex?

OMG you ROCK! I can't tell you how much time I've been spending on this researching and flipping through the manual, and googling, etc. My IQ is fairly high, but javascript makes me feel like it's about 12! LOL! It was probably so basic for you...thanks so much! I'll learn it - it'll just take time (something I don't have much of)!

So... when would you use the replace string command? I was way off base on that one, eh?

Last edited by Tattoued; March 17th, 2010 at 06:16 AM..
Reply With Quote
Old March 18th, 2010, 07:53 AM
mhilger's Avatar
mhilger mhilger is offline
Senior Contributor
Join Date: Aug 2008
Location: Chicago
Posts: 325
Default Re: Replacing text - complex?

Hello Tattoued,

Welcome to the power of JavaScript in FusionPro! While it might seem daunting to learn JS for use with the product, most longer-time users of the product will tell you that it's absolutely indispensable to your VDP workflow. Feel free to use this forum to pose questions to the community and we'll help you out with sample script. After a certain time, you will likely start seeing common needs that JS can solve for you and you'll be writing your own scripts no problem.

To answer your question about using the replace string command, you were not way off on that line of thinking at all. Some JS could have been written using the ReplaceSubstring() function to accomplish what you were looking to accomplish. That solution would have been "right" as well in that it accomplished the task at hand. Eric's script was just a simpler way to accomplish the task as opposed to using ReplaceSubstring().

So when would you use ReplaceSubstring()? There are a ton of applications for this but I think one of the most useful is that of replacing some text within a field with alternate text.

Let's say you have some data for a restaurant with a list of menu items. Perhaps this data uses "food service industry shorthand" for some of the names of the products. So the Chicken Salad item might be in the data as "Chk. Salad". Another field might have "Chicken Sandwich" as "Chk. Sandwich". So you don't want to typeset this shorthand on your piece - you want to replace any text that is "Chk." with "Chicken". Great reason to use ReplaceSubstring() to accomplish this.

menuItem = Field("MenuItem1");

returnText = ReplaceSubstring(menuItem, 'Chk.', 'Chicken');

return returnText;
One theme you'll notice is that there are a number of ways to skin the JS cat, if you will. You'll probably see some posts where 1 user posts maybe 10 lines of JS and another replies with an alternate solution that uses only 3 lines of JS. Both solutions are right! JavaScript has a lot of flexibility in this way so there's no single magic way that a user needs to figure out to accomplish most tasks. Any script that accomplishes a given task is "right" by definition.

If you have some time, check out this training video - especially the last 2 sections of it which talks about select JavaScript rule items: JavaScript Video
--Mark Hilger
Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

All times are GMT -7. The time now is 02:33 PM.

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2022, Jelsoft Enterprises Ltd.
(c) 2011, PTI Marketing Technologies™, Inc.