g_form in ui macro

davidgustafsson
Tera Expert

Trying to add a UI macro on incident table that will, when you push related button, add an instance to a related list. This works fine but in lists with incidents I get an error where an invisible square is placed above the list making it impossible to interact with the lists components.

According to the error messages and ServiceNow support I should blame this UI macro and more specifically an illegal use of g_form. I guess that it is in the "<span id=" row. that the issue is and that you can´t use g_form there. The question is then how to rewrite it not using g_form?

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
	<g:evaluate object="true" var="company_already_there" jelly='true'>
		var answer = true;
		
		if(new AffectedCompanyExists().checkExistsAffectedCompanyList(current.company))
			 answer = false;
		
		<!-- if(!gs.hasRole("itil"))
		      answer = false; -->
		
		answer;
	</g:evaluate>  
<!--	<j:if test="${company_already_there}"> -->
	 <j:set var="jvar_n" value="add_caller_to_affected_${ref}"/>
	<span id="${jvar_n}" onclick="createAffectedCompany(g_form.getUniqueValue())" title="Add caller company to affected" alt="Add Caller company to affected" tabindex="0" class="btn btn-default icon-user-add">
      <span class="sr-only">Add Caller company</span>
	</span>
		 
	
<script>
	
	
createAffectedCompany();
//

function createAffectedCompany(current_sys_id) {
	var comp = g_form.getReference('company',get_company);	
}
	
function get_company(comp){
	var callerCompany = comp.name;
	if(!comp)
		return false;
	
	//Verify if callers company is already in list of affected companies in order to not get duplicates
	var existingAffComp = new GlideRecord("u_m2m_incidents_companies");
		existingAffComp.addQuery('u_company', comp.sys_id );
		existingAffComp.addQuery('u_incident', g_form.getUniqueValue() );
	    existingAffComp.query(queryAnswer);
	
	function queryAnswer(existingAffComp) {	
		if (existingAffComp.next())
			return false;	
	
		//Add callers company to list of affected company
		var aff_comp = new GlideRecord("u_m2m_incidents_companies");
		aff_comp.u_incident = g_form.getUniqueValue();
		aff_comp.u_company =  comp.sys_id;    //  callerCompany;
		var aff_comp_sysID = aff_comp.insert();	

		var affectedCompanies = GlideList2.getListsForTable('u_m2m_incidents_companies')[0];
		affectedCompanies.refresh();
	
		if($("add_caller_to_affected_incident.caller_id")){
			$("add_caller_to_affected_incident.caller_id").hide();
		}

		return true;
	}
}

	</script>
		
<!--</j:if>  -->
</j:jelly>
1 ACCEPTED SOLUTION

davidgustafsson
Tera Expert

I got the solution.

The issue was that I added the row:

createAffectedCompany();

This made the script load every time the page was loaded. As you load a preview of the incident page from the I symbol it tried to load the script as well. From list you do not have access to g_form why this throw an error.

View solution in original post

4 REPLIES 4

Varsha Jadhav1
Giga Expert

Hi davidgustafsson

I think g_form wouldn't work on ui macro.

Follow below approach;

 <h2 id ="message1"></h2>

 <h2 id ="message2"></h2>

Write an onload client script.

document.getElementById("message1").innerHTML = g_form.getElement('number');

document.getElementById("message2").innerHTML = g_form.getElement('type');

 

if your query has resolved please mark the answer correct or helpful.

davidgustafsson
Tera Expert

I got the solution.

The issue was that I added the row:

createAffectedCompany();

This made the script load every time the page was loaded. As you load a preview of the incident page from the I symbol it tried to load the script as well. From list you do not have access to g_form why this throw an error.

Mark Endsley
Tera Guru

You actually can use g_form in a UI Macro

https://www.youtube.com/watch?v=ivP3bl7r0Ws

Yes, that is correct that you can use g_form in UI macros. The issue is that I tried to use it from a list. As i was initiating it from list there are no specific form for an instance from the table to open the form from why g_form will not work.