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

How to set custom variable value from widget

avid
Kilo Expert

Hello.

In order to simplify my question I've created a new ui macro variable that contain only a single input field:

<?xml version="1.0" encoding="utf-8" ?>

<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">

<input type="text" id="test" name="test"></input>

</j:jelly>

In order to view the variable the catalog form I've connected a new widget to the macro variable that contain the same field:

Body HTML template:

<div>

<input type="text" id="test" name="test" />

</div>

And indeed the new variable is shown on the catalog form as input field in the new service portal, but when i submit the catalog with some string inside the input field the variable value stay empty:

find_real_file.png

I tried several methods in order to set the value in the variable but unfortunately i didn't succeed.   my only concern is how to set the value in the widget, not in the macro.

Thank you/

1 ACCEPTED SOLUTION

larstange
Mega Sage

Hi



Values entered in widget variables are NOT saved when you submit the item.


You need to add additional regular variables (eg. a single line text) and then on an onSubmit script (only for ui macros on the old service catalog) copy the value from your widget to the regular variable. You can hide the regular variable using a UI policy.



Making your own widget for SP, you will not be able to access the content with an onSubmit script, as you can't accces the DOM. Instead you build an onChange logic into your widget to copy the value from the input field to your regular variable everytime a change happens.



You can access variables on the catalog item from the widget via:



$scope.page.g_form.setValue('var_name');



Also see how to monitor for changes in catalog variables here: Embedding widgets in Service Catalog - ServicePortal.io - Service Portal, CMS, and Custom Apps


View solution in original post

13 REPLIES 13

yes, i have declared $scope declared as a service in the client script function.


larstange
Mega Sage

Can you show me the script


The Widget is for converting the "hours:minutes:seconds" to duration type value. below is the script.


HTML:



<table>


  <th>Downtime</th>


  <tr>


  <td>Hours</td>


  <td>Minutes</td>


  <td>Seconds</td>


  </tr>


  <tr>


  <td><input type="number" ng-model="c.hours" ng-change="change()" min="0" max="9999" id="hours" /></td>


  <td><input type="number" ng-model="c.minutes" ng-change="change()" min="0" max="9999" id="minutes" /></td>


  <td><input type="number" ng-model="c.seconds" ng-change="change()" min="0" max="9999" id="seconds" /></td>


  </tr>  


  </table>



Client Controller:



function($scope) {


  /* widget controller */


  var c = this;


$scope.change=function()


{


var hours = c.hours;


var minutes = c.minutes;


var seconds = c.seconds;


console.log(c.hours+","+c.minutes+","+c.seconds);


console.log("hrs:mts:sec:"+ hours+":"+minutes+":"+seconds);


var durationField = hours+":"+minutes+":"+seconds;


var sp=durationField .split(':');


var sec = sp[0]*3600+sp[1]*60+sp[2]*1;


var val= parseInt(sec);


var days=Math.floor(val/(86400));


var hrs=Math.floor((val-days*86400)/3600);


var mts=Math.floor((val-days*86400-hrs*3600)/60);


var secs=Math.floor(val-days*86400-hrs*3600-mts*60);


var finalduration=days+" "+hrs+":"+mts+":"+secs;


console.log(finalduration);


var g_form = $scope.page.g_form;


g_form.setValue('sg_outage_duration',finalduration); // variable name sg_outage_duration


}


}



Thanks to help..


Skip the step where you try to create a variable on the Angular service and just do it in one line:



$scope.page.g_form.setValue('sg_outage_duration',finalduration);


Thanks, It worked.