Javascript in “Conditions” of Action calls & WNR

One who have got some exposure on USD, would have definitely observed the big “Condition” box in Action Calls & Window navigation rules (and may be any other entities as well which I haven’t observed so far) where we can mention condition expression which will be evaluated at the run time. Based on the result the corresponding Action call or WNR will execute further.

A very simple example as shown below means – Execute the Action call only if it is “Account” session
image

The idea of this post is to explore what else we can use as conditions ? It turned out pretty nice experiment – One can use almost all JS functions. Except the once which are dependent on Browser object (i.e. Window, Navigator, Screen, History and Location).

Let me quickly go with some examples so that you will have some idea on what I’m trying to say over here.

NOTE: All the below example conditions are tested in debugger view and found working fine.

Ex1: Execute Action call only if the Contact’s Country is USA/US/United States/United States Of America

var cntry="[[contact.address1_country]+]".toLowerCase();
cntry=="us" || cntry=="usa"|| cntry=="united states"||cntry=="united states of america"
image

Note the usage of “toLowerCase()” which is a javascript function, basically we are converting the country in lower case first and then comparing it against the possible values.

Ex2: Execute Action call if the First 2 letters of Customer’s post code is “AB” (I know that sounds a crazy requirement Smile)

"[[contact.address1_postalcode]+]".substring(0,2)=="AB"
image

Observe the usage of Substring, which again is a JS function.

In the same way we can use all other string related functions like indexOf, length, search, match, split, trim etc. Its all just your requirement and find out which one would work for you.

Apart from string functions, Date functions also fulfill some nice requirements.

Ex3: Consider a requirement, If the customer is Created before 2010 then open X lob application else open Y lob application. Isn’t it sounding like a valid requirement ? Lets see how we can achieve this with conditions.

Note: As the main concentration is on Conditions in this post, I’m not going to explain how do we create action calls, associate to events etc over here. Refer Neil’s blog where you can find numerous examples on this front.

Create 2 action calls to navigate X & Y Applications. The condition for X application should be

//Condition for X Application

new Date(“[[Contact.createdon]+]”).getYear()<=2010

image
The condition for Y Application should be
//Condition of Y application

new Date(“[[Contact.createdon]+]”).getYear()>2010

image

Observe how we have converted the CreatedOn field in to Date and then took the Year part of the date.

Ex4: Lets say the requirement is, If the contact is created in last 30 days then Open “Recently_Created_Contact_Billing_LOB Application”. Not so crazy requirement, Isn’t it ? Lets see how the condition looks like for this.

new Date("[[Contact.createdon]+]") >=new Date().setDate(new Date().getDate()-30)

In the first look it might be looking slightly complex but all we are doing is, removing 30 days from today and forming a new Date which we are comparing against contact created date.

Ex5: One final example. This time with out any replacement parameters and pure javascript methods. Scenario – On the Last Friday of the month, When ever agent opens USD display a message saying “Today is Last Friday, Don’t forget to update Timesheets & Leaves”. If you see here, This is a generic message to user w/o any session/replacement parameters. Lets see the how the condition looks like.

function LastFridayOfMonth() {
var year=new Date().getYear();
var month=new Date().getMonth()+1;
var i, last_day;
i = 0;
while (true) {
last_day = new Date(year, month, i);
if (last_day.getDay() === 5) {
return last_day;
}
i -= 1;
}
};
LastFridayOfMonth().getDate()==new Date().getDate()
image

Well, The condition might be looking like a beast. But the point to note over here is it is not just single line statemetns rather we can even create big functions which does some crazy caluclations as part of our conditions. (ps: the code for LastFridayOfMonth function is found with some random googling, not pretty sure how far it is working correctly – please test once again if you want to use it).

That’s it for now. Hope this post helps in gaining some idea on how we can use Javascript in conditions. You just need to figure out what function would help you and start using !!

HAPPY NEW YEARSmile,  Probably my last post for the year, See you next year Smile

ps: If you find any limitations in using any JS function, it would be nice if you can post it in comments. Helps others also with limitations.

 

6 thoughts on “Javascript in “Conditions” of Action calls & WNR

  1. Shradha

    Thank you , But I am facing issue related to action call conditions. I am using USD 4.0 but unable to execute any action call where condition is written.
    Please guide me on the same. looks like action call didn’t run because a condition failed.
    Please find the detailed of one of the action: [I tried with simple conditions as well, looks like some issue in USD configuration]
    Source:Debugger
    Name:Expander Right Panel Collapse
    Application:Custom Panel
    Action:SetVisualProperty
    Action Data:elementname=RightPanelExpander
    propertyname=IsExpanded
    value=false
    Parameters:
    Condition:”[[$Session.Count]g+]” == “1” && “[[$Session.AssociateChat]g+]”==”” /* Closed event happens before variable is updated */
    Condition Result:ConditionFailed

    Like

    1. ansrikanth

      Hi, I hope these double quotes (“) are regular Javascript quotes (not those fancy quotes that word documents will automatically convert). That is the first step I will look in to.

      Normally, whenever I face such a problem, I will check them with a showmessage action. In your debugger, just place this ”[[$Session.Count]g+]” as data parameter to Showmessage action (GlobalManager Hosted control) and see what is the value you are getting. That way you can easily check/compare and predict the result of the condition.

      Let me know if you need more details.
      -Srikanth

      Like

  2. Jatin Solanki

    Hi Shrikanth,

    Thank you so much for this amazing blog. It has helped me a lot.
    I have one query related to the Action call condition. Can you please help me with this?
    I have one “action call”. In the action call condition, I want to read the value of Hosted control name on the same record. Is it possible to get the hosted control name?
    Kindly help me to achieve this. Please let me know if you need more information.

    Thank you.

    Like

Leave a comment