which is best practice to design client script for below scenario

Supriya25
Tera Guru

Hi All,

 

Kindly help me 
we have multiple values under "Due Type " 

Supriya25_0-1753409103394.png

For each drop-down value has separate set of new fields to populate on Incident form
example
1. For Approval Review option
Approver Manager, Due Date, Approval Department, Approval Group, Approver, Approval comments
2. For Closed Review option
Remarks, Closed notes, Due date, Closed by, 

Like this  for each 'Due Type" option different set of new fields has to be "SetValue , Display, Read-Only, Mandatory "  on form based on different conditions.

Kindly let me know one thing , Can I design Single ONCHNAGE client script on "Due Type" field for all "Due Type" Options ?
 Or design separate client scripts  on "Due Type" field for each "Due Type" Options ?

which is best practice to maintain code in good way.


 

4 REPLIES 4

Chaitanya ILCR
Kilo Patron

Hi @Supriya25 ,

One OnChange Client script is enough 

something like this should help you

replace allfields with your field names

fields with your field names and duedatechoices with your choice backend values

 

this should work just replace it your actual values

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    var allFieds = ['add all the fields names comma separated to  dependent on the duedate in this array']
    for (var f = 0; f < allFieds.lenght; f++) {
        g_form.setMandatory(allFieds[f], false);
        g_form.setDisplay(allFieds[f], false);
    }

    if (isLoading || newValue === '') {
        return;
    }


    var fieldObj = {
        'dueDatechoice1': ['field1', 'field2', '..'], //choice backend name as keys and fields as array dependent field backend names
        'dueDatechoice2': ['field3', 'field4', '..'],
        'dueDatechoice3': ['field4', 'field5', '..']
    }

    var fields = fieldObj[newValue];
    for (var i = 0; i < fields.lenght; i++) {
        g_form.setDisplay(fields[i], true);
        //extend your logic if required
    }



}

hope this make sense

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

 

Hi @Chaitanya ILCR 
Thanks for your valuable response. the above code work for Display the fields.
My Apologies , I missed to mention "SetValue , Display, Read-Only, Mandatory, "  

How about if we want to SetValue, Display, Read-Only, Mandatory, Kindly advise in this case.
Example :
For Due Type = 5 Days||7 Days
 set Due-date field value by calculating Schedules
Auto Set Approver value (based on certain conditions Approver roles and approver groups validations)

For Due Type = 30 Days||27 Days
Some conditions like Combination of DueType, Category, Channel- Set Due Date

For Due Type = 20 Days
exclude weekends and Display due date and display other fields.

I'm not looking for code for all these. I have code.  But I want to know that to follow  Best practice and healthily code how many scripts I can design for them . Single or more than a single ?

If I design separate script for each DueType , will it be a problem?
If I design Single script for all DueTypes, how it would be problem  ? in future if I want to make Changes 

Ankur Bawiskar
Tera Patron
Tera Patron

@Supriya25 

you can use onChange + GlideAjax to get the days to set

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Deepak Shaerma
Kilo Sage

hi @Supriya25 

the best practice follows by using the ui policy and it will achieved smoothly, but if you want to do it with the client script only, then create a onchange client script on due date and use the choices like :
if(newValue == 'value'){
g_form.setVisible('field_name','true');
}


Please mark my answer as helpful/correct if it resolves your query.

Regards,
Deepak Sharma