Based on the selection of field another field is populated on submit/insert

wiganmichae
Tera Contributor

Hi,

I have the need to have a field populate based on the selection of another field.  I have this sorted for any updates i itil view with a onchange client script however when end user raise a form using a record producer this doesn't work.

Is there a way I can do it with an onsubmit script of business rule?

This is my on change script and the field changing is u_mw_subtype

function onChange(control, oldValue, newValue, isLoading) {
  if (isLoading || newValue == '') {
    return;
  }
  if (newValue == 'hacking'||newValue == 'identity_theft'||newValue == 'phishing'||newValue == 'Remote_Access') {
    g_form.setValue('mw_deal_type', 'attempts to gain your personal information'); 
 } else if (newValue == 'Buying_or_Selling') {
    g_form.setValue('mw_deal_type', 'buying or selling'); 
 } else if (newValue == 'Dating & Romance') {
    g_form.setValue('mw_deal_type', 'Dating and Romance'); 
 } else if (newValue == 'Fake Charities') {
    g_form.setValue('mw_deal_type', 'Fake Charities'); 
 } else if (newValue == 'Betting & Sporting Investments'||newValue == 'Investments') {
    g_form.setValue('mw_deal_type', 'investment'); 
 } else if (newValue == 'Jobs & Employment'||newValue == 'Pyramid Schemes') {
    g_form.setValue('mw_deal_type', 'jobs & employment'); 
 } else if (newValue == 'Other') {
    g_form.setValue('mw_deal_type', 'other'); 
 } else if (newValue == 'Ransomware & Malware'||newValue == 'Threat to life, Arrest or Other') {
    g_form.setValue('mw_deal_type', 'threats & extortion'); 
 } else if (newValue == 'Inheritance and unexpected Money'||newValue == 'Rebate deals') {
    g_form.setValue('mw_deal_type', 'unexpected money'); 
 } else if (newValue == 'Travel, Prizes & Lottery') {
    g_form.setValue('mw_deal_type', 'unexpected winnings'); 
  } else {
    g_form.setValue('mw_deal_type', ''); // Clears the 'deal Type' field
  }
}



4 REPLIES 4

ChiranjeeviR
Kilo Sage

Hi @wiganmichae ,

 

 

You're correct that onChange client scripts don't run on Record Producers when creating a record — Record Producers use a Catalog Item form, and the underlying GlideForm scripting behaves slightly differently. In these cases, g_form.setValue(...) works only with Catalog Client Scripts or Catalog UI Policies.

Recommended Solution: Use a Catalog Client Script (not a Business Rule)

To replicate your current logic when using a Record Producer, convert your logic into a Catalog Client Script scoped to the Record Producer, not the target table.

📌 Steps to do this:

  1. Go to Service Catalog > Catalog Definitions > Record Producers.

  2. Open your Record Producer.

  3. Scroll down to the 'Catalog Client Scripts' related list.

  4. Click New.

Configure the Client Script:

  • Type: onChange

  • Variable name: the question/field in the Record Producer that maps to u_mw_subtype

  • Script:

 

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

  var dealType = '';

  switch (newValue) {
    case 'hacking':
    case 'identity_theft':
    case 'phishing':
    case 'Remote_Access':
      dealType = 'attempts to gain your personal information';
      break;
    case 'Buying_or_Selling':
      dealType = 'buying or selling';
      break;
    case 'Dating & Romance':
      dealType = 'Dating and Romance';
      break;
    case 'Fake Charities':
      dealType = 'Fake Charities';
      break;
    case 'Betting & Sporting Investments':
    case 'Investments':
      dealType = 'investment';
      break;
    case 'Jobs & Employment':
    case 'Pyramid Schemes':
      dealType = 'jobs & employment';
      break;
    case 'Other':
      dealType = 'other';
      break;
    case 'Ransomware & Malware':
    case 'Threat to life, Arrest or Other':
      dealType = 'threats & extortion';
      break;
    case 'Inheritance and unexpected Money':
    case 'Rebate deals':
      dealType = 'unexpected money';
      break;
    case 'Travel, Prizes & Lottery':
      dealType = 'unexpected winnings';
      break;
    default:
      dealType = '';
  }

  g_form.setValue('mw_deal_type', dealType);
}

 

🔍 Make Sure:

  • The variable name matches your Record Producer variable (not the target table field name).

  • The target table field (mw_deal_type) should be mapped from a variable in the Record Producer.

Thanks and Regards,

Chiranjeevi R

 

Please mark as Correct Answer/Helpful, if applicable.

 
Thanks & Regards,
Chiranjeevi
ServiceNow Developer | | ITSM | | ServiceNow Discovery | | Event Management | | Service Mapping | | CMDB

Please mark as Correct Answer/Helpful, if applicable.

Ankur Bawiskar
Tera Patron
Tera Patron

@wiganmichae 

so it's a record producer and you want it to happen when user opens that form in portal

what type of script have you written?

It should be catalog client script which Applies on Catalog Item View

share client script config screenshots.

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

Sanjay Bagri1
Tera Guru

Hello @wiganmichae ,

 

Please create a script include and call that script include in client script based on your requirement like if you want it in onChange ,onSubmit than it will work and fulfill your requirement. 

 

Please mark my answer correct and helpful if it helps.

Thanks

Sanjay Bagri

 

 

wiganmichae
Tera Contributor

I could do a client script on change on the record producer yes but the problem is the field in question is not on the record producer.  We don't expect end users to see it or know anything about it but I could put it on there and hide the field.

 

But what I was more inclined was to have it done after the user raises it so that's why I was thinking and onsubmit script on ITIL side or a business rule on insert.  Something like that.