How do I determine the target_field from within a field_map script?

kevinharalson
Mega Expert

Within an inbound web service, I am using a field map source script to validate the value of a coalesce field. Answer is set to the validated value, but I need to dynamically identify the field name associated with "answer". Is there a method for determining the exact field on "target" that will receive the value I place in answer?

4 REPLIES 4

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

I am not sure I follow what you are trying to do, but I did want to offer that you don't have to use field maps at all.   You can check the run script checkbox on your transform map and then script out your mappings.



target.fieldname = source.fieldname



This may be better for you as it sounds like you need to figure out where the data needs to go.



Edit: You don't have to script out everything and can leave your other field maps in place.


venkatiyer1
Giga Guru

Hi Kevin,




When you do field map source script, you have a drop down on target section to choose which field you want your validated value to be inserted into.


answer = (function transformEntry(source) {


     


  // Add your code here, if the column name of incoming data is user then use source.user to get the value


  return ""; // return the value to be put into the target field




})(source);


kevinharalson
Mega Expert

I appreciate the responses, and although I do not want to get into the weeds, I think I need to clarify my need if possible.



I have defined an object containing multiple properties (dynamically created from query of sys_transform_entry). Each property corresponds to the name of a coalesced target field as defined in the field maps, and is an object containing properties.



                  var coalesceFields = {};



                  coaleceFields['field1'] = new Object();


                  coalesceFields['field1'].label = label from sys_dictionary for field1


                  coalesceFields['field1'].value = display value for field1's validated user data



                  coaleceFields['field2'] = new Object();


                  coaleceFields['field2'].label = label from sys_dictionary for the field2


                  coaleceFields['field2'].value = display value for field2's validated user data



Within the field map script, I set the value for the coalesceFields property that corresponds to the target field (field1, field2, etc). I can currently hard-code the property's value as illustrated above, but I would prefer to do it dynamically based on the target_field to be updated by answer.



                  coalesceFields[target.target_field for answer].label = column_label;


                  coalesceFields[target.target_field for answer].label = validated_value;


venkatiyer1
Giga Guru

Hi Kevin,



Without going into details as to why you would approach it this way, if I were to suggest an approach it would be to either see if you are getting map variable in the source script or current and try getting target_field value using glide query for the current map.