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

Akash4
Kilo Sage
Kilo Sage

Hi Andrew,

Can you share the screenshot for time_card field and choices in it. Also, what 'type' is the servicetype field here?

You can also do this via onLoad client script: 

function onLoad() {

if g_form.isNewRecord(){

g_form.setDispalyValue('servicetype','Application support consultant');

}

return;

}

You may also do this at dictionary level: Right click on servicetype field > Configure dictionary > Give the back end name of 'Application Support Consultant' in the default value. This is even easier to do. 

find_real_file.png

Thanks, Akash

Regards, Akash
If my response proves useful, please mark it "Accept as Solution" and "Helpful". This action benefits both the community and me.

lease find screen shot below.

I don't want the default value to change as it seems to overwrite the option to select other choices.

If you want the value of the field to be set when you initially create a new record you will need to do this with an onLoad client script. If you use a business rule you will only be able to run it before insert, so only when you save the form will the servicetype value be set. Is that what you're going for or do you want it to be set and visible whilst your users are populating the form?

Hi again David

The whole scope is so when a user creates a new time card a rule dictates what the service type is when they book time depending on their assignment group.

For example,

if network - Choice 1 = Application support consultant

if marketing - Choice 3 = Marketing productivity time

if service management - Choice 15 = SDM quota


The field for service type is u_service_type and the table is time_card. Looks like as well the choice table is.... Hope that helps.

find_real_file.png