Built something you're proud of? Tell the story. A quick G2 review of App Engine or Build Agent helps other developers see what's possible on ServiceNow. Share your experience.

Need to populate table name based on Type field

suuriya
Tera Contributor

HI

 

I have a requirement,

 

In a flashes form (name of the table flashes), there is choice field called type. choices: user, location and account.

and there another field called table name its data type is table name so this field will display all tables present in the instances.

Now, if i select user from type the user table (sys_user) table need to be default in table name field

IF I select location from type field then location table (cmn_location) need to be default in table name field...similarly for account means account table.

suuriya_0-1695373580045.png

 

 

Is it possible to do so?

If it is achievable, If so, please do let me know how we can achieve this.

4 REPLIES 4

anshul_goyal
Mega Sage

Hi @suuriya,

Yes, it is possible to do. Please use the below code for your reference:

Client Script:
Type: onChange
Field: Type
Script: 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
g_form.clearValue("u_table_name");
return;
}

//Type appropriate comment here, and begin script below
if (newValue == "User") {
g_form.setValue("u_table_name", "sys_user");
} else if (newValue == "Location") {
g_form.setValue("u_table_name", "cmn_location");
} else if (newValue == "Account") {
g_form.setValue("u_table_name", "customer_account");
}

}

I hope this will help you solve your problem. Please mark it as Accepted and Helpful.

Thanks and Regards,
Anshul

Ankur Bawiskar
Tera Patron

@suuriya 

did you try adding ref qualifiers?

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

Deep Raj 1
Tera Contributor

Hello @suuriya ,

This is possible. You can configure "onChange" client script on your current table.
I have written a demo script for you.

DeepRaj1_0-1695377139596.png

u_reference will be your "Table type" and you can set "sys_id" of respective tables there.

Vishal Birajdar
Giga Sage

Hi @suuriya 

 

We can write onChange client script on "Type" field.

 

//Use your fields backend name

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

    if (isLoading) {
        return;
    }

    var getType = g_form.getValue('u_type');

    if (getType == 'user') {
        g_form.setValue('u_table', 'sys_user'); //put backend name of tables
    } else if (getType == 'location') {
        g_form.setValue('u_table', 'cmn_location'); //put backend name of tables
    } else if (getType == ''){
        g_form.clearValue('u_table');
    }
}

 

If possible make "Table name" field as readonly.

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates