Defaulting to a Related List field

zerofidelis1
Tera Contributor

Hello All,

 

I have what I assume is a very simple action that I can't seem to figure out and I was hoping you all might be able to provide some help.

 

I have a field "Field A" that is a reference to the sys_user table on my Change Request form.  I am trying to default the value of that field to the Requestor of the Incident that this Change was created from.  The Incident has a link to the Change via the rfc field in the Incident table and the Change has a link to the Incident via the related list "Incidents Pending Change", but I can't find a working method of pulling.  In addition, this should only be defaulted one time, when the Change is first inserted from the Incident, as I need people to be able to populate that field manually and not have it revert.

 

I thought I had a script that would work applied to the Change table, but running it as either a business rule on Insert, or a Client Script on Submit, it doesn't appear to do anything.  Does anyone have an approach from this one?  Or an idea of where my script is breaking down as I can't seem to get the debugger to work?

 

(function executeRule(current, previous /*null when async*/) {
 
var currentChange = g_form.getValue('number');
 
    // Query the Incident table to find the Incident related to this Change Request
    var incidentGR = new GlideRecord('incident');
    incidentGR.addQuery('rfc', currentChange); 
    incidentGR.query();
 
    if (incidentGR.next()) {
        // Get the Requestor value from the Incident record
        var requestor = incidentGR.caller_id;
 
        // Set the Business Requestor field in the current Change Request form
        g_form.setValue('Field A', requestor);
    }
 
})(current, previous);
1 ACCEPTED SOLUTION

DrewW
Mega Sage
Mega Sage

That looks like code for a BR, which would be Server side and g_form is not a server side object.  Use

var currentChange = current.getValue('number');
and

current.setValue("field_a_name", requestor);

 

In place of the appropriate lines of code.

 

But you also have a chicken and egg thing going on.  The incident cannot but updated until the change is inserted but the change is querying for the incident before its updated.  So you going to have to update the code that is creating the change in the first place to set "Field A" before the change is inserted.  I'm assuming you are using a UI Action of some type to cause the change to be created and that is where you need to set "Field A".

 

View solution in original post

2 REPLIES 2

DrewW
Mega Sage
Mega Sage

That looks like code for a BR, which would be Server side and g_form is not a server side object.  Use

var currentChange = current.getValue('number');
and

current.setValue("field_a_name", requestor);

 

In place of the appropriate lines of code.

 

But you also have a chicken and egg thing going on.  The incident cannot but updated until the change is inserted but the change is querying for the incident before its updated.  So you going to have to update the code that is creating the change in the first place to set "Field A" before the change is inserted.  I'm assuming you are using a UI Action of some type to cause the change to be created and that is where you need to set "Field A".

 

You hit the nail on the head,  the code adjustment makes sense, and may have worked, but it was much easier to just populate that field from the Incident as part of the OOB Create Normal Change UI Action.  Thanks for getting me on the right track!