How to get current value for a referenced field?

runfast
Kilo Guru

I am trying to insert a record into a the relationship table if the parent child relationship does not exist on relationship table.

I get the parent.name from the current.value which is a text field, but the challenge I am having is that getting the child.name from current.configuration_item which is a reference field.

Below script returns 0 value for the relationships variable even though there are records that matches current.value and current.configuration_item. 

var gr= new GlideAggregate('cmdb_rel_ci');
  
  gr.addQuery('parent.name',current.value); 
  gr.addQuery('child.name',current.configuration_item);
  gr.addAggregate('COUNT');
  gs.log('parent: ' + current.value); 
  gs.log('child: '  + current.configuration_item); 
  gr.query();	
  var relationships = 0;
  if (gr.next()) {
   relationships = gr.getAggregate('COUNT');
   gs.log("Records in relationship table: " + relationships );
	  current.setAbortAction(true);
  }
	else
		
   {
	gr.initialize();
	gr.parent.setDisplayValue(current.value);
	gr.child.setDisplayValue(current.configuration_item);
	gr.insert();
	
	}

 

can someone please shade some light on how to get the value for current.configuration_item?

3 REPLIES 3

Niclas
Giga Guru

Try to replace:

gr.child.setDisplayValue(current.configuration_item);

with:

gr.setValue("child", current.getValue("configuration_item"));

 

Explanation:

  • If current.configuration_item is a reference field, current.configuration_item usually returns a sys_id.
  • However you have used setDisplayValue, which sets gr.child based on a display value but there is no CI with a display value containing a sys_id, thus this might cause your issue that the field is not set properly
  • setValue will set a reference field using a sys_id, and getValue on a reference field returns the sys_id

 

Niclas
Giga Guru

If current.configuration_item is a reference field, current.configuration_item returns the sys_id, not the name. In your query it would be best practice to build the query against the sys_id instead of the name, as the name might not be unique and you may end up in getting the wrong record.

Try to replace:

gr.addQuery('child.name',current.configuration_item);
[...]
gr.child.setDisplayValue(current.configuration_item);

with:

gr.addQuery('child', current.getValue('configuration_item'));
[...]
gr.setValue('child', current.getValue('configuration_item'));

 

If you need to get the name of a reference field you have two options:

  • If you know that name is the display value (which it is out of the box), you can use current.configuration_item.getDisplayValue()
  • If name is not the display value, you can dot walk to it with current.configuration_item.name 

siva_
Giga Guru

 I want to ask you to check three things : 

1) Do you really have a value field on the current object where you have used 

gr.addQuery('parent.name',current.value);

2) Are you getting the expected values in your logs or not if not you need to first correct them?

gs.log('parent: ' + current.value);

gs.log('child: ' + current.configuration_item);

3)You have referenced your gr as a Glide Aggregate object here , and you have used initialize() . There is no initialize() on the GlideAggregate object . Its only there on Glide Record , so modify your code to use GlideRecord and remove aggregate functions from it. 

 

Hope that helps 

 

Thanks,

Siva

Mark this response as correct if that really helps