UI Page is disappearing after record update

Jeff_GWC
Tera Contributor

I have a UI page that is not staying open when saving a record. I need it to function in the following manner. 

  • On the record, some fields are updated. The user clicks the modified "Save" button
  • The UI Action saves the current record, then based on certain criteria from the record a UI page will load.

When the updating the record and clicking the Save button, the UI page displays briefly and then the form reloads and it is gone. The record does save properly, however, I can't get the UI page to stay open. 

If I remove the line, g_form.save();, the ui page loads without a problem and functions the way it needs to, however, the original changes do not save to the current record. 

UI Action: 

  • Name: Save
  • Action Name: sysverb_update_and_stay
  • Client is checked
  • Onclick: checkState();

Script: 

function checkState(){
	var state = g_form.getValue('u_org_state');
	var leads = g_form.getValue('current.u_active_related_leads');
	
	if (state == 20 || state == 30 || state == 40){
		if (leads > 0){
			
			g_form.save();
			openDialogWindow();
		} else {
			gsftSubmit(null, g_form.getFormElement(), 'sysverb_update_and_stay');
		}
		
	} else {
		gsftSubmit(null, g_form.getFormElement(), 'sysverb_update_and_stay');
	}
}

function openDialogWindow(){
	var sysid = g_form.getUniqueValue();
	var organization = g_form.getDisplayValue('organisation_name');
	var active_leads = g_form.getValue('u_active_related_leads');
	
	var dialog = new GlideModal('x_vort2_crmnow_crm_update_active_leads', true); //Instantiate the dialog containing the UI Page 'add_comments_dialog'
	dialog.setTitle('Update Active Lead Records'); // Sets the dialog title
	dialog.setPreference('sourceID', sysid);
	dialog.setPreference('organization', organization);
	dialog.setPreference('active_leads', active_leads);
	dialog.render(); // opens the dialog
}


//Code that runs without 'onclick'
//Ensure call to server-side function with no browser errors
if(typeof window == 'undefined')
	runServerSideCode();

function runServerSideCode(){
	current.update();
	if (! current.isActionAborted())
		action.setRedirectURL(current);
}

 

UI Page: 
  • Name: crm_update_active_lead
HTML
	<g:ui_form>
		<!-- Get the values from dialog preferences -->
		<g:evaluate var="jvar_sysid" expression="RP.getWindowProperties().sourceID" />
		<g:evaluate var="jvar_organization" expression="RP.getWindowProperties().organization" />
		<g:evaluate var="jvar_active_leads" expression="RP.getWindowProperties().active_leads" />
		<g:evaluate>
			var gr_status = new GlideRecord('sys_choice');
			gr_status.addEncodedQuery('name=x_vort2_crmnow_lead^element=state^inactive!=true');
			gr_status.orderBy('sequence');
			gr_status.query();
		</g:evaluate>
		<!-- Set up form fields and labels -->
		<head>
			<style>
				table {
					width: 100%;
					border-collapse: collapse;	
				}
				.td1 {
					text-align:center;
					color:red;
				}
				.td2 {
					vertical-align: top;	
					text-align:left;
					font-size:15px;
				}
			</style>
		</head>
		<input type="hidden" id="no_update" name="no_update" value="true"/>
		<input type="hidden" id="org_sysid" name="org_sysid" value="${jvar_sysid}"/>
		<table>
			<tr>
				<td id="organization" class="td2">
					Organization:<font style ="color:#003087; padding-left:10px"> ${jvar_organization}</font>
				</td>
				<td id="activelead" class="td2">
					Active Leads:<font style ="color:#003087; padding-left:10px"> ${jvar_active_leads}</font>
				</td>
			</tr>
			<tr >
				<td class="td2" style="padding-top:20px;" colspan="2">
					<font style="padding-right:10px"> A blocked state was chosen, do you want to update all active leads? </font>
					<font style="padding-right:10px"> <g:ui_radio id="yesRadio" name="update_leads" value="true" checked_value="${jvar_}" 
					onchange="showHideLeadFields();" text="${gs.getMessage('Yes')}"
					/> </font>
					<g:ui_radio	id="noRadio" name="update_leads" value="false" checked_value="${jvar_}"
					onchange="showHideLeadFields();"
					text="${gs.getMessage('No')}" />
				</td>
			</tr>
		</table>
		<table id="lead_updates" style="display:none">
			<tr>
				<td class="td1" colspan="2">
					You may only update leads with a status that indicates it is blocked.
				</td>
			</tr>
			<tr>	
				<td class="td1" colspan="2">
					(i.e. Denied By GWCC, Rejected By Agent, etc.)
				</td>
			</tr>
			<tr>
				<td style="vertical-align:bottom; text-align:right; height:50px;">
					<!-- Status Choice list populated with the values from the lead table -->
					<font style="padding-right:10px"> Lead Status: </font>
				</td>
				<td style="vertical-align:bottom; text-align:left; height:50px;">
					<select id="status_select" onchange="disableEnableLeadFields()" name="status" >
						<option value="">-- None --</option>
						<j:while test="${gr_status.next()}">
							<option value="${gr_status.value}">${gr_status.label}</option>
						</j:while>
					</select>
				</td>
			</tr>
			<tr>
				<td style="vertical-align:bottom;text-align:right; height:35px;">
					<!-- Status Choice list populated with the values from the lead table -->
					<font style="padding-right:10px">Lead Status Reason: </font> 
				</td>
				<td style="vertical-align:bottom;text-align:left; height:35px;">
					<g:ui_choicelist name="reason" table="x_vort2_crmnow_lead" field="u_status_reason" />				
				</td>
			</tr>
			<tr>
				<td id="label.status_comments" class="label" nowrap="true" style="vertical-align: top; padding-top: 10px;" type="string" choice="0">
					<g:form_label > Status Comments </g:form_label>
				</td>
				<td style="padding-top: 10px;">
					<textarea id="status_comments" style="width: 100%" rows="10" name="status_comments" wrap="soft" size=""></textarea>
				</td>
			</tr>
			<tr>
				<td id="label.additional_comments" class="label" nowrap="true" style="vertical-align: top; padding-top: 10px;" type="string" choice="0">
					<g:form_label > Additional Comments </g:form_label>
				</td>
				<td style="padding-top: 10px;">
					<textarea id="additional_comments" style="width: 100%" rows="10" name="additional_comments" wrap="soft" size=""></textarea>
				</td>
			</tr>
		</table>
		<table>		
			<tr>
				<td colspan="2" style="text-align:center; padding-top:20px">
					<g:dialog_button_ok ok="return validateComments()" ok_id="ok_button" ok_type="submit" />
				</td>
			</tr>
		</table>
	</g:ui_form>

 

Processing script:

if (no_update == 'false'){
	var query = 'active=true^organisation=' + org_sysid;
	var leads = new GlideRecord('x_vort2_crmnow_lead');
	leads.addEncodedQuery(query);
	leads.query();
	
	while (leads.next()){
		gs.info("The LEAD is: " + leads.full_name);
		gs.info("Organization sysid: " + org_sysid);
		gs.info("Status Selected: " + status);
		gs.info("Reason Selected: " + reason);
		gs.info("Status Comments: " + request.getParameter("status_comments"));
		gs.info("Additional Comments: " + request.getParameter("additional_comments"));
		
		//leads.state = status;
		//leads.u_status_reason = reason;
		//leads.u_status_comment = status_comments;
		//leads.update();
	}
	
	var session = gs.getSession();
	var URL = session.getUrlOnStack();
    response.sendRedirect(URL);
}

Client script:

function showHideLeadFields(){
	// This script is called when the user clicks the Yes radio button. 
	// It shows the fields that are required to update a lead record. 
	var lead_updates = document.getElementById("lead_updates");
	var no_update = gel('no_update');
	
    if (document.getElementById("yesRadio").checked) {
		// If Yes is selected, display the information needed to update a lead record. 
		lead_updates.style.display = 'inline-table';
		no_update.value = "false";
	}
    else {
		// If Yes is NOT selected, hide the information needed to update a lead record and clear the values
		lead_updates.style.display = 'none';
		no_update.value = "true";		
	}
}

function disableEnableLeadFields(){
	// This script runs when the Status value is selected
	// It disables and clears the values of the Reason, Status Comments, and Additional Comments if the wrong status is selected.
	// Statuses that disable the ui page are ones where work is still required: 
	//     Unassigned, Assigned To Agent, Accepted By Agent, Submitted, Quoted, System Inactivated (this one doesn't allow any processing)
	var status = document.getElementById("status_select").value;
	var reason = document.getElementById("reason");
	var status_comments = document.getElementById("status_comments");
	var additional_comments = document.getElementById("additional_comments");
	var ok_button = document.getElementById("ok_button");
		
	if (status == '10' || status == '30' || status == '40' || status == '60' || status == '80' || status == '500'){
		// If the status is one of the ones listed above, set the fields to disabled
		reason.disabled = true;
		status_comments.disabled = true;
		additional_comments.disabled = true;
		ok_button.disabled = true;
	} else {
		// If the status is not one of the ones listed above allow entry set the fields to not disabled
		reason.disabled = false;
		status_comments.disabled = false;
		additional_comments.disabled = false;
		ok_button.disabled = false;
	}
}

function validateComments(){
	// This script is called when the user clicks the Submit button
	// If the YES radio button is selected, make sure Status, Reason, and Status Comments have been entered before processing
	// If the NO radio button is selected, set comments on the organization and close the dialog window
	var lead_updates = document.getElementById("lead_updates");
	
	if (document.getElementById("yesRadio").checked) {
		var msg = [];
		var status = document.getElementById("status_select").value;
		var reason = document.getElementById("reason").value;
		var status_comments = document.getElementById("status_comments").value;

		if (status == ''){msg.push('Lead Status');}
		if (reason == ''){msg.push('Status Reason');}
		if (status_comments == ' '){msg.push('Status Comments');}

		if (msg != ''){
			alert('You must enter values for the following fields: ' + msg);
			return false;
		} else {
			return true;
		}
	}
    else {
		var sysid = document.getElementById("org_sysid").value;
		var gr = new GlideRecord('x_vort2_crmnow_organisation');
		gr.get(sysid);
		gr.additional_comments_public = 'System Generated Comment: A Blocked state was set with Active leads, however, when asked to update all active leads the user selected "No" and clicked the "Ok" button.';
		gr.update();

		GlideDialogWindow.get().destroy(); // Close the dialog window
		return false;
	}
}

 

Any help on this would be greatly appreciated, 

Thanks,

Jeff

1 ACCEPTED SOLUTION

Andrew Wortham
Kilo Guru

Hello Jeff,

 

You are going to have to consider doing this differently. 

On load of the page all client scripts are loaded. And only onLoad client scripts are run.

Because g_form.save() causes a reload, you are getting only as much "openDialogWindow();" time as it takes the page to reload.

 

There is no way to do what you want with the scripts in the current order they are in.  You could either

1) Move the Dialog window to an onLoad client script.  (You can add conditional logic as needed as to when it runs.  If needed, you could add a hidden field, that "Save" triggers, and then the onLoad client script sets back to false)

2) Move the save until after the dialog window.

 

Something has to give in you current order.  I am thinking that #1 will better suit your needs.

 

Does that make sense?

 

Best,

Andrew

View solution in original post

5 REPLIES 5

Andrew Wortham
Kilo Guru

Hello Jeff,

 

You are going to have to consider doing this differently. 

On load of the page all client scripts are loaded. And only onLoad client scripts are run.

Because g_form.save() causes a reload, you are getting only as much "openDialogWindow();" time as it takes the page to reload.

 

There is no way to do what you want with the scripts in the current order they are in.  You could either

1) Move the Dialog window to an onLoad client script.  (You can add conditional logic as needed as to when it runs.  If needed, you could add a hidden field, that "Save" triggers, and then the onLoad client script sets back to false)

2) Move the save until after the dialog window.

 

Something has to give in you current order.  I am thinking that #1 will better suit your needs.

 

Does that make sense?

 

Best,

Andrew

Andrew,

Thanks for taking the time to respond. 

I am all for doing this differently, however, I am not sure if I am completely following the onLoad Client Script part. But I think maybe I have it. 

I create an onload client script that contains logic to check a hidden field and if that field is a certain value, launch the UI page. 

When I save the record with certain values set that hidden field so when the page reloads it will launch the UI page?? 

 

Is that what you were talking about? 

Thanks,
Jeff

 

Hello Jeff, 

 

Yes that is the same train of thought I was referring to! Please test that out and let me know if you run into any issues!

 

Best,

Andrew

Andrew,

I did what you had suggested. Moved my openDialog() functionality to an onLoad Client Script and it is working great now. Of course I need to do some more testing, but it is sufficient for what I needed. 

The only part of your suggestion I didn't do is create an extra hidden field. I was able to just move my IF logic into the onload client script. 

 

Thank you for your help. It is greatly appreciated. 

 

Jeff