How to generate a dynamic url in a field from values selected in List Collector.

Sri63
Mega Expert

I have a requirement where I need to generate a URL in a field based on sys_ids(values) selected in List Collector

Scenario:

1. When ever I select a value in List Collector from LEFT to RIGHT
2. URL must generate automatically based on the selection of right value
3. All the values in this list collector are from a custom table ('Cust Table')
4. So when ever I select a value from in list collector there is a field allocated for URL. It has to populate dynamically in that URL
5. If I select 1 value URL must contain that sys_id value. If I select 3 values in list collector then I want to have those 3 sys_id's in that URL .
** Based on the number of values i select this URL must take the selected value's sys_id's and add to the url automatically (dynamic)

Example:
Scenario 1

 

find_real_file.png

Scenario 2
find_real_file.png

 

1 ACCEPTED SOLUTION

dvp
Mega Sage
Mega Sage

Here is the script that works for both back-end view as well as service portal

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

   //Type appropriate comment here, and begin script below
	
	var arr = newValue.split(',');
	var str = '';
	if(arr.length == 1)
		str = newValue;
	else{
		str = arr[0];
		for(var i =1; i<arr.length; i++){
			str += 'ORparent%3D'+arr[i];
		}
	}
	
	var url = 'https://'+ top.location.host + '/cust_table.do?sysparm_query=parent%3D' + str;
	
	g_form.setValue('YOUR_variable', url);
}

View solution in original post

3 REPLIES 3

Alikutty A
Tera Sage

Hello,

You can write an onChange script on your List collector variable and use the following script in it.

 

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}else if(oldValue != newValue){
		
		var url = getInstanceURL();
		var ids = newValue.split(",");
		for(var i =0;i<ids.length;i++){
			var query = "parent="+ids[i].toString();
			url += query + "OR";
		}
		
		url = url.substring(0, url.length-2);
		alert(url);
		g_form.setValue('url_variable_name', url); //Replace URL variable name	
	}
	
	function getInstanceURL(){
		var tableName = "cust_table.do?"; //Replace your table name here
		var query = "sysparm_query=";
		var location = window.location.href;
		var url = location.indexOf(".com/");
		if(url > -1){
			url = location.substring(0,url+5);
			url = url+tableName+query;
			return url;
		}
	}
}

}

dvp
Mega Sage
Mega Sage

Here is the script that works for both back-end view as well as service portal

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

   //Type appropriate comment here, and begin script below
	
	var arr = newValue.split(',');
	var str = '';
	if(arr.length == 1)
		str = newValue;
	else{
		str = arr[0];
		for(var i =1; i<arr.length; i++){
			str += 'ORparent%3D'+arr[i];
		}
	}
	
	var url = 'https://'+ top.location.host + '/cust_table.do?sysparm_query=parent%3D' + str;
	
	g_form.setValue('YOUR_variable', url);
}

It worked . 🙂 

 

Thanks DVP