Source value is mentioned options some field set mandatory and read only

harinya
Tera Contributor

HI 
can someone please help on the below requirement
i have issue form there is one  source field which many values if source value clearing or finding ,then parent issue field should mandatory and is group set as false and is group set read only 
but here ui policy will not work need use client script 
in the script i have tried like below

 

var Sourcecheck = Class.create();
Sourcecheck.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

getNames: function() {
var ids = this.getParameter('sysparm_id'); // source coming from client script


var allowedSourceIds = [sys_id1, sys_id2];// instead of hardcoding property call

var allowedList = allowedSourceIds.split(',');

var selectedIds = ids.split(',');

for (var i=0; i < selectedIds.length; i++) {

if (allowedList.includes (selectedIds[i].trim())) {

return true;

else{

return false;

}
client script is :

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}

var response = g_form.getValue('response');
var issueSource = g_form.getValue('source'); // sys_ids
//var profile = g_form.getValue('profile'); // sys_id of Entity

if (response === '2' && issueSource && profile) { // Assuming '2' = Accept
var ga = new GlideAjax('Sourcecheck');
ga.addParam('sysparm_name', 'getNames');
ga.addParam('sysparm_id', issueSource);

ga.getXMLAnswer(function(answer) {
if (answer === 'true') {
g_form.setMandatory('parent_issue', true);
g_form.setReadOnly('is_group', true);
} else {
g_form.setMandatory('parent_issue', false);
g_form.setReadOnly('is_group', false);
}
});
} else {
g_form.setMandatory('parent_issue', false);
g_form.setReadOnly('is_group', false);
}
}
this is not working as expected can please help on this have checked logs also some time it is is going if loop some times not for the same record can we use like this or else is there any other way please suggest

 

 

 

4 REPLIES 4

Arun_Manoj
Mega Sage

Hi @harinya ,

 

Corrected Script Include (Server-side):
Key Fixes:

allowedSourceIds.split(',') doesn’t work on an array — use it only on strings.

Ensure return values are always string ('true'/'false') to match on client-side.

Fix the if...else structure.

var Sourcecheck = Class.create();
Sourcecheck.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
getNames: function() {
var ids = this.getParameter('sysparm_id'); // comma-separated source sys_ids

// Replace this with property-based loading if needed
var allowedSourceIds = ['sys_id1', 'sys_id2'];

var selectedIds = ids.split(',');

for (var i = 0; i < selectedIds.length; i++) {
if (allowedSourceIds.includes(selectedIds[i].trim())) {
return 'true';
}
}
return 'false';
}
});

 

 


Corrected Client Script (Client-side onChange):
Key Fixes:

Ensure null/empty checks for issueSource are correct.

Avoid blocking AJAX with isLoading.

Log answer for debugging.

javascript
Copy code
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}

var issueSource = g_form.getValue('source'); // This should be a comma-separated sys_id string
var response = g_form.getValue('response');
var profile = g_form.getValue('profile');

if (response === '2' && issueSource && profile) {
var ga = new GlideAjax('Sourcecheck');
ga.addParam('sysparm_name', 'getNames');
ga.addParam('sysparm_id', issueSource);

ga.getXMLAnswer(function(answer) {
console.log('Server answer:', answer);
if (answer === 'true') {
g_form.setMandatory('parent_issue', true);
g_form.setReadOnly('is_group', true);
} else {
g_form.setMandatory('parent_issue', false);
g_form.setReadOnly('is_group', false);
}
});
} else {
g_form.setMandatory('parent_issue', false);
g_form.setReadOnly('is_group', false);
}
}
Additional Suggestions:
Use GlideProperties (Optional):
If allowedSourceIds should come from a system property, use:

javascript
Copy code
var allowedSourceIds = gs.getProperty('your.property.name', '').split(',');
Validation Tip:
Make sure your "source" field returns sys_ids and is not empty when onChange triggers. Add debugging logs to be sure.

Testing Tip:
Add console.log(issueSource); before calling GlideAjax to verify you're sending valid input.

Would you like me to help you convert the allowedSourceIds to be loaded from a system property for better flexibility.

 

If the solution is fine, please give helpful.

 

 

 

 

 

neetusingh
Giga Guru

@harinya - Can you try the below code -

 

Script Include -

var Sourcecheck = Class.create();
Sourcecheck.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

getNames: function() {
var ids = this.getParameter('sysparm_id'); // comma-separated sys_ids string

// Fetch allowed source IDs dynamically, e.g., from system properties or a table (example below uses property)
var allowedSourceIdsString = gs.getProperty('your.custom.allowed_source_ids', ''); // e.g. "sys_id1,sys_id2"
var allowedList = allowedSourceIdsString.split(',').map(function(id) { return id.trim(); });

if (!ids) {
return 'false';
}

var selectedIds = ids.split(',').map(function(id) { return id.trim(); });

// Check if any selected ID is in allowed list
for (var i = 0; i < selectedIds.length; i++) {
if (allowedList.indexOf(selectedIds[i]) !== -1) {
return 'true';
}
}
return 'false';
}
});

 

Notes:

  • Use a system property (your.custom.allowed_source_ids) to avoid hardcoding.

  • Return 'true' or 'false' as strings.

  • Check if any selected source matches allowed list.

 

Step 2: Client Script (onChange Client Script on Source field)

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}

// If source is cleared, make parent_issue mandatory and is_group read-only
if (!newValue) {
g_form.setMandatory('parent_issue', true);
g_form.setReadOnly('is_group', true);
return;
}

// Call GlideAjax to check if source matches allowed list
var ga = new GlideAjax('Sourcecheck');
ga.addParam('sysparm_name', 'getNames');
ga.addParam('sysparm_id', newValue);

ga.getXMLAnswer(function(answer) {
if (answer === 'true') {
g_form.setMandatory('parent_issue', true);
g_form.setReadOnly('is_group', true);
} else {
g_form.setMandatory('parent_issue', false);
g_form.setReadOnly('is_group', false);
}
});
}

 

Notes:

  • When source is cleared (newValue is empty), set mandatory/read-only as required.

  • When source has value, call GlideAjax to check.

  • Removed unused variables (response, profile) for clarity.

  • Make sure this client script is attached to the onChange event of the Source field.

Thanks for Response @neetusingh ,it seems working fine in native view but not working in workspace i didn't understand why it is happening please help

Ankur Bawiskar
Tera Patron
Tera Patron

@harinya 

try this

Client Script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }

    var issueSource = g_form.getValue('source');

    var ga = new GlideAjax('Sourcecheck');
    ga.addParam('sysparm_name', 'getNames');
    ga.addParam('sysparm_id', issueSource);

    ga.getXMLAnswer(function(answer) {
        if (answer === 'true') {
            g_form.setMandatory('parent_issue', true);
            g_form.setReadOnly('is_group', true);
        } else {
            g_form.setMandatory('parent_issue', false);
            g_form.setReadOnly('is_group', false);
        }
    });
}

Script Include:

var Sourcecheck = Class.create();
Sourcecheck.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

    getNames: function() {
        var ids = this.getParameter('sysparm_id'); // source coming from client script
        var allowedSourceIds = ['sys_id1', 'sys_id2']; // Replace with property call if needed

        var selectedIds = ids.split(',');

        for (var i = 0; i < selectedIds.length; i++) {
            if (allowedSourceIds.includes(selectedIds[i].trim())) {
                return 'true';
            }
        }
        return 'false';
    }
});

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