Need to populate one variable value based on another

Rekha20
Tera Contributor

Hi All,

 

I have form:

Rekha20_0-1708191021503.png

 

where I need to populate "Org Number" and "Sub Account" based on SIU Fund variable.

 

"Org Number" and "Sub Account" referring to same table but having simple referring qualifier: 

 

"Org Number" where Type = Organization

"Sub Account" where Type = Account

 

So requirement is:

  • SIU Fund variable value is "A", Org Number" should populate as 30553, sub account should populate as 5897
  • SIU Fund variable value is "B", Org Number should populate as 30564, sub account should populate as 5897
  • SIU Fund variable value is "C", Org Number should populate as 30122, sub account should populate as 5897
  • SIU Fund variable value is "D",
    • There should only be two value available to select for Org Number variable as below
    •  03790, sub account 5897
    • 03828, sub account 5897

Challenge I am facing is how to make it dynamic for 

  • SIU Fund variable value is "D",?

Any help would be appreciated.

 

Thanks,

8 REPLIES 8

Danish Bhairag2
Tera Sage
Tera Sage

Hi @Rekha20 ,

 

You can try the autopopulate feature of catalog item where based upon selection of one field value other fields can be populated without any scripting.

 

Take a look at below link

 

https://www.servicenow.com/community/developer-articles/auto-populate-a-variable-based-on-a-referenc...

 

Thanks,

Danish

swathisarang98
Giga Sage
Giga Sage

Hi @Rekha20 ,

 

Please find the comments for below queries,

  • SIU Fund variable value is "A", Org Number" should populate as 30553, sub account should populate as 5897
  • SIU Fund variable value is "B", Org Number should populate as 30564, sub account should populate as 5897
  • SIU Fund variable value is "C", Org Number should populate as 30122, sub account should populate as 5897

For the above three you can create a onChange client script and set the value through g_form.setValue and for the below one 

 

  • SIU Fund variable value is "D",
    • There should only be two value available to select for Org Number variable as below
    •  03790, sub account 5897
    • 03828, sub account 5897

For this you have to use reference qualifier in both of your fields("Org Number" and "Sub Account") and write a script include pass the SIU variable value to it and inside script include check for the condition whether the option selected is "D" and return the value . You can write something as below in the reference qualifier ,

swathisarang98_0-1708200219324.png

 

Please mark this comment as Correct Answer/Helpful if it helped you.

Regards,

Swathi Sarang

Hi @swathisarang98 

 

Thank you for your response.

 

 

Please find the comments for below queries,

  • SIU Fund variable value is "A", Org Number" should populate as 30553, sub account should populate as 5897
  • SIU Fund variable value is "B", Org Number should populate as 30564, sub account should populate as 5897
  • SIU Fund variable value is "C", Org Number should populate as 30122, sub account should populate as 5897

For the above three you can create a onChange client script and set the value through g_form.setValue and for the below one  ->  Yes, I did already same for this.

 

 

Regarding 

  • SIU Fund variable value is "D",
    • There should only be two value available to select for Org Number variable as below
    •  03790, sub account 5897
    • 03828, sub account 5897

For this you have to use reference qualifier in both of your fields("Org Number" and "Sub Account") and write a script include pass the SIU variable value to it and inside script include check for the condition whether the option selected is "D" and return the value. You can write something as below in the reference qualifier,

 

My concern is- If I use reference qualifier for both variables, then client script will work?

 

Also, can you please help me with the script include code? Would appreciate it.

 

Thanks,

 

Rekha20
Tera Contributor

HI @swathisarang98 

I have written the script include: 

var setSubAccount = Class.create();
setSubAccount.prototype = {
    initialize: function() {},
    getOrgAndSubAccountValues: function(siuValue) {
        var orgAndSubAccountValues = {};

        // Check if the SIU variable value is "D"
        if (siuValue === "D") {
            // Return the predefined values for Org Number and Sub Account fields
            orgAndSubAccountValues.organization = ["c374861c1b4ba5108e9987f5624bcbb9", "0b74861c1b4ba5108e9987f5624bcbc1"];
            orgAndSubAccountValues.sub_account = ["9ba60e9c1b4ba5108e9987f5624bcb22"];
        } else {
            // Return default values or handle other cases as needed
            orgAndSubAccountValues.organization = [];
            orgAndSubAccountValues.sub_account = [];
        }

        return orgAndSubAccountValues;
    },

    type: 'setSubAccount'
};
 
 
Advanced reference qualifier for Sub Account variable: 
 
javascript: new setSubAccount.getOrgAndSubAccountValues(current.siu_fund)
 
But no luck. any thoughts please?