The CreatorCon Call for Content is officially open! Get started here.

set value on requested item using catalog client script

srikanthvk
Giga Expert

Hi,

I have been trying to set a value on my requested item form through an OnSubmit catalog client script, but to my surprise i'm unable to set it.

I'm wondering what went wrong, i have also included a timer for 30 secs on the workflow which didn't give any results.

Below is the code snippet

function onSubmit() {

    //Type appropriate comment here, and begin script below

  if(g_form.getValue('var_req') != ''){

  g_form.setValue('u_input','4 hours');

  }

   

}

1 ACCEPTED SOLUTION

As stated earlier, you will not have direct access to the created item from client script. You would need to create a hidden variable, set it. Then it would get pushed into the field on the target record through naming convention.



OR, you can use the script within the catalog item definition. I posted a image of the script field. You would have access to producer variables and the current target record.


View solution in original post

12 REPLIES 12

Mihir Mohanta
Kilo Sage

Please do this in workflow run script activity.Script will be like



if(current.variables.var_req != ''){


  current.u_input = 4 hours ;


current.update();


  }


It can be done without current.update();




I assume that u_input is also variable, so script will be rather like this:



if(current.variables.var_req != ''){


  current.variables.u_input = '4 hours' ;


}


grzegorzch
Kilo Expert

What type of variables are var_req and u_input ?


raprohaska
Kilo Guru

First a question, when you say nothing is happening do you mean that on the created record, the field u_input isn't set? If this is the case, is there a variable within the catalog item named u_input?



Client Script will not have direct access to the fields on the resulting record. When you use g_form.setValue it is setting a field on the client page. From there, if the variable has a name equal to the name of the field on the target table... it will automatically set it.




IF this is a static value you are setting, you can simply set the value (as suggested by 553931) in the script on the producer: current.u_input.


find_real_file.png



You can also tie a template to producers (at least in new versions of ServiceNow), then you wouldn't have to hard code it and set the value via the template.



Finally, you may want to think about using a business rule. If this is a value you want to set based on a specific request and if it is possible that the same kind of request could come in from a place other than the service catalog, a business rule would always set the value as needed based on the set of conditions instead of duplicating that code any place that request could come in from. Other sources would include integrations or someone creating a request directly through the target table instead of via the catalog.