REPLACE_PATTERN function ->result 'empty'

If you think that something doesn't work in Aware IM post your message here
Post Reply
weblike
Posts: 1165
Joined: Sun Dec 02, 2012 12:00 pm
Location: Europe

REPLACE_PATTERN function ->result 'empty'

Post by weblike »

This topic is based on this post: https://www.awareim.com/forum/viewtopic.php?f=4&t=9026
I try to use REPLACE_PATTERN function with some attributes, but if one attribute is empty->the result of the function is empty attribute also.

For example:
Orders.CustomerFirstName = Jane
Orders.CustomerLastName = Doe

Creating another BO based on Orders.BO with these rules => the result is ok.
Rule 1:

Code: Select all

If Messages.HTML CONTAINS '{CustomerFirstName}' Then 
Messages.HTML=REPLACE_PATTERN(Messages.HTML,'\{CustomerFirstName\}',Messages.Owner.Customer_FirstName)
Rule 2:

Code: Select all

If Messages.HTML CONTAINS '{CustomerLastName}' Then 
Messages.HTML=REPLACE_PATTERN(Messages.HTML,'\{CustomerLastName\}',Messages.Owner.Customer_LastName) 
BUT if one of the fields are empty => the result of REPLACE_PATERN is also empty
Orders.CustomerFirstName = 'Jane'
Orders.CustomerLastName = ''

https://streamable.com/3g3yf

Here is a sample BSV:
1. Create one Order (complete Customer First Name, Customer Last name)
2. Create a message template with both 2 tokens {CustomerFirstName} {CustomerLastName}
2. In Orders list query -> click "Create Message" button
==> inspect the result in "Messages" query (you should see the message with entered info in Orders attributes)
3. Edit the Order and empty one field
4. In Orders list query -> click "Create Message" button -->inspect the created message ->the result will be an empty field.
Attachments
replace_pattern.zip
(40.25 KiB) Downloaded 990 times
Thx,
George
________________________________
Developer Edition
AwareIM: v8.5, build 2824
OS: Windows Server 2012
DB: MySql 5.6.42
Jaymer
Posts: 2528
Joined: Tue Jan 13, 2015 10:58 am
Location: Tampa, FL
Contact:

Re: REPLACE_PATTERN function ->result 'empty'

Post by Jaymer »

i would say "look at the log"
you think its NOT firing, but it MUST be firing (the REPL)

and, make it easy on yourself (and more readable code), quit having to use the extra \ to escape the {

just make your insert tokens be !Fname! instead of {Fname}

the user doesn't care cause he is not even typing them in.
Click Here to see a collection of my tips & hacks on this forum. Or search for "JaymerTip" in the search bar at the top.

Jaymer
Aware Programming & Consulting - Tampa FL
weblike
Posts: 1165
Joined: Sun Dec 02, 2012 12:00 pm
Location: Europe

Re: REPLACE_PATTERN function ->result 'empty'

Post by weblike »

Hello Jaymer,
I use the log viewer allt the time when I these kind of cases.
Changed the curly brackets with other annotations, not the problem here.
I think this is a bug. REPLACE_PATTERN should take empty field as '' and replace only the argument I set into it, not entire attribute.
Selection_999(131).png
Selection_999(131).png (61.6 KiB) Viewed 12567 times
Thx,
George
________________________________
Developer Edition
AwareIM: v8.5, build 2824
OS: Windows Server 2012
DB: MySql 5.6.42
Jaymer
Posts: 2528
Joined: Tue Jan 13, 2015 10:58 am
Location: Tampa, FL
Contact:

Re: REPLACE_PATTERN function ->result 'empty'

Post by Jaymer »

well thats simple

IF html contains !thetag! and replacement_value is DEFINED THEN
replace_pattern
Click Here to see a collection of my tips & hacks on this forum. Or search for "JaymerTip" in the search bar at the top.

Jaymer
Aware Programming & Consulting - Tampa FL
weblike
Posts: 1165
Joined: Sun Dec 02, 2012 12:00 pm
Location: Europe

Re: REPLACE_PATTERN function ->result 'empty'

Post by weblike »

Jaymer wrote:well thats simple

IF html contains !thetag! and replacement_value is DEFINED THEN
replace_pattern
What you say leaves the !thetag! untouched =>the result is "Hello Jane !thetag! " ....and this involves an ELSE statement... Imagine in real life app I have at least 15-20 tokens to replace...
As described in source post I use this for Email Templates....which contains 2 attributes "Subject" & "Body"...that result 20 x 2 =40 IF's ELSE statements...
This should be kept simple and not give to the app not needed high consuming operations. REPLACE_PATTERN should deal with a simple '' .
Thx,
George
________________________________
Developer Edition
AwareIM: v8.5, build 2824
OS: Windows Server 2012
DB: MySql 5.6.42
BobK
Posts: 555
Joined: Thu Jan 31, 2008 2:14 pm
Location: Cincinnati, Ohio, USA

Re: REPLACE_PATTERN function ->result 'empty'

Post by BobK »

weblike wrote:I think this is a bug.
It is not a bug, it is a java feature :lol:

A lot of java functions return null if any of the parameters are null. I suspect that is what is happening here.

One solution is:
change your rules to

Code: Select all

If Messages.HTML CONTAINS '{CustomerLastName}' Then 
Messages.HTML=REPLACE_PATTERN(Messages.HTML,'\{CustomerLastName\}',(Messages.Owner.Customer_LastName+'~'))
which is adding ~ to the replacement value so any null attribute is no longer null.
Note: your can use any character besides ~ as long it is not used anywhere else in the message.

Then add the following rule to remove all of the ~

Code: Select all

If Messages.HTML CONTAINS '~' Then 
Messages.HTML=REPLACE_PATTERN(Messages.HTML,'~','')
Bob
Jaymer
Posts: 2528
Joined: Tue Jan 13, 2015 10:58 am
Location: Tampa, FL
Contact:

Re: REPLACE_PATTERN function ->result 'empty'

Post by Jaymer »

BobK wrote:It is not a bug, it is a java feature
LOL

Bobk, yes, that'll do it.
you are right George, I forgot about that. I wasn't thinking about that part being null - guess I didn't read it correctly (twice).
Click Here to see a collection of my tips & hacks on this forum. Or search for "JaymerTip" in the search bar at the top.

Jaymer
Aware Programming & Consulting - Tampa FL
weblike
Posts: 1165
Joined: Sun Dec 02, 2012 12:00 pm
Location: Europe

Re: REPLACE_PATTERN function ->result 'empty'

Post by weblike »

BobK wrote: It is not a bug, it is a java feature :lol:
LOL :lol: :lol: :lol:
Thx,
George
________________________________
Developer Edition
AwareIM: v8.5, build 2824
OS: Windows Server 2012
DB: MySql 5.6.42
Post Reply