Multi Row Variable Set Problem

Ace009
Tera Contributor

I have an issue Im able to replicate even on my PDI.

 

SN Version:  Washington DC

Problem: When I have a Multi Row Variable Set, clicking the Add button seems to break the g_form API.

 

How to replicate on PDI: On a record producer, add the following variables:

 

Description (Single Line Text)

Multi Row Variable Set: (has a single line text variable)

 

Create the following widget and add it to sc_cat_item:

 

HTML

 

<button type="button" class="btn btn-primary btn-block" ng-click="c.uiAction()">Click</button>

 

 

Client Controller

 

 

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

	$rootScope.$on('spModel.gForm.initialized', function(e,gFormInstance){
		g_form = gFormInstance;
	})
	
  c.uiAction = function(action){
	alert(1);
	g_form.setValue('description1',123);
  }
};

 

 
 
Example Record Producer.png

 
 
Clicking the button simply sets the description field to 123.
 
However, clicking the OOTB Add button for the MultiRow Variable set seems to cause g_form to no longer.
Why does this happen? 
 
1 ACCEPTED SOLUTION

Nick Parsons
Mega Sage

Why does this happen? 

Because ServiceNow emits the "spModel.gForm.initialized" event not just on your parent form load, but also when the MRVS popup form loads too, overwriting the reference you have set for g_form.

 

How can you fix this?
I suggest using a different method to get a reference to the g_form, such as through the scope's page:

api.controller=function($scope) {
  /* widget controller */
  var c = this;
  var g_form = $scope.page.g_form; // get the g_form via the page of the current $scope
	
  c.uiAction = function(action){
    alert(1);
    g_form.setValue('description1',123);
  }
};

View solution in original post

3 REPLIES 3

Nick Parsons
Mega Sage

Why does this happen? 

Because ServiceNow emits the "spModel.gForm.initialized" event not just on your parent form load, but also when the MRVS popup form loads too, overwriting the reference you have set for g_form.

 

How can you fix this?
I suggest using a different method to get a reference to the g_form, such as through the scope's page:

api.controller=function($scope) {
  /* widget controller */
  var c = this;
  var g_form = $scope.page.g_form; // get the g_form via the page of the current $scope
	
  c.uiAction = function(action){
    alert(1);
    g_form.setValue('description1',123);
  }
};

Thanks for confirming that opening a MRVS modal also triggers that event. It was driving me crazy trying to understand why the fields returned from g_form.getFieldNames() were changing on form load and when adding entries to an MRVS, lol.

So the workaround of using $scope.page.g_form doesn't seem to work for me. I get undefined in the log. Are there some prerequisites needed to get it to work?  

Hi @peter0611 - I think I see what the issue is. My original assumption was that you added your custom widget as a variable to the catalog item/record producer (using the "Custom" variable type), but looking at your screenshot again and your post, it looks like that's not the case and you've added it to the sc_cat_item page itself. The above would work if you were adding the widget within the record producer itself using the "Custom" variable type. So if you can move the widget into the record producer as a variable, then the above should work. Otherwise I believe you'd need to look at accessing the DOM node of the catalog item to read it's scope from your widget (or customing the catalog item widget [HRM Catalog Item]).