The CreatorCon Call for Content is officially open! Get started here.

Field mapping script for reference field.

SurapureddyD
Tera Contributor

I need to have source script for reference field for company and location fields in user table to load data.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@SurapureddyD 

something like this for company, ensure "Referenced value field name" -> sys_id

answer = (function transformEntry(source) {

	// Add your code here
	var incomingValue = source.u_companyname;
	var gr = new GlideRecord("core_company");
	gr.addQuery("name", incomingValue);
	gr.query();
	if (gr.next()) {
		return gr.getUniqueValue();
	}
	else{
		return -1; // don't allow update
	}

})(source);

AnkurBawiskar_0-1743065131951.png

 

something like this for location, ensure "Referenced value field name" -> sys_id

answer = (function transformEntry(source) {

	// Add your code here
	var incomingValue = source.u_locationname;
	var gr = new GlideRecord("cmn_location");
	gr.addQuery("name", incomingValue);
	gr.query();
	if (gr.next()) {
		return gr.getUniqueValue();
	}
	else{
		return -1; // don't allow update
	}

})(source);

AnkurBawiskar_1-1743065165778.png

 

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

2 REPLIES 2

Saiganeshraja
Kilo Sage
function getID(ID,table){
// returns the sys_id, Company: core_company, location: cmn_location
var companyid= new GlideRecord(table);
companyid.addQuery("sys_id",ID);
companyid.query();
if(companyid.next())
{
return companyid.sys_id;

}
return ;
}

// source.your import set field for getting the importset value

 

 

 

Mark correct and helpful.

Ankur Bawiskar
Tera Patron
Tera Patron

@SurapureddyD 

something like this for company, ensure "Referenced value field name" -> sys_id

answer = (function transformEntry(source) {

	// Add your code here
	var incomingValue = source.u_companyname;
	var gr = new GlideRecord("core_company");
	gr.addQuery("name", incomingValue);
	gr.query();
	if (gr.next()) {
		return gr.getUniqueValue();
	}
	else{
		return -1; // don't allow update
	}

})(source);

AnkurBawiskar_0-1743065131951.png

 

something like this for location, ensure "Referenced value field name" -> sys_id

answer = (function transformEntry(source) {

	// Add your code here
	var incomingValue = source.u_locationname;
	var gr = new GlideRecord("cmn_location");
	gr.addQuery("name", incomingValue);
	gr.query();
	if (gr.next()) {
		return gr.getUniqueValue();
	}
	else{
		return -1; // don't allow update
	}

})(source);

AnkurBawiskar_1-1743065165778.png

 

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