Onchange Client script is not working in workspace

XYD23
Tera Contributor

Below on change client script is working fine on the form level, but its not working on the workspace level.
Can anyone help me on this please?

Client script:

XYD23_0-1705916075388.pngXYD23_1-1705916096160.png

 

1 ACCEPTED SOLUTION

Hi @XYD23 

 

Follow below steps :

 

Step 1 : Create client callable script include

Name : TechTaskUtils

 

 

var TechTaskUtils = Class.create();
TechTaskUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

   /* Function */
	getTechTaskDetails : function(){
	/*1. Declare & initialize necessary variables/JSON  */	
	var result = {
		'title' :'',
		'assigned_to' : '',
		'due_date' :''
	};
    
	/*2.Get newValue (sys_id) from client script */
	var getSysId = this.getParameter('sysparm_sysID');
    
	/*3. Glide record on your table */
	var grTech = new GlideRecord('u_tech_task');
	grTech.addQuery('sys_id',getSysId);
	grTech.query();
    
	if(grTech.next()){

		/*3.1 Set JSON object */
		result.title = grTech.getValue('u_title');
		result.assigned_to = grTech.getValue('u_assigned_to');
		result.due_date = grTech.getValue('u_due_date');
          
	}
   
   /*4.Stringify result & return it */
   return JSON.stringify(result);

	},

    type: 'TechTaskUtils'
});

 

 

VishalBirajdar_0-1705926833521.png

 

Step 2 : Write onChange Client script 

 

 

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

   /*1. Call Script include using GlideAjax */
   var ga = new GlideAjax('TechTaskUtils');  //script include name
   ga.addParam('sysparm_name','getTechTaskDetails');  //function name within script include
   ga.addParam('sysparm_sysID',newValue);   //pass the value to script incldue

   ga.getXMLAnswer(callBackFunction);

   function callBackFunction(answer){

	/* here we will get the result from script include & Parse the answer */
	var result = JSON.parse(answer);

	/* Set value on form */

	g_form.setValue('u_title',result.title);
	g_form.setValue('u_assigned_to',result.assigned_to);
	g_form.setValue('u_due_date',result.due_date);

   }
 
}

 

 

Hope this helps...!!!

 

 

Vishal Birajdar
ServiceNow Developer

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

View solution in original post

16 REPLIES 16

I doubt mate. @Anurag Tripathi need guidance.

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

Amit Verma
Kilo Patron
Kilo Patron

Hi @XYD23 

 

Can you please take the value of search field in a variable like var searchValue = g_form.getField('search') and then pass it to the the query as techList.addQuery('sys_id',searchValue);

 

Thanks & Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.

XYD23
Tera Contributor

getField is not working.

tried with getValue its not working.

Hi @XYD23 

 

Try after unchecking the Global Checkbox and setting the view to workspace.

 

Thanks & Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.

XYD23
Tera Contributor

tried that as well not working