We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

How to pass the current object in ui page(html) to processing script ?

Kevin Smith2
Giga Expert

Hi,

I have a requirement to pass the current object from html to processing script.

Here is my code snippet:

HTML:

<g:evaluate var="jvar_taskobj" jelly="true" object="true">
			
			var base_table_name = RP.getWindowProperties().get('sysId');
			var gr = new GlideRecord("wm_task");
			gr.addQuery('sys_id',base_table_name);
			gr.query();
			gr.next();
			gr;
	</g:evaluate>

Processing script:

if ('false' == cancelled) {
var clone = new SMTask().cloneTask(taskobj);
(new StateFlow().processFlow(taskobj, '41dfa2e0d7630100fceaa6859e61035d', 'manual'));
gs.addInfoMessage(gs.getMessage("Cloned from {0}", task_number));
response.sendRedirect("wm_task.do?sys_id=" + clone);
}

How can I pass the current object in the above code "taskobj" is not working.

Thanks

Kevin

1 ACCEPTED SOLUTION

Hi,

I am referring to the <g:evaluate> tag

I could find in both the g:evaluate tags you are doing query of same table "wm_task" with same sys_id using RP.getWindowProperties().get('sysId')

I could see already hidden element "task_id" which stores the sys_id at the top.

So no need of new hidden element.

you can use that directly in processing script

Try this

if ('false' == cancelled) {

    gs.info('Inside sysid is: ' + task_id);
    var gr = new GlideRecord('wm_task');
    gr.get(task_id);

    var clone = new SMTask().cloneTask(gr);
    (new StateFlow().processFlow(gr, '41dfa2e0d7630100fceaa6859e61035d', 'manual'));
    gs.addInfoMessage(gs.getMessage("Cloned from {0}", task_number));
    response.sendRedirect("wm_task.do?sys_id=" + clone);
}

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 10x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

24 REPLIES 24

Hi Kevin,

please share your script HTML and processing

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 10x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

HTML:

<g:ui_form>
 <!-- <g:requires name="scripts/sn.dragdrop/jquery-ui.min.js" includes="true" />
    <g:requires name="scripts/lib/jquery_includes.js" />
    <g:requires name="styles/heisenberg/heisenberg_all.css" includes="true" />
    <g:requires name="scripts/sn/common/messaging/deprecated/NOW.messaging.js" />
    <g:requires name="scripts/sn/common/messaging/deprecated/NOW.messaging.record.js" />
    <g:requires name="scripts/app.scrum/NOW.messaging.sdlc.js" /> -->
  <j:set var="task_id" value="${RP.getWindowProperties().get('sysId')}" />
  <input type="hidden" name="cancelled" id="cancelled" value="false"/>
  <input type="hidden" name="task_id" id="task_id" value="${RP.getWindowProperties().get('sysId')}"/>
  <input type="hidden" name="task_number" id="task_number" value="${RP.getWindowProperties().get('number')}"/>
<j:set var="cancelled" value="false"/>
  

<style>
	
#task_close_incomplete .left {
    max-width: 560px;
	padding-left: 10px;
}

#task_close_incomplete #yesno {
    width: 98%
}

HTML[data-doctype=true] #task_close_incomplete #yesno {
    width: 100%
}
</style>
<table id="task_close_incomplete" style="width:560px">
 
 <g:evaluate var="jvar_taskid" jelly="true" object="true">
			var objWOT = {};
			//var styleFilter;
			var base_table_name = RP.getWindowProperties().get('sysId');
			var gr = new GlideRecord("wm_task");
			gr.addQuery('sys_id',base_table_name);
			gr.query();
			if(gr.next())
			{
				objWOT.u_serial_number =gr.u_serial_number;
				objWOT.u_asset_tag =gr.u_asset_tag;
				objWOT.u_pcd_style =gr.u_pcd_style;
				objWOT.u_pcd_style_display =gr.u_pcd_style.getDisplayValue();
				objWOT.u_billable= gr.u_billable;
				objWOT.under_warranty = gr.under_warranty;
				objWOT.styleFilter = "u_company="+gr.company+"^u_active=true";
				//gs.info('##uip '+task_id + 'gr.u_serial_number '+gr.u_serial_number );
			}
			objWOT.timeworked= new GlideDate();
			objWOT.slaQuery= "task="+base_table_name;
			objWOT;
	</g:evaluate>
	
	<g:evaluate var="jvar_taskobj" jelly="true" object="true">
			
			var base_table_name = RP.getWindowProperties().get('sysId');
			var gr = new GlideRecord("wm_task");
			gr.addQuery('sys_id',base_table_name);
			gr.query();
			gr.next();
			gr;
	</g:evaluate>


<input type="hidden" id="hidden_sys_id" value="${jvar_taskobj.sys_id}"></input>
		 <tr>
    <td class="left">
      
      <label for="yesno">Serial Number</label>
    </td>
	 <td align="left" style="padding: 0 0 4px 8px;">
	 <input type="text" id="serial_number" name="serial_number" value="${jvar_taskid.u_serial_number}" />
	 </td>
	</tr>
		
	<tr>
    <td class="left">
   
      <label for="yesno">Asset Tag</label>
    </td>
	 <td align="left" style="padding: 0 0 4px 8px;">
	 <input type="text" id="asset_tag" name="asset_tag" value="${jvar_taskid.u_asset_tag}"/>
	 </td>
	</tr>
	
	<tr>
    <td class="left">
      
      <label for="yesno">Style </label>
    </td>
	 <td align="right" style="padding: 0 0 4px 8px;">
	<g:ui_reference jelly="true" name="u_pcd_style" id="u_pcd_style" table="u_pcd" completer="AJAXTableCompleter" query="${jvar_taskid.styleFilter}" value="${jvar_taskid.u_pcd_style}" displayvalue="${jvar_taskid.u_pcd_style_display}" />
	 </td>
	</tr>
	
	<tr>
    <td class="left">
   
      <label for="yesno">Billable </label>
    </td>
	 <td align="left" style="padding: 0 0 4px 8px;">
	 <g:ui_checkbox name="u_billable" value="${jvar_taskid.u_billable}"/>
	 </td>
	</tr>
	
	<tr>
    <td class="left">
     
      <label for="yesno">Under warranty </label>
    </td>
	 <td align="left" style="padding: 0 0 4px 8px;">
	 <g:ui_checkbox name="under_warranty" value="${jvar_taskid.under_warranty}"/>
	 </td>
	</tr>
	
	<tr>
    <td class="left">
       <span title="${gs.getMessage('Mandatory - must be populated before Submit')}" mandatory="true" oclass="mandatory" style="background-color:#C00;color:#C00;margin-right:3px;margin-left:1px;">I</span>
      <label for="yesno">Activity  </label>
    </td>
	 <td align="left" style="padding: 0 0 4px 8px;">
	<select id="timecategory" name="timecategory" >
          <option value="--None--">--None--</option>
          <g2:evaluate>	  
			var grChoice = new GlideRecord('sys_choice');
			grChoice.addEncodedQuery("nameINjavascript:getTableExtensions('time_card')^element=category^language=en^inactive=false");
			grChoice.query();
	</g2:evaluate>
			
          <j2:while test="$[grChoice.next()]">
			  
            <option value="$[grChoice.value ]">$[grChoice.label]</option>
          </j2:while>
        </select>		  
	 </td>
	</tr>
	
	<tr>
    <td class="left">
   
      <label for="yesno">Date Worked</label>
    </td>
	 <td align="left" style="padding: 0 0 4px 8px;">
	<g:ui_date name="date_worked" value="${jvar_taskid.timeworked}"/>
	 </td>
	</tr>
	
	
	<tr>
    <td class="left">
   
      <label for="yesno">Time Worked</label>
    </td>
	 <td align="left" style="padding: 0 0 4px 8px;">
		<table>
  <th>Days</th>
  <th>Hours</th>

  <th>Minutes</th>

  <th>Seconds</th>

  <tr>
	<td><input type="number" min="0" max="23" id="days" name ="days" onChange="validateHours(this.value)"/></td>
  <td><input type="number" min="0" max="23" id="hours" name ="hours" onChange="validateHours(this.value)"/></td>

  <td><input type="number" min="0" max="59" id="minutes" name="minutes" onChange="validateMinutes(this.value)"/></td>

  <td><input type="number" min="0" max="59" id="seconds" name="seconds" onChange="validateSeconds(this.value)"/></td>

  </tr>

  </table>
  
	 </td>
	</tr>
<script>
function validateHours(value){

}


function validateMinutes(value){

}


function validateSeconds(value){ 

}

</script>

	<tr>
    <td class="left">
   
      <label for="yesno">Total Distance</label>
    </td>
	 <td align="left" style="padding: 0 0 4px 8px;">
	 <input type="text" id="total_distance" name="total_distance" />
	 </td>
	</tr>
	
	
	<tr>
    <td class="left" valign="top">
      <span title="${gs.getMessage('Mandatory - must be populated before Submit')}" mandatory="true" oclass="mandatory" style="background-color:#C00;color:#C00;margin-right:3px;margin-left:1px;">I</span>
      <label for="close_notes">Close notes</label>
    </td>
    <td align="right">
      <textarea wrap="soft" autocomplete="off" rows="5" id="close_notes" data-length="4000" style="; width:98%; overflow:auto; background-color:LightGoldenRodYellow;" name="close_notes"></textarea>
    </td>
  </tr>
  
    
   <tr>
    <td class="left" valign="top">
      
      <label for="work_notes_update">Work notes</label>
    </td>
    <td align="right">
      <textarea wrap="soft" autocomplete="off" rows="5" id="work_notes" data-length="4000" style="; width:98%; overflow:auto; background-color:LightGoldenRodYellow;" name="work_notes"></textarea>
    </td>
  </tr>
  
    <tr>
    <td class="left" valign="top">
     
      <label for="work_notes_update">Additional Comments</label>
    </td>
    <td align="right">
      <textarea wrap="soft" autocomplete="off" rows="5" id="comments" data-length="4000" style="; width:98%; overflow:auto; background-color:LightGoldenRodYellow;" name="comments"></textarea>
    </td>
  </tr>
  <tr>
  
    <td colspan='2'>  <g:dialog_buttons_ok_cancel ok="return submitChanges()" cancel="return submitCancel()" />
	 </td> 
  </tr>
	
  <!-- 
  <tr>

  <td colspan='2'> 
	 <g:list_default table="task_sla" view="WOTList"/>
	 <g:evaluate jelly="true">
            var label = "WOT SLA List";
            //var title = gs.getMessage("WOT SLA List");
            ListProperties.setHasTopNav(true);
            ListProperties.setHasTitle(true);
            ListProperties.setHasTitleContextMenu(false);
            ListProperties.setHasFilter(false);
            ListProperties.setHasBreadcrumbs(false);
            ListProperties.setHasSearch(false);
            ListProperties.setHasTopVCR(false);
            ListProperties.setHasHeader(true);
            ListProperties.setHasHeaderContextMenu(false);
            ListProperties.setHasListMechanic(false);
            ListProperties.setHasRowContextMenu(false);
            ListProperties.setShowLinks(false);
            ListProperties.setHasPopup(false);
            ListProperties.setShowEmpty(true);
            ListProperties.setHasBottomNav(false);
            ListProperties.setHasActions(false);
            ListProperties.setHasBottomVCR(false);
            ListProperties.setCanChangeView(false);
            ListProperties.setCanGroup(false);
            ListProperties.setListName(label);
            ListProperties.setListID(label);
            ListProperties.setTitle("WOT SLA List");
           RP.setView('WOTList');
         </g:evaluate>

        <g2:list_record_default properties="${ListProperties.serialize()}" query="${objWOT.slaQuery}">
            <g:inline template="list2.xml" />
        </g2:list_record_default>

  </td>
  </tr>
-->

<tr>
  <td colspan='2'>
	<div id="targetList">      
	<iframe src="task_sla_list.do?sysparm_query=task=${task_id}" style="min-height:40px;width:100%;" scrolling="no" frameborder="0"></iframe>
	</div>

	<script>
		
		  var caller = document.getElementById('task_id').value ;
		  //alert('caller ' + caller);
		var newIframe = '<iframe src="task_sla_list.do?sysparm_query=task='+caller+'" style="min-height:500px;width:100%;" scrolling="no" frameborder="0"></iframe>' ;
		  document.getElementById('targetList').innerHTML =newIframe ;
		
	</script>

	
  </td>
  
  </tr> 



</table>
</g:ui_form>

 

Client Script:

function submitCancel() {
    var c = gel('cancelled');
    c.value = "true";
    GlideDialogWindow.get().destroy();
    return false;
}

 

Processing Script:

if ('false' == cancelled) {

    var sysid = hidden_sys_id;

    var gr = new GlideRecord('wm_task');
    gr.get(sysid);

    var clone = new SMTask().cloneTask(gr);
    (new StateFlow().processFlow(taskobj, '41dfa2e0d7630100fceaa6859e61035d', 'manual'));
    gs.addInfoMessage(gs.getMessage("Cloned from {0}", task_number));
    response.sendRedirect("wm_task.do?sys_id=" + clone);
}

 

UI Action:

function closeIncomplete() {
    var gdw = new GlideDialogWindow('service_management_close_incomplete');
    gdw.setTitle('Close Incomplete');
    gdw.setSize(750, 300);
    gdw.setPreference('sysId', g_form.getUniqueValue());
    gdw.setPreference('number', g_form.getValue('number'));
    gdw.render();
}

Hi,

try adding log in processing script

also you need to update gr in 2 places

if ('false' == cancelled) {

    var sysid = hidden_sys_id;
    gs.info('Inside sysid is: ' + sysid);
    var gr = new GlideRecord('wm_task');
    gr.get(sysid);

    var clone = new SMTask().cloneTask(gr);
    (new StateFlow().processFlow(gr, '41dfa2e0d7630100fceaa6859e61035d', 'manual'));
    gs.addInfoMessage(gs.getMessage("Cloned from {0}", task_number));
    response.sendRedirect("wm_task.do?sys_id=" + clone);
}

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 10x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi Ankur,

I am getting the following warning:

 

find_real_file.png

Hi Kevin,

please update the HTML Section as below

I have given the name attribute as name="hidden_sys_id"

<input type="hidden" name="hidden_sys_id" id="hidden_sys_id" value="${jvar_taskobj.sys_id}"></input>

please update processing script as below:

Directly use that hidden variable

if ('false' == cancelled) {

    gs.info('Inside sysid is: ' + hidden_sys_id);
    var gr = new GlideRecord('wm_task');
    gr.get(hidden_sys_id);

    var clone = new SMTask().cloneTask(gr);
    (new StateFlow().processFlow(gr, '41dfa2e0d7630100fceaa6859e61035d', 'manual'));
    gs.addInfoMessage(gs.getMessage("Cloned from {0}", task_number));
    response.sendRedirect("wm_task.do?sys_id=" + clone);
}

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 10x ServiceNow MVP  ||  ✨ ServiceNow Community Leader