Input to server coming as undefined in widget

kanishkab
Tera Contributor

Hi,

I'm trying to create a custom widget which will take input from user and based on it will provide count of incident records. For that I wanted to pass the user input to server side script.

HTML:

<div>
<!-- your widget template -->
<span class="sn-canvas-header-tools" >Task Overview</span>
<div>
<table>
<tr>
<th class ="required">Owning Business</th>
</tr>
<tr>
<td>
<select ng-model="data.owningBusiness" required onchange="myMarket()">

<option id = 'am' ng-model = 'c.data.fname' ng-repeat="i in data.market" value={{i}}>{{i}}</option>
</select>
</td>
<td><button class="button" id = 'button1' type = 'submit' ng-click = 'ApplyFilter()' >Filter</button></td>
</tr>
</table>
</div>
</div>

 

Client script:

api.controller=function($scope, $rootScope) {
/* widget controller */
var c = this;
c.data = $scope.data;
c.server.update();

$scope.ApplyFilter = function() {
//alert("count1: " + c.data.count1);
var che = document.getElementById('am').text;
alert("value: " + che);
c.data.trigger = true;
c.data.ob = 'OwB'
c.server.update().then(function(){})

}
Server script:

(function() {
    /* populate the 'data' object */
    /* e.g., data.table = $sp.getValue('table'); */
 
    data.owningBusiness = $sp.getParameter('owningBusiness');
    var userGr = new GlideRecord('sys_user');
    userGr.addQuery('sys_id', gs.getUserID());
    userGr.query();
    if (userGr.next()) {
        var associatedMarket = userGr.getDisplayValue('u_associated_markets');
        if (associatedMarket !== null) {
            data.market = associatedMarket.split(',');
        }
    }
    if (input.ob) {
console.log('this'+ input.fname);
}
 
But here in fname on server side I'm getting undefined, also alert on client side is returning every time same value, even if I select some other value

 

3 REPLIES 3

dgarad
Giga Sage

Hi @kanishkab 

 try the below code

HTML
<div>
<!-- your widget template -->
<span class="sn-canvas-header-tools" >Task Overview</span>
<div>
<table>
<tr>
<th class ="required">Owning Business</th>
</tr>
<tr>
<td>
<select ng-model="data.owningBusiness" required onchange="myMarket()">

<option id = 'am' ng-model = 'fname' ng-repeat="i in data.market" value={{i}}>{{i}}</option>
</select>
</td>
<td><button class="button" id = 'button1' type = 'submit' ng-click = 'ApplyFilter()' >Filter</button></td>
</tr>
</table>
</div>
</div>

clientside code
api.controller=function() {
  /* widget controller */
  var c = this;
  

$scope.ApplyFilter = function() {
//alert("count1: " + c.data.count1);
var obj={};
obj.fname= c.fname;
alert("value: " + obj.fname);
obj.trigger = true;
obj.ob = 'OwB';
c.server.update(obj).then(function(){

});
};
};
server-side code
(function() {
  /* populate the 'data' object */
  /* e.g., data.table = $sp.getValue('table'); */
  data.owningBusiness = $sp.getParameter('owningBusiness');
    var userGr = new GlideRecord('sys_user');
    userGr.addQuery('sys_id', gs.getUserID());
    userGr.query();
    if (userGr.next()) {
        var associatedMarket = userGr.getDisplayValue('u_associated_markets');
        if (associatedMarket !== null) {
            data.market = associatedMarket.split(',');
        }
    }
    if (input.ob) {
$sp.log('this'+ input.fname);
	}

})();
If my answer finds you well, helpful, and related to the question asked. Please mark it as correct and helpful.

Thanks
dgarad

kanishkab
Tera Contributor

Not getting any output with this, instead getting error on line 16 saying cannot read ob from undefined

server-side ya client side 

If my answer finds you well, helpful, and related to the question asked. Please mark it as correct and helpful.

Thanks
dgarad