number

bhaavani
Tera Contributor

Hi Experts,

 

I have a requirement. On an incident form we have a field called "Caused By Change". The requirement is on the closed incidents we need to created a button when clicked, opens a pop-up or modal and that window should populate the incident form "Caused By Change" value. if Caused by change is not having any value then we need to select existing Change Requests. I tried this by creating a UI action, UI page and called script include in the client script of UI page. However there is no luck. Need your expertise here. I cant paste the code here due to our project restriction. Its little critical

 

Thanks in advance.

@Ankur Bawiskar I saw you helped many people need your advice here as well.

1 ACCEPTED SOLUTION

@bhaavani 

this worked for me

Send the sysId and display value as well and set both

function showModal() {
    var gm = new GlideDialogWindow('showchangerequest');
    gm.setTitle('My UI page');
	gm.setSize(600,600); //Set the dialog size

    gm.setPreference('sysparm_changesysid', g_form.getValue('caused_by'));
	gm.setPreference('sysparm_changenumber', g_form.getDisplayBox('caused_by').value);
    gm.render();
}

Please enhance from your side.

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<html>
    <g:ui_form> 
        <g:evaluate var="jvar_changeSysId" expression="RP.getWindowProperties().get('sysparm_changesysid')" />
		<g:evaluate var="jvar_changenumber" expression="RP.getWindowProperties().get('sysparm_changenumber')" />
        <input type="hidden" id="changerequest1" name="changerequest1" value="${jvar_changeSysId}"></input>
        <div id='mydiv3'>
            <label id='label3'>Change Request</label>
			
			<g:ui_reference name="changerequest" id="changerequest" table="change_request" value="${jvar_changeSysId}" displayValue="${jvar_changenumber}" style="width:180px"/>

        </div>
    </g:ui_form>
</html>
</j:jelly>

Output:

set onload value reference field ui page.gif

I hope I have answered your question and you can enhance it further

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

10 REPLIES 10

Ankur Bawiskar
Tera Patron
Tera Patron

@bhaavani 

very difficult to help without script shared here.

there are lot of blogs for UI page which will help you.

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Hi Ankur,

 

I did it in my PDI and below is the code:

 

UI Action:

function openModal() {
    var causedbysysId = g_form.getValue('caused_by');
    console.log("Caused By Sys ID: " + causedbysysId);
	
    var dialog = new GlideDialogWindow("set_caused_by_modal");
    dialog.setTitle("Select Change Request");
    dialog.setSize(400, 200);
    dialog.setPreference('causedbysysId', causedbysysId);
    dialog.render();
	
}

 

 

UI Page HTML:

<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<g:include src="dialog_form.xml"/><script>
    var causedbysysId = g_form.getValue('caused_by');
    console.log("Received Caused By Sys ID: " + causedbysysId); // Add this line to check the value
</script>
<form id="modalForm">
<div>
<g:evaluate var="causedbysysId" /> 
Caused By Change :<br/>
<g:ui_reference label="Name" id ="causedBy" name="causedBy" table="change_request" field="name" value="${causedbysysId}" /> <br/>
</div>
<br/>

<button type="button" onclick="saveCausedBy()">Submit</button>
 <button type="button" onclick="GlideDialogWindow.get().destroy()">Cancel</button>
 </form>

 </j:jelly>
 

 

UI Page Client script:

function saveCausedBy() {
    var changeSysId = document.getElementById("causedBy").value;
	
    if (!changeSysId) {
        alert("Please enter a valid Change Request.");
    }
    var params = new URL(location.href).searchParams;
    var incidentSysId = params.get('sys_id');
      
    var ga = new GlideAjax('global.CBAIncidentAJAXUtils');
    ga.addParam('sysparm_name', 'saveCausedByChange');
    ga.addParam('sysparm_change_id', changeSysId);
    ga.addParam('sysparm_incident_id', incidentSysId);
	
    ga.getXMLAnswer(function(response) {
        
		if(response){
			g_form.addInfoMessage("Change Request is linked successfully");
		}
		else{
			g_form.addInfoMessage("Change Request is not linked");
		}
        
        GlideDialogWindow.get().destroy();
       // window.opener.location.reload(); // Ensure the parent page refreshes
		
    });
}
// Ensure the reference field is set if causedBySysId is present
window.onload = function() {
    var causedBySysId = "${causedBySysId}";
    if (causedBySysId) {
        document.getElementById("causedBy").value = causedBySysId;
    }
};

 

Script Include:

 

saveCausedByChange: function() {  
        var changeSysId = this.getParameter("sysparm_change_id");
        var incidentSysId = this.getParameter("sysparm_incident_id");
        
       
        var gr = new GlideRecord("incident");
        if (gr.get(incidentSysId)) {
            gr.caused_by = changeSysId; // Assign the Change Request reference
            gr.update();
            return true;
        } else {
            
            return false;
        }
		
    },

 

I am not sure where I am incorrect its not working trying since 2 days

 

Thanks in advance

 

 

@bhaavani 

this worked for me. please enhance. I was able to apply query and user is able to select only that CHG

if that field is empty then user can select any CHG

UI Action:

function showModal() {
    var gm = new GlideDialogWindow('showchangerequest');
    gm.setTitle('My UI page');
    gm.setPreference('sysparm_changesysid', g_form.getValue('caused_by'));
    gm.render();
}

UI Page:

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<html>
<body onload="autopopulate()">
    <g:ui_form> 
        <g:evaluate var="jvar_changeSysId" expression="RP.getWindowProperties().get('sysparm_changesysid')" />
        <input type="hidden" id="changerequest1" name="changerequest1" value="${jvar_changeSysId}"></input>
        <div id='mydiv3'>
            <label id='label3'>Change Request</label>
			<j:if test="${jvar_changeSysId !=''}">
        <g:ui_reference name="changerequest" id="changerequest" table="change_request" query="sys_id=${jvar_changeSysId}" style="width:180px"/>
			</j:if>
			<j:if test="${jvar_changeSysId == ''}">
<g:ui_reference name="changerequest" id="changerequest" table="change_request" style="width:180px"/>
			</j:if>
        </div>
    </g:ui_form>
</body>
</html>
</j:jelly>

Output:

ui page set filter.gif

I hope I have answered your question and you can enhance it further

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Hi Ankur,

 

You are always helpful. However the ask is the number should auto-populate as on the incident caused by change is already available. If its not available then user should select the changes from reference icon. Something like this

 

bhaavani_0-1740742288363.png

 

The above example screenshot on dialog window CI autopopulated based on problems CI.

 

Thanks in advance