Need help on client script both on load and on change
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-15-2022 08:56 AM
I got a requirement to create client script both onload and on change.
If language is 'Chinese' && Knowledge base is 'Knowledge' the Ownership group should auto populate.
I tried to write the onload script (need a new record due to we use dynamic translation from Eng to Chinese) but not work.(not auto populate the ownership group field)
Script (on load)
function onLoad() {
//Type appropriate comment here, and begin script below
if ((g_form.isNewRecord()) && (g_form.getValue('language') == 'zh') && (g_form.getValue ('kb_knowledge_base') == 'dfc19531bf2021003f07e2c1ac0739ab')){
g_form.setValue('ownership_group','6208f0391b28191037e598aebd4bcbb7');
}}
And this is the on change script that also doesn't work.(not auto populate the ownership group field)
Script (on change)
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
if(oldValue != newValue){
if (newValue == 'zh' && (g_form.getValue ('kb_knowledge_base')== 'dfc19531bf2021003f07e2c1ac0739ab')){
g_form.setValue('ownership_group','6208f0391b28191037e598aebd4bcbb7');
}}
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-15-2022 09:13 AM
Hi Bird,
Try to add g_form.save() at the end.
Mark Correct or Helpful if it helps.
Thanks,
Yousaf
***Mark Correct or Helpful if it helps.***
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-15-2022 09:48 AM
You have too many () in your scripts. The onLoad should be like this:
function onLoad() {
//Type appropriate comment here, and begin script below
if (g_form.isNewRecord() && g_form.getValue('language') == 'zh' && g_form.getValue('kb_knowledge_base') == 'dfc19531bf2021003f07e2c1ac0739ab'){
g_form.setValue('ownership_group','6208f0391b28191037e598aebd4bcbb7');
}
}
Then modify your onChange so the IF matches the formatting from the onLoad; by getting rid of the extra () surrounding the g_form.getValue
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
if(oldValue != newValue){
if (newValue == 'zh' && g_form.getValue('kb_knowledge_base') == 'dfc19531bf2021003f07e2c1ac0739ab'){
g_form.setValue('ownership_group','6208f0391b28191037e598aebd4bcbb7');
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-16-2022 02:01 AM
Thank you. What I found is. If I use the on load client script like this., it will be workable (auto populate the field)
function onLoad() {
//Type appropriate comment here, and begin script below
if (g_form.getValue('language') == 'zh' && g_form.getValue('kb_knowledge_base') == 'dfc19531bf2021003f07e2c1ac0739ab'){
g_form.setValue('ownership_group','6208f0391b28191037e598aebd4bcbb7');
}}
But If I add g_form.isNewRecord() in the condition, it's not workable. (not auto populate the field)
And also the 'on change' client script, it doesn't work. I found that when create a new KB article, the 'Knowledge base' field is empty, so we need to manually key in 'Knowledge' first and then the 'Language' field will be editable and allow us to choose the language.
The client script could be based on only 1 field and I selected 'Language' field.
I am not sure that what should be the best solution to achieve it? I need if 'Knowledge base' = Knowledge && 'language' = 'zh' (Chinese) , the ownership field will auto populate.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-15-2022 09:55 AM
Hi,
You can write a single onChange client script as below and that will work for both onLoad and onChange.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (newValue === '') {
if (g_form.getValue('language') == 'zh' && g_form.getValue('kb_knowledge_base') == 'dfc19531bf2021003f07e2c1ac0739ab')
{
g_form.setValue('ownership_group','6208f0391b28191037e598aebd4bcbb7');
}
}
if (g_form.getValue('language') == 'zh' && g_form.getValue('kb_knowledge_base') == 'dfc19531bf2021003f07e2c1ac0739ab')
{
g_form.setValue('ownership_group','6208f0391b28191037e598aebd4bcbb7');
}
}
Mark as correct and helpful if it solved your query.
Regards,
Sumanth