Script to populate a field with a default value

Andrew_TND
Mega Sage
Mega Sage

I cant seem to get a business rule to work and wondering if anyone can validate my script?

So,

When a time_card is created I want the default value to be choice 1 which is Application support consultant.

The code I've got is... 

condition: current.isNewRecord()

(function executeRule(current, previous /*null when async*/) {

current.servicetype = "Application Support Consultant";

})(current, previous);

 

I've even tried it with the choice number but no joy.

Once I get it working I would like it to be dependent on the users group so for example something along the lines of...

var servicetype = current.user.group.getvalue()
var group = current.marketing
if(marketing){
return "Application support consultant";
}
if(software){
return "Software support consultant";
}
and so on and so on.

Please bear in mind I am quite new to coding so the above is likely to not be 100% correct.

Thanks

1 ACCEPTED SOLUTION

not really but it's more config.

your client script would be something like this:

var ga = new GlideAjax('ScriptIncludeName');
ga.addParam('sysparm_name', 'functionName');
ga.addParam('sysparm_user', g_form.getValue('created_by'));
ga.getXMLAnswer(callBackFunction);

function callBackFunction(response){
g_form.setValue('u_service_type', response);
}

then you need to write a script include, call it whatever you call the GA (ScriptIncludeName in the example above) and make sure the client callable tick box is ticked.

The config will be something like:

var ScriptIncludeName = Class.create();
ScriptIncludeName.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	functionName: function(){
		var usr = this.getParameter('sysparm_user');
		var gr = new GlideRecord('sys_user_grmember');
		gr.addQuery('user', usr);
		gr.query();
		
		if(gr.next()){
			var grp = gr.getValue('group');
			var gr2 = new GlideRecord('sys_choice');
			gr2.addEncodedQuery('element=u_service_type^u_group=' + grp); //u_group is your new field referencing sys_user_group
			gr2.query();
			if(gr2.next()){
				return gr2.getValue('value');
			}
		}
	},
	type: 'ScriptIncludeName'
});

View solution in original post

9 REPLIES 9

If you're happy for the value to be set when the user saves the form then you can do it all in a business rule. Something like the below would work, you'll need to add a field to your choice option form so you can relate a group to a choice option.

var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('user', current.created_by);
gr.query();

if(gr.next()){
var grp = gr.getValue('group');
var gr2 = new GlideRecord('sys_choice');
gr2.addEncodedQuery('element=u_service_type^u_group=' + grp) //u_group is your new field referencing sys_user_group
gr2.query();

if(gr2.next()){
current.u_service_type = gr2.getValue('value');
}

If you need it to be visible when the user opens the new form then you'll need to use a glide ajax which is a whole other thing!

I'd need it on load so it shows the value as they open it. Is that complicated to do?

not really but it's more config.

your client script would be something like this:

var ga = new GlideAjax('ScriptIncludeName');
ga.addParam('sysparm_name', 'functionName');
ga.addParam('sysparm_user', g_form.getValue('created_by'));
ga.getXMLAnswer(callBackFunction);

function callBackFunction(response){
g_form.setValue('u_service_type', response);
}

then you need to write a script include, call it whatever you call the GA (ScriptIncludeName in the example above) and make sure the client callable tick box is ticked.

The config will be something like:

var ScriptIncludeName = Class.create();
ScriptIncludeName.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	functionName: function(){
		var usr = this.getParameter('sysparm_user');
		var gr = new GlideRecord('sys_user_grmember');
		gr.addQuery('user', usr);
		gr.query();
		
		if(gr.next()){
			var grp = gr.getValue('group');
			var gr2 = new GlideRecord('sys_choice');
			gr2.addEncodedQuery('element=u_service_type^u_group=' + grp); //u_group is your new field referencing sys_user_group
			gr2.query();
			if(gr2.next()){
				return gr2.getValue('value');
			}
		}
	},
	type: 'ScriptIncludeName'
});

Thats perfect. Thanks for the help.

Prateek kumar
Mega Sage

Hello Andrew 

I wonder why do we even need script for this.

1.when to run section: 

Insert-checked(this will check if the record is new).

2. Actions

Set field values: Service type to 'Your Value'

Please let me know if this helps.


Please mark my response as correct and helpful if it helped solved your question.
-Thanks