Script Include Inserting Empty Record

stephaniet
Kilo Expert

Hi everyone,

This is my first "big" scripting project, so please bear with me.   I'd really appreciate your help.

I am trying to have the system automatically create a record in my new table called 'u_assignment_record' that includes who an incident was assigned to, who it is assigned to now, and who changed, every time someone updates the assigned to field on an incident.   I have it mostly working...except for the part where the record that the script include creates is blank.

Now, I tried using the OOB Metric for Assigned To duration, but this does not give me what we need in the format we need it.

Here is what I have so far.

CLIENT SCRIPT:

find_real_file.png

I want to pass the system ID's for all the values (the 'sysparm_who' value was a desperate attempt to figure out what I'm doing wrong...still didn't work), so that I can run reports on related fields for the values.   If I enable the commented out section in both the client script above and the script include below, I get an alert showing all the information I need (sys_id's for all values).

SCRIPT INCLUDE:

find_real_file.png

When I disable the alert parts above and change the value for an incident, I get a record in the u_assignment_record table BUT it's blank...

find_real_file.png

<xml>

<u_assignment_record>

<sys_created_by>stephanie.taveras</sys_created_by>

<sys_created_on>2016-11-07 19:13:29</sys_created_on>

<sys_id>d885d1800f87a200e867ee68b1050e06</sys_id>

<sys_mod_count>0</sys_mod_count>

<sys_updated_by>stephanie.taveras</sys_updated_by>

<sys_updated_on>2016-11-07 19:13:29</sys_updated_on>

<u_assigned_by/>

<u_assigned_from/>

<u_assigned_to/>

<u_number/>

</u_assignment_record>

</xml>

As you can see from the XML above, all 4 parameters that I try to pass are blank on my table.   I have tried setting these fields as Reference type, as String and as Translated Text.   Same thing happens for each type.   I did comment out the parameter and had it create a record with a static value (i.e '1', '2', '3', '4') and those values did pass into my table.

I would really appreciate anyone's help with this!

1 ACCEPTED SOLUTION

OK.. I messed around a bit with the setup.   You have to have the getXML() function, even if you do nothing with it:



find_real_file.png



find_real_file.png


find_real_file.png


View solution in original post

9 REPLIES 9

Genius!!   Thank you so much.   I knew it had to be something simple!


Hadn't realized I left that in there...that was from an earlier attempt at something else that didn't work.   But I removed it and the record still gets created blank.


Use an after business rule to achieve it .



Condition: current.assigned_to.changes() && !current.assigned_to.nil()



var gr= new GlideRecord('u_assignment_record');


gr.initialize();


gr.u_number=current.number;


gr.was_assigned=previous.assigned_to;


gr.has_assigned=current.assigned_to;


gr.u_assigned_by=current.sys_updated_by;


gr.u_updated_on=current.sys_updated_on;


gr.insert();


Thank you!   I thought I needed a script include for a business rule as well (like I said, still new at this!).   But this worked!   I had to change the values a little bit to:



var gr = new GlideRecord('u_assignment_record');


  gr.initialize();


  gr.u_number = current.sys_id;


  gr.u_assigned_from = previous.assigned_to;


  gr.u_assigned_to = current.assigned_to;


  gr.u_assigned_by = gs.getUserID();


  gr.insert();



And I removed the second condition, since I do want to track if someone change the value to nothing, but other than that, it works perfectly!



Can you just answer one more question for me?   Just to make sure I understand...the reason I don't need a script include from a business rule is because it runs server side already, correct?   Would there be any reason I would ever call a script include from a business rule at all (in a different case, not this one)?


The answer is very simple. Script include is a reusable code.



For example: We have   3 business rules . You are performing some similar calculation based upon some values.


If you will write the same piece of code in 3 business rules the amount of the code will be more and you are going to maintain a lot.




If you will use a script include , you have a common function which is going to execute this calculations and you can invoke it from business rule with a single line of code.



The only thing we need to change the parameters if required.



for eg. new ScriptincludeName().MethodName(start_date,end_date);




Hope i clarified you!