Unable to access g_form in widget code

Dave Clemmer
Mega Guru

I am working my way through a method for one service portal page to call another and pass arguments.  At least, that's the idea.

As a waypoint along that path, I am trying to write a widget version of onChange().  Yes, $rootScope.$on('field.change', function(e,a) {...}) is the overall mechanism.  Anyway, for various reasons, within that method, I want to access all the fields for this form (the widget in question is a copy of the OOTB form widget.  So far, the only change is this onChange() handler, which is in the client controller).  So, ideally, I would access the g_form object (which, thanks to code created for the old UI, but which did only most of what I wanted, I know exists).  I've read, in several places, that $scope.page.g_form should provide that.

It doesn't.  The $scope.page object does exist, but it has no g_form property (nor anything close, come to that.  All of its properties, so far as I can tell, are strings).

Any thoughts on what stupid mistake I undoubtedly made here?

Thanks,

Dave

1 ACCEPTED SOLUTION

 

Dave, 

Get the reference at initialization, then use it on field change:

            var g_form;
            $scope.$on('spModel.gForm.initialized', function(e, gFormInstance) {
                g_form = gFormInstance;
            });

View solution in original post

10 REPLIES 10

Mohith Devatte
Tera Sage
Tera Sage

hello @Dave Clemmer ,

you need to access it like below syntax 

var  g_form= $scope.page.g_form

And one more thing you need to do it pass $scope in api.controller function in the client controller like below 

All this should be inside the filed.change event 

api.controller=function($scope) {
  /* widget controller */
  var c = this;
	var g_form = $scope.page.g_form;
};

Hope this helps 

PLEASE MARK MY ANSWER CORRECT IF THIS HELPS YOU

 

Not sure if I'm understanding you... The Form widget has a controller like:

function($rootScope, $scope, $timeout, $location, $log, $window, spUtil, nowAttachmentHandler, spAriaUtil, spNavStateManager) {

}

In the middle of that function, I put my

$rootScope.$on('field.change', function(evt, args) {

   var frm = $scope.page.g_form;

   spUtil.addInfoMessage("Form obj: " + JSON.stringify(frm));

// info message shows frm to be undefined

// attempt to do other stuff here

 });

I did some further info messages that showed me properties of $scope.page.

So, is that different from what you are saying? (I don't think it is, but I don't know enough about ServiceNow to be sure of that.)

Thanks for responding,

Dave

@Dave Clemmer this is actually perfect that you wrote which works as same as my code which i provided above 

so if you want to access any field you can do this as per your code 

instead of args try passing in parms like below 

copy paste the below code and try once 

$rootScope.$on('field.change', function(evt,parms) {

   var frm= $scope.page.g_form;
alert(frm.getValue('your_field_name));

});

Hope this helps 

Hmm... ok... I think you missed, though, the comment in there saying that $scope.page.g_form is undefined.  That's what has me baffled.

Thanks again for the response,

Dave