How to convert my pseudo code into a working onLoad Client Script?

SusanSchwar
Tera Contributor

I have some pseudo code that I would like to use in a client script, but I'm not sure exactly how to convert it:

function onLoad() {
   //if current.task is task1 {
    //if name_of_field is true{
        //name_of_field_2.readonly is false
    //}
    //if name_of_field_3.readonly is valid {
        //name_of_field_4 readonly is false
    //}
   //}
   
}
2 REPLIES 2

Mohammed8
Giga Sage

Hi @SusanSchwar 

What's the requirement here?

 

Here is code, make changes accordingly:

 

function onLoad() {

// Example condition: only when task = task1
if (g_form.getValue('task') !== 'task1') {
return;
}

// If name_of_field is true (checkbox / boolean)
if (g_form.getValue('name_of_field') === 'true') {
g_form.setReadOnly('name_of_field_2', false);
}

// If name_of_field_3 is currently read-only
if (g_form.isReadOnly('name_of_field_3')) {
g_form.setReadOnly('name_of_field_4', false);
}
}

 

Here is link to understand the above used functions better:

https://www.servicenow.com/docs/r/api-reference/c_GlideFormAPI.html

 

Regards,

Mohammed Zakir

DGAJ
Giga Guru

Hi @SusanSchwar ,

Whats the use case here ? 

 

function onLoad() {
    // Get the current task number 
    var taskNumber = g_form.getValue('number'); 
    
    if (taskNumber == 'TSK0010001') { // Replace with your specific task number 
        
        // Check if name_of_field is true
        if (g_form.getValue('name_of_field') == 'true' || g_form.getValue('name_of_field') == true) {
            g_form.setReadOnly('name_of_field_2', false);
        }
        
        // Check if name_of_field_3 is readonly and valid
        if (g_form.getValue('name_of_field_3') != '') { // Checking if field has a value (is valid)
            g_form.setReadOnly('name_of_field_4', false);
        }
    }
}

 *******************************************************************************************
If my answer has helped with your question, please mark my answer as the accepted solution and give a thumbs up.

Best regards,

DGAJ