The CreatorCon Call for Content is officially open! Get started here.

Incident Form - Populating field values based on Sub category field

MThomas1
Tera Contributor

Hello,

 

We are exploring options to populate field values based on subcategory field on Incident form before submitting the ticket.

Templates will not work in our case. 

Thank you.

3 REPLIES 3

aruncr0122
Mega Guru

Hi @MThomas1 ,

 

One way to do this without using templates is to drive it from a client script. You can use an onChange client script on the Subcategory field and then set values on the other fields you want.

For example:

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

// Example logic: set Assignment Group / Category / etc. based on subcategory
if (newValue == 'network') {
g_form.setValue('assignment_group', '287ebd7da9fe198100f92cc8d1d2154e'); // Networking group sys_id
g_form.setValue('category', 'infrastructure');
}
else if (newValue == 'hardware') {
g_form.setValue('assignment_group', '6816f79cc0a8016401c5a33be04be441'); // Hardware group sys_id
g_form.setValue('category', 'hardware');
}
}

That way you can pre-populate or adjust fields in real time as soon as the subcategory changes.

MThomas1
Tera Contributor

Hello  aruncr0122,

 

Thank you for your response.

we tried client script as below but it is not working. Any idea what we are missing ? 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue == '') {
      return;
   }
   if (newValue == 'Testing') {
              g_form.setValue('assignment_group', '87986d24db15a7c0de5b6572ca961939');
              g_form.setvalue('description','This is a test');
   }
   else if (newValue == 'Virus incident') {
g_form.setValue('assignment_group', '75972d24db15a7c0de5b6572ca9619f1');
g_form.setValue('description', 'Hello World');
}  
}

Hi @MThomas1 ,

 

I see the issue in your script. There are a couple of small mistakes that are causing it not to work:

1.Case sensitivity in g_form.setValue:

JavaScript is case-sensitive. In your first if block you wrote:

g_form.setvalue('description','This is a test');


It should be setValue with a capital V:

g_form.setValue('description','This is a test');


2.Other things to check:

Make sure your onChange client script is actually attached to the correct field (the one you’re monitoring).

Ensure the field values you are checking ('Testing' and 'Virus incident') exactly match the choice values (case-sensitive and no extra spaces).

Here’s the corrected script:

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') {
console.log('Form is loading or value is empty, exiting script.');
return;
}

console.log('New value detected: ' + newValue); // Logs new value

if (newValue == 'Testing') {
g_form.setValue('assignment_group', '87986d24db15a7c0de5b6572ca961939');
g_form.setValue('description','This is a test');
g_form.addInfoMessage('Assignment group and description set for Testing.');
console.log('Set values for Testing.');
}
else if (newValue == 'Virus incident') {
g_form.setValue('assignment_group', '75972d24db15a7c0de5b6572ca9619f1');
g_form.setValue('description', 'Hello World');
g_form.addInfoMessage('Assignment group and description set for Virus incident.');
console.log('Set values for Virus incident.');
}
else {
g_form.addInfoMessage('No matching value found.');
console.log('No matching value for: ' + newValue);
}
}

 

I have added some logs in the script.. which will help you debug the issue.

 

Thanks