Issue with Syntax of Client Script

Damian Mudge
Tera Expert

I hope you can help.

 

I created a simple Client Script on the Incident Table to make a date field "Read Only" depending on the values / entry of three specific fields. I have struggled for the syntax for the relevant Date field being populated (is NOTNULL / not empty), and after doing some reading I believe that I should be using "&&" instead of "||".  So I re-worked the Syntax replacing the "||" with "&&" and the script fails and still have not found a solution for NOTNULL / not empty / populated

 

The Business Scenario is:

 

if an Account is flagged as an "Integrated Customer" and the "On hold reason" is "Awaiting Integrated Customer" and the "On hold until" date is populated, the set the  "On hold until" field as read only. Admin role overrides. And if any of these are not true then the field will remain editable.   

 

A slight complication may be that the "Integrated Customer" flag field is a "Reference" to the Account Table.

 

The script that appears to work but I question is as follows:

 

function onLoad() {

    if (g_user.hasRole('admin'))
        return;
{
    var ohd = g_form.getValue('u_on_hold_until_date');
    var ic = g_form.getValue('u_integrated_d2d');
    var hr = g_form.getValue('hold_reason');

    if ('ohd', '!==', '' || ic == 'true' || hr == '6')
        g_form.setReadOnly('u_on_hold_until_date', true);
}
}

 

When I changed the Syntax to this it fails:

 

function onLoad() {

    if (g_user.hasRole('admin'))
        return;
{
    var ohd = g_form.getValue('u_on_hold_until_date');
    var ic = g_form.getValue('u_integrated_d2d');
    var hr = g_form.getValue('hold_reason');

    if (ohd != '' || ic == true || hr == '6')
        g_form.setReadOnly('u_on_hold_until_date', true);
}
}

 

I just can't work out what the Syntax for the IF statement should be:

 

if ('ohd', '!==', '' || ic == 'true' || hr == '6')

 

to achieve this 

 

IF the "On hold date" populated  AND the "integrated Customer Flag" is "true" AND the "On hold reason" is "Awaiting integrated Customer (value of 6) THEN make the "On hold date" read-only.

2 ACCEPTED SOLUTIONS

Bert_c1
Kilo Patron

try changing the line in your second script from 

    if (ohd != '' || ic == true || hr == '6') to

    if ((ohd != '') && (ic == true) && (hr == '6'))

 

and why have the extra "{}" around the lines that get the values and set the read-only? that shouldn't be a problem though.

 

View solution in original post

Sandeep Rajput
Tera Patron
Tera Patron

@Damian Mudge 

Adding onto what Bert mentioned, here is how the script should look.

 

function onLoad() {

    if (g_user.hasRole('admin'))
        return;
    var ohd = g_form.getValue('u_on_hold_until_date');
    var ic = g_form.getValue('u_integrated_d2d');
    var hr = g_form.getValue('hold_reason');

    if (ohd != '' && ic == true && hr == '6')
      g_form.setReadOnly('u_on_hold_until_date', true);
}
 

View solution in original post

6 REPLIES 6

Bert_c1
Kilo Patron

try changing the line in your second script from 

    if (ohd != '' || ic == true || hr == '6') to

    if ((ohd != '') && (ic == true) && (hr == '6'))

 

and why have the extra "{}" around the lines that get the values and set the read-only? that shouldn't be a problem though.

 

Sandeep Rajput
Tera Patron
Tera Patron

@Damian Mudge 

Adding onto what Bert mentioned, here is how the script should look.

 

function onLoad() {

    if (g_user.hasRole('admin'))
        return;
    var ohd = g_form.getValue('u_on_hold_until_date');
    var ic = g_form.getValue('u_integrated_d2d');
    var hr = g_form.getValue('hold_reason');

    if (ohd != '' && ic == true && hr == '6')
      g_form.setReadOnly('u_on_hold_until_date', true);
}
 

Damian Mudge
Tera Expert

@Bert_c1 @Sandeep Rajput ,

Alas, this did not work. But I think there must be something else happening. I tried to breakdown the script in to small chunks to see where it was failing. Even with a single parameter it did not behave as expected. I created a T/F field on the Incident Table which is automatically populated to True when the "On hold until" date field is populated as a starting point to troubleshoot:

 

function onLoad() {

 

    if (g_user.hasRole('admin'))
        return;
 
    var ohdp = g_form.getValue('current.u_on_hold_until_populated');
    if (ohdp == true)
        g_form.setReadOnly('u_on_hold_until_date', true);
}

 

This also failed.

 

So, if I wanted to acheive the same in a Business Rule / or UI Policy, how would I amend the original script to achieve the the desired outcome. Can define the trigger with a filter query:

 

'Integrated Customer' IS true

AND

'On hold until populated' IS true

AND

'On hold reason' IS value '6'

THEN

'On hold date' IS 'readonly'

UNLESS User has 'Admin role'

 

IF the "On hold date populated" IS true  AND "integrated Customer Flag" is "true" AND the "On hold reason" is "Awaiting integrated Customer (value of 6) THEN make the "On hold date" read-only unless user has Admin role?

 

@Damian Mudge Are you testing this script from an admin account, 

 

As line 

   
 if (g_user.hasRole('admin'))
        return;

 

Will not let other parts of the script execute if you are logged in as an admin.

 

Also, you are trying to use current object in your script which isn't available in client script. Current object is only available in the server side script. 

 

Here is the script you should use.

 

function onLoad() {
 
    if (g_user.hasRole('admin'))
        return;
 
    var ohdp = g_form.getValue('u_on_hold_until_populated'); //Current is not available in client script
    if (ohdp == true)
        g_form.setReadOnly('u_on_hold_until_date', true);
}

Also, the conditions you have mentioned can be applied in business rule and UI Policy.