Need help on client script both on load and on change

Bird1
Mega Sage

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.

find_real_file.png

 

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');    
    }}

6 REPLIES 6

Yousaf
Giga Sage

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.***

Ian Mildon
Tera Guru

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');    
	}
}

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.

 

 

 

 

SumanthDosapati
Mega Sage
Mega Sage

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