Passing Variables to Task "Description" Field

ahsan4
Giga Contributor

Hello All,

I hope you are having a wonderful Monday! I needed to reach out to the community for some assistance and see if anyone can help me figure my problem out.   I need to take all the variables and pass them in to the "Description" field in the task (in the workflow).   I need to make sure make sure they display in the "description" field in a certain order, skip anything that is null, and each variable is in a new line, etc.   The variables are all types (reference, integers, check box, string, etc.) This item has about 35/40 variables but I will provide how I have coded a few of them below.   I am getting syntax errors and also the code is not really working.

1) is there a better way that what I have present below?

2) Can you please help me perfect my code below and explain what I might be doing wrong?

Example of my code (have to code this for about 38 variables due to them being displayed in a certain order):

//create a variable that will be appended to as you check each request variable for a value

var new_description;  

var event_name = current.variables.event_name;

var event_date_start = current.variables.event_date_start;

var event_time_start = current.variables.event_time_start;

var requested_by = current.variables.requested_by;

if (null !=event_name) {        

new_description = (new_description && "Name of Event: " && event_name.toString());

}

if (null !=event_date_start) {      

new_description = \n(new_description && "Date Event Will Begin: " && event_date_start.toString());

}

if (null !=event_time_start) {      

new_description = \n(new_description && "Daily Start Time: " && event_time_start.toString());

}

if (null !=requested_by) {        

new_description = \n(new_description && "Requested By: " && requested_by.toString());

}

current.description = new_description;

***not passing the actual variables over because the page looks too busy for our customer and is overwhelming. that would have taken seconds to do but they wanted me to pass the value into the description field for their full-fillers to be able to read. ***

1 ACCEPTED SOLUTION

Community Alums
Not applicable

I just did a similar type of thing in a workflow, creating a task with the summary of the variables in the description field:



  var gr = new GlideRecord("sc_req_item");


  gr.addQuery("request", current.request.sys_id);


  gr.query();


  while(gr.next()) {


      // Get Owned Variables for Requested Item and sort by Order


      var ownvar = new GlideRecord('sc_item_option_mtom');


      ownvar.addQuery('request_item.number', gr.number);


      ownvar.addQuery('sc_item_option.value','!=','');


      ownvar.orderBy('sc_item_option.order');


      ownvar.query();


  var items = "Summary of " + gr.number + ":   " + gr.cat_item.getDisplayValue() + "\n\n";  


      while(ownvar.next()) {


          var field = ownvar.sc_item_option.item_option_new;


                        var fieldValue = ownvar.sc_item_option.item_option_new.name;


                        // Print variable name


            items += field.getDisplayValue() + ": " + gr.variables[fieldValue].getDisplayValue() + "\n";


        }


  }


task.description = items;



Try that and see if it works for what you need. It does skip null variables and does an 'OrderBy' based on the Order number of the variable. It ends up looking something like this:



++++++



Summary of RITM0016419:   Unitize Rental/RPO Unit



Equipment Number: 0987


ETM Order Number: 0987


Tag Number: 05-698


Is this an asset?: No


Characteristic Value: Test


Characteristic Name: Test


Equipment Description: Test


Brand Name: Test


Model Number: Test


Model Year: Test


Serial Number: Test



++++++



Hope that helps!



Cheers,



Tim


View solution in original post

41 REPLIES 41

Yeah, I am trying that but for some reason, the reference is pulling in the sys id value.   Below is what I did as a test :



var new_description;  


var event_name = current.variables.event_name;


var event_date_start = current.variables.event_date_start;


var event_time_start = current.variables.event_time_start;


var requested_by = current.variables.requested_by.toString();





  if (current.variables.event_name != '') {


  new_description += "Name of Event: " + current.variables.event_name + "\n";


  }


  if (current.variables.event_date_start != '') {


  new_description += "Date Event Will Begin: " + current.variables.event_date_start + "\n";


  }


  if (current.variables.event_time_start != '') {


  new_description += "Daily Start Time: " + current.variables.event_time_start + "\n";


  }


  if (current.variables.requested_by != '') {


  new_description += "Requested By: " + current.variables.requested_by + "\n";


  }


  task.description = new_description;



Results: it duplicates it as well.



Name of Event: test


Date Event Will Begin: 2016-05-10


Daily Start Time: 08:00:00


Requested By: 9f05a7d06f2ed2008438204fae3ee4f5


Name of Event: test


Date Event Will Begin: 2016-05-10


Daily Start Time: 08:00:00


Requested By: 9f05a7d06f2ed2008438204fae3ee4f5


Hi Tim,



I am still stumped but have determined that the script is not making it to Step #4 and on.   I get the right count of variables with Step #3, then nothing more.   I am getting a "" around the same time but not finding anything on the web that seems to correlate.   Is your script still working?   We are on the latest Helsinki patch.



gs.info("Workflow - Step #1");


var gr = new GlideRecord("sc_req_item");


gr.addQuery("request", current.request.sys_id);


gr.query();


while(gr.next()) {


// Get Owned Variables for Requested Item and sort by Order


var ownvar = new GlideRecord('sc_item_option_mtom');


ownvar.addQuery('request_item.number', gr.number);


ownvar.addQuery('sc_item_option.value','!=','');


ownvar.orderBy('sc_item_option.order');


ownvar.query();


var items = "Summary of " + gr.number + ":   " + gr.cat_item.getDisplayValue() + "\n\n";


gs.info("Workflow - Step #2");


while(ownvar.next()) {


var field = ownvar.sc_item_option.item_option_new;


var fieldValue = ownvar.sc_item_option.item_option_new.name;


// Print variable name


items += field.getDisplayValue() + ": " + gr.variables[fieldValue].getDisplayValue() + "\n";


gs.info("Workflow - Step #3");


}


gs.info("Workflow - Step #4");


}


gs.info("Workflow - Step #5");


task.description = items;


gs.info("Workflow - Step #6" + items);



Script Logs.jpg


Thanks,


-Wesley


Community Alums
Not applicable

That looks OK to me. Do you know what it was that changed so the script is no longer working? As in was a patch applied and since then it's not working?



I usually use 'Scripts -> Background' or a fix script to test queries to ensure they are working properly. I would suggest doing that to first ensure you are getting the results you are expecting. Switch out 'current.request.sys_id' for an actualy sys_id of a REQ record.



Cheers,



Tim


Hi Tim,



No changes we can think of, but we have gone through a few SN patches, but not able to determine when it stopped working.   I tried your suggestion, but must not be doing it correctly because I am getting a null error on line #3.   Can you advise, since, I still week at JavaScript syntax?



gs.info("Workflow - Step #1");


var gr = new GlideRecord("sc_req_item");


gr.addQuery("request", 40276bd90fb84300b855f08ce1050e15);


gr.query();



Thanks,


-Wesley


Community Alums
Not applicable

Your sys_id will need to be in quotes:



gr.addQuery('request', '40276bd90fb84300b855f08ce1050e15');



Tim