How to parameterize a Script Include Glide Record Query

Jeff77
Tera Guru

Still pretty much a noob.....so appreciate any help/direction anyone can offer. 

This may have been answered somewhere but I haven't been able to find it (maybe its impossible).  What I'm trying to do is create a simple function in a Script Include that I can then use in catalog forms to return another value from a reference field.  Basically I want to pass the function the tablename, sysID of the record, and fieldname I want returned.  The function is simple enough....code here: 

    getFieldValBySysID: function() {

        var tableName = this.getParameter('sysparm_tableName');
        var record_sysID = this.getParameter('sysparm_record_sysID');
        var fieldName = this.getParameter('sysparm_fieldName');
        var tableRec = new GlideRecord(tableName);
        tableRec.get('sys_id', record_sysID);
       return tableRec.fieldName;

    }

It will query the table row just fine, and if I change the last row to explictly reference a field in the table (e.g. if it was passed the Incident table, and sys_parm_fieldName=caller, it will return the caller field value) it will work fine.  

Still new to javascript, so maybe this is not possible....but hoping someone has an idea that I can use to make this work, as I'd like to create 1 script I can use for re-use (rather than hard-code the field name).

Thanks in advance! 
Jeff 

 

 

1 ACCEPTED SOLUTION

Filipe Cruz
Kilo Sage
Kilo Sage

Hi Jeff,

Yes, you can do it.
Replace your statement: 

return tableRec.fieldName;

by:

return tableRec.getValue(fieldName);

 

This should do the trick for you!!

Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!

Best Regards,

Filipe Cruz

View solution in original post

4 REPLIES 4

Filipe Cruz
Kilo Sage
Kilo Sage

Hi Jeff,

Yes, you can do it.
Replace your statement: 

return tableRec.fieldName;

by:

return tableRec.getValue(fieldName);

 

This should do the trick for you!!

Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!

Best Regards,

Filipe Cruz

Thanks Filipe! 

OlaN
Giga Sage
Giga Sage

Hi,

What you have is a very good start, keep it up.

Some pointers:
Wrap the .get() method in an if statement, to make your code more robust, in case no record was found.
Similar, check that the field name provided actually exists by wrap it in an if-statement,
And as Filipe already mentioned, use .getValue() method when possible.

getFieldValBySysID: function() {

	var tableName = this.getParameter('sysparm_tableName');
	var record_sysID = this.getParameter('sysparm_record_sysID');
	var fieldName = this.getParameter('sysparm_fieldName');
	var tableRec = new GlideRecord(tableName);
	if (tableRec.get(record_sysID))
	{
		if (tableRec.isValidField(fieldName))
			return tableRec.getValue(fieldName);
		else
			return '';
	}
	else
		return '';
}

 

Thanks OlaN, definitely good additioin.....much appreciated!