New Button on Embedded List

ggg
Giga Guru

I want to add a New button on an embedded related list.

I want the new button vice double clicking on the row to insert because I have validations on the form that i want to trigger.

I found this post to add a ui macro below the embedded list but i cannot get it to work:

https://community.servicenow.com/community?id=community_question&sys_id=40824321dbd8dbc01dcaf3231f9619c9

has anyone done this?

1 ACCEPTED SOLUTION

Chuck Tomasi
Tera Patron

If you really want a New button and a form so you can validate, change it from an embedded list to a related list.

The key to know about the embedded lists is that they act as part of the form. Even if you enter information, it's not saved until the form it is embedded on is saved. It's a quick way to enter lots of new records (e.g. answers to a question for example). It doesn't sound like you need the embedded list functionality and really want to see that full form for the child record so you can do some validation - I recommend changing to a related list. Just remove the embedded list from the form and add the related list.

You could try validating your embedded list fields based on an after BR on the parent record being created/updated or on the embedded list record being created/updated, but this feels like a hack to me.

 

Related lists | ServiceNow Docs

View solution in original post

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

If user is trying to populate the values can you try to create before insert business rule to perform the validation

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

Chuck Tomasi
Tera Patron

If you really want a New button and a form so you can validate, change it from an embedded list to a related list.

The key to know about the embedded lists is that they act as part of the form. Even if you enter information, it's not saved until the form it is embedded on is saved. It's a quick way to enter lots of new records (e.g. answers to a question for example). It doesn't sound like you need the embedded list functionality and really want to see that full form for the child record so you can do some validation - I recommend changing to a related list. Just remove the embedded list from the form and add the related list.

You could try validating your embedded list fields based on an after BR on the parent record being created/updated or on the embedded list record being created/updated, but this feels like a hack to me.

 

Related lists | ServiceNow Docs

Hi Chuck,

I am new to servicenow, i was working on this same scenario, the problem is that RelatedList dont show up when the parent form loads, one has to save the parent form record and then only the related list of that parent form is visible.

 

Is there a way to load the related list with the parent form at the start and can the parent and this child relatedlist be submitted in one go?

 

 

athm
Tera Guru

Hi ggg,

I am not sure about the above post, but  I have used the below to achieve a similar requirement which would add a button 'Add Row' on top of the Embedded List and a drop down next to the button to insert multiple rows at the same time, something similar to below screen shot.

find_real_file.png

 

My use case may slightly differ from yours in the sense I am not doing any validations but instead inserting rows with some calculated values based on some field values on the parent form. I basically created a UI Formatter and added it to the form right above the embedded list.

Below is the script inside the macro used in the formatter

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">	
	<script>				
	var checkExist = setInterval(function() {
		if ($j("table[list_name='parent_table_name.embedded_list_table_name.parent_reference_field_name'] tr.list_add").length) {		
		    $j("table[list_name='parent_table_name.embedded_list_table_name.parent_reference_field_name'] tr.list_add").hide();
			clearInterval(checkExist);
		}
	}, 1000); 

	function insertRows(){	
		if(!g_form.isNewRecord()){
                        //Basically do your server side thing here, like inserting rows into the emebdded list with pre-set values or whatever your use case is
			//var ga = new GlideAjax('scriptIncludeName');
			//ga.addParam('sysparm_name','functionNameInScriptInclude');
			//ga.addParam('parm1',g_form.getUniqueValue());		
			//ga.addParam('rows',gel('rowselector').value);
			//ga.getXML(checkRowsInserted);	
		}
		else{
		//If record is a new record - prompt the user to save the record
			alert('Record must be saved before adding New Rows.');
		}
	}		

	function checkRowsInserted(response){
		var resp = response.responseXML.documentElement.getAttribute("answer");
		if(resp == "rowsInserted"){
			try {
				var elementLookup = $$('div.tabs2_list');
				var listName = "Embedded List Name Here";
				for (i = 0; i != elementLookup.length; i++) {
					if (elementLookup[i].innerHTML.indexOf(listName) != -1) {
						var listHTML = elementLookup[i].id.split('_');
						var listID = listHTML[0];
						nowRefresh(listID);
					}
				}
			}
			catch (err) {
				//alert(err.message);
			}
		}			
	}
		
	function nowRefresh(id) {
		//Refresh the EmbeddedList
		GlideList2.get(id).refresh('');

		//Hide the Add Row Functionality after inserting row
		var checkExist = setInterval(function() {
			if  ($j("table[list_name='parent_table_name.embedded_list_table_name.parent_reference_field_name'] tr.list_add").length) {		            
				 $j("table[list_name='parent_table_name.embedded_list_table_name.parent_reference_field_name'] tr.list_add").hide();		
				clearInterval(checkExist);
			}		     
		}, 1000); 			
	}

	</script>	


	<style>
		#rowselector{
		background-color: #fff;
		background-image: none;
		border: 1px solid #bdc0c4;
		border-radius: 3px;
		height:25px;
		width: 75px;
		}	

		#insertechrow{
		box-sizing: border-box;
		font: inherit;
		overflow: visible;
		text-transform: none;
		font-family: inherit;
		font-size: 13px;
		line-height: 1.42857;
		font-weight: normal;
		text-align: center;
		vertical-align: middle;
		cursor: pointer;
		background-image: none;
		border: 1px solid transparent;
		white-space: nowrap;
		display: inline-block;
		color: #fff;
		background-color: #278efc;
		border-color: #0368d4;
		padding: 2px 4px;
		margin: 1px 4px;
		border-radius: 3px;
		}		
	</style>	

	<table width="100%">	
		<!-- 		 -->
		<tr class="list_row list_even embedded list_row_compact">
			<td>			
				<span>
					<input id="insertechrow" type="button" value="AddRows" onClick="insertRows()"/>
					<select id="rowselector">
						<option value="1">1</option>
						<option value="2">2</option>
						<option value="3">3</option>
						<option value="4">4</option>
						<option value="5">5</option>
						<option value="6">6</option>
						<option value="7">7</option>
						<option value="8">8</option>
						<option value="9">9</option>
						<option value="10">10</option>
					</select> 
				</span>
			</td>
		</tr>
	</table>

</j:jelly>

 

I hope this helps you ggg.