Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

getElementById() not working

Justin Dreher
Kilo Expert

Hi all,

In trying to enhance the functionality of a widget in the service portal, I have come across what I fee is an error. I am using the method "getElementById()" on an element that I know for sure exists, and it is coming back null.

Is there a work around to this or and alternative method I should/could use?

Thanks!

1 ACCEPTED SOLUTION

I just realized I named the variable incorrectly in the ng-if lol. That's probably the issue

 

This is what I had:
<div id="ifTrue" ng-if="data.userFormApproval">

...

<div id="ifFalse"ng-if="!data.userFormApproval">

 

But you're setting the true/false to this variable in your server script: data.userFormApprovalArray.

So it should be:

<div id="ifTrue" ng-if="data.userFormApprovalArray">

...

<div id="ifFalse"ng-if="!data.userFormApprovalArray">

 

Let me know if that works! I edited my original post with that change.

View solution in original post

9 REPLIES 9

Hi Justin,

Sorry for the late reply!

Instead of doing it that way, I would recommend using in the userFormApproval variable in data object and leveraging ng-if on the two div's (the ifTrue ifFalse ones). Try to take advantage of angular where possible. Ng-if will hide/show the content based on the condition you give.

This is going to be untested, but here is what I'd suggest.

HTML:


<div id="ifTrue" ng-if="data.userFormApprovalArray">
<div ng-if="options.portal && approval.state == 'requested'" class="col-xs-6">
<button name="reject" class="btn btn-danger btn-block" ng-click="reject(approval.sys_id);">${Do Not Need}</button>
</div>
<div ng-if="options.portal && approval.state == 'requested'" class="col-xs-6">
<button name="approve" class="btn btn-success btn-block" ng-click="approve(approval.sys_id);">${Completed}</button>
</div>
</div>

<div id="ifFalse"ng-if="!data.userFormApprovalArray">
<div ng-if="options.portal && approval.state == 'requested'" class="col-xs-6">
<button name="reject" class="btn btn-danger btn-block" ng-click="reject(approval.sys_id);">${Reject}</button>
</div>
<div ng-if="options.portal && approval.state == 'requested'" class="col-xs-6">
<button name="approve" class="btn btn-success btn-block" ng-click="approve(approval.sys_id);">${Approve}</button>
</div>
</div>

Client Controller:

//Don't need anything here

 

Server Script:

 

var userFormApproval = false;
if(j.instructions.indexOf('User Form') !== -1){
userFormApproval = true;
}else{
userFormApproval = false;
}

data.userFormApprvalArray = [];
data.userFormApprovalArray = userFormApproval;

Hi Justin,

Any updates here? Did the code I posted solve the issue?

If so, can you please mark my answer as correct, otherwise I'm happy to continue to help

Thanks,

Josh

Hi, sorry for the delay!

I tried your code and, while the value was coming through as true it still showed the incorrect set of buttons. could it be the buttons?

Thanks!

I just realized I named the variable incorrectly in the ng-if lol. That's probably the issue

 

This is what I had:
<div id="ifTrue" ng-if="data.userFormApproval">

...

<div id="ifFalse"ng-if="!data.userFormApproval">

 

But you're setting the true/false to this variable in your server script: data.userFormApprovalArray.

So it should be:

<div id="ifTrue" ng-if="data.userFormApprovalArray">

...

<div id="ifFalse"ng-if="!data.userFormApprovalArray">

 

Let me know if that works! I edited my original post with that change.

That worked!

Thank you so much!