Jelly Script : Dynamic Content Block
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-16-2014 05:04 AM
Hello Again Community,
I'm working on building a dynamic content block that will modify a value on the sys_user record when you click a button. I want to do this using Jelly script but i'm not sure on how to layout the script so I tried using <script> tags instead. I created a UI Macro that has this code in it and just used the <g:> tag to invoke the macro in the dynamic content block. The button shows up but nothing happens when i click on it. The idea is to run a query to the user record and see if a specific field is true or false. If true, it should change it to false, and if false, should change it to true. Any idea why this is not working when you click the button?
For the button...
<button type="button" onclick="updateStatus()">Change Status</button>
The script behind the button...
<script>
function updateStatus(){
var gr2 = new GlideRecord('sys_user');
gr2.addQuery('sys_id','9b4a46976f1d110092a4186e6b3ee4c1');
gr2.query();
while(gr2.next()){
if(gr2.u_is_available_ == true){
gr2.u_is_available_ = false;
gr2.update();
}else if(gr2.u_is_available_ == false){
gr2.u_is_available_ = true;
gr2.update();
}
}
}
</script>
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2014 06:25 AM
Hi Kenneth,
Everything is correct just write false in quotes
else if(gr2.u_is_available_ == 'false'
This should work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2015 08:25 AM
I used a combination of javascript and jelly to get this to work.
In the UI Macro I first check the status of the user (I just used the active field) by using jelly.
UI Macro
<g:evaluate var="jvar_bool" jelly="true">
var grUsr = new GlideRecord('sys_user');
grUsr.get('${RP.getParameterValue("sysparm_usr_sys_id")}'); //I used a URL parameter with user id value
if(grUsr){
var theBool = grUsr.active; // check my desired field
}
if(theBool != true){
theBool = false;
}
theBool;
</g:evaluate>
Also in the UI Macro I then created two separate functions. One to set to false and one to set to true.
<script>
function setFalse(usr_sys_id){
var deactUser = new GlideRecord('sys_user');
deactUser.get(usr_sys_id);
if(deactUser){
deactUser.active = false;
deactUser.update();
}
}
function setTrue(usr_sys_id){
var deactUser = new GlideRecord('sys_user');
deactUser.get(usr_sys_id);
if(deactUser){
deactUser.active = true;
deactUser.update();
}
}
</script>
UI Page
Then in the UI Page I call the macro and check the status from the output of the g:evaluate script. Then depending upon the boolean display a button with an onclick of the corresponding function to set true or set false.
<g:inline template="myMacro.xml" />
<j:set var="jvar_usrSysID" value="${RP.getParameterValue('sysparm_usr_sys_id')}" /> //grab sys_id passed in URL
<input type='hidden' name='usrSysID' value="sysparm_usr_sys_id=${RP.getParameterValue('sysparm_usr_sys_id')}" /> //grab sys_id passed in URL for the processor script to redirect after submitting. Not required.
<g:ui_form>
<j:if test="${jvar_bool == true}">
<p>This user is active</p>
<button type="submit" onclick="setFalse('${jvar_usrSysID}')">Deactivate</button>
</j:if>
<j:if test="${jvar_bool == false}">
<p>This user is not active</p>
<button type="submit" onclick="setTrue('${jvar_usrSysID}')">Activate</button>
</j:if>
</g:ui_form>
I used a form because it was hard to tell if the script was working after clicking the button when the type is just button. By using a form and type submit, it gave me an indicator that action was taking place because when the page refreshed the buttons and verbiage changed reflecting the change in status. But the script still works if not a form.