How to get sys_id from a related list and populate it on a form

bammar
Kilo Sage
Kilo Sage

Hello. I have a Survey related List below my Incident and it shows the Survey sent out (survey_details) table. Can someone please advise how i can get the sys_id of the Survey that was sent out. Then take that sys id and populate it to a text field on the parent table.   I tired about 50 different things. Can somone advise here basically I need a sys id from a related talist that only has one item...

  var gr = new GlidRecord("task_survey");

  //var result;

finding the Survey thats attached to this incident

  gr.addQuery("task.sys_id", parent.sys_id);

  gr.query();

  while(gr.next()) {

//trying to fetch the sys id of the survey that was found

  var result = gr.sys_id;

//trying to populate a reference field I have that references the task survey table from Incident

  parent.u_survey = result;

//trying instead to populate a plain text field with the sys_id

  parent.u_survey_sysid = result;

7 REPLIES 7

Robert Beeman
Kilo Sage

Where and when are you running this script? And on what table? Is this an on insert Business Rule to the survey_details table?


Ok so here is the deal. I have a request to Send the Survey BOTH on Resolved and Closed ( putting aside the why - it is just something I must do) . So what I did is I created a relationship between Task Survey and Incident.   When you resolve an incident I got the Survey to go out just fine and when you load the resolved incident you see the related list and you see the Survey.   My plan is when the incident is closed, i want a BR on the incident table to check the related list, get that Survey ID and populate a hidden field I have- i want the string of the sys_id of the Survey.   Then I will in the Close notification use the sys id of the Survey to build the URl to the SAME exact Survey they were asked to do in Resolved. If they tok it the first time, they will get a message that they had already done it, if not they can do the Survey. It is important it is the exact same survey link.



Long Story Short without understanding the above it boils down to - how do you look at the Task Survey table in a BR off the incident and find the Survey that corresponds to the Incident you have, then get that Surveys sys ID.   There is a field in Task Survey called "task" that has the incident number. It sounds easy but for some reason i tried every id and cant get anything to work. I would be happy if i could just get the sys id to write to my field in the incident table. Thank yu for replying and Thank you in advance for any insight.   Even if you could answer how do you code looking at a related list wit one item in it and getting that one items sys id that would be awesome.


This might be a lot simpler than you are thinking. If you run the BR against the incident table, then it's record is in the current object.



Business Rule


Table: Incident



When: Before Insert and Update



Advanced


Condition:


current.state == 7



Script:


function onBefore(current, previous) {   //This function will be automatically called when this rule is processed.


        var gr = new GlideRecord("task_survey");


        gr.get("task", current.sys_id); // Get survey record related to current incident


        current.u_survey = gr.sys_id; // Set survey record's sys_id to current record's u_survey field


}


Hello Robert. Thank you so much this worked! I didnt know the correct syntax and the gr.get("task", current.sys_id); was the key here. Thank you so much I will definitly apply this knowledge elsewhere going forward. Trying to find where i can mark your answer as the Resolving answer but I cant see it in this new Interface.