Script Background - GS.PRINT - value from databse
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-25-2013 12:40 AM
Hello,
Before I write the text here I want to thank you to all who spend time to read and reply on this, Thank You.
(I had opened same topic some time ago - name was""" script background - gs.print how to get the result to excell ? "" but there were not respond's I expected)
Now I have found one way I want to Share with you all and Also want to find if some of You does know any other way how to achieve the below.
I need get data from database. I have script in Script's-Background. This script give me result's back.
To be able take those data to excell file, You must write SEMICOLON in gs.print before every value I want get.
Like this --- gs.print(";"+ c.u_code_of_mill +";"+ c.sys_id +";"+ c.u_country_code);
(without semicolon i cannot get value's in separate column later in excel or CSV)
After I get result on the screen, I copy the data from screen to Notepad and save it as CSV file to be able have every value in separate column. So here I finally achieve that I have everey values I asked for in, in separate column in CSV or excel.
Now My question is if there is any other way how to achieve this ?
Please note, that I cannot use reporting as I need get back also SYS_ID value which is not displayed on the form where I take a data from table, so reporting is not a solution here.
Any Tip's or tric's ?
Petr
Script Background:
var c = new GlideRecord('u_mill_code');
c.addQuery('u_code_of_mill');
c.orderBy('u_mill_code');
c.query();
while (c.next()) {
c.autoSysFields(false); // Do not update sys_updated_on, sys_updated_by, and sys_mod_count
c.setWorkflow(false); // Do not run any other business rules
gs.print(";"+ c.u_code_of_mill +";"+ c.sys_id +";"+ c.u_country_code);
//gs.print(c.sys_id);
}
Result in CSV file here:
Code SYS ID Country
AT_160258162d4b9fec100438058bb8299c0a4Austria
AT_161218162d4b9fec100438058bb8299c0a5Austria
AT_162e18162d4b9fec100438058bb8299c0a5Austria
AT_912a58162d4b9fec100438058bb8299c0a5Austria
AU_600698162d4b9fec100438058bb8299c0a5Australia
AU_9172d8162d4b9fec100438058bb8299c0a5Australia
BE_130ed8162d4b9fec100438058bb8299c0a5Belgium
BE_225a18162d4b9fec100438058bb8299c0a6Belgium
BE_443658162d4b9fec100438058bb8299c0a6Belgium
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2013 06:28 PM
Another way would be to generate an attachment with your CSV data in it, then show a link with gs.print.
This worked for me:
var blankInc = new GlideRecord("ecc_queue");
blankInc.insert();
var value = "test"
var attachment = new Attachment();
var attachmentRec = attachment.write("ecc_queue", blankInc.sys_id.toString(), "import.txt", "text/plan", value);
var attachRec = new GlideRecord("sys_attachment");
attachRec.addQuery("table_sys_id", blankInc.sys_id.toString());
attachRec.addQuery("file_name", "import.txt");
attachRec.query();
attachRec.next();
var url = "sys_attachment.do?sys_id="+attachRec.sys_id.toString()+"&view=true";
gs.print("<a href='" + url + "'> here</a>");
blankInc.deleteRecord();
Obviously you would want to use a generic table if you could, to create then delete a record.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-29-2013 09:39 PM
Hello Chris,
thank you for your script.
Unfortunatelly it does not work on for me so far as it work for you. And I have not found yet why. Maybe I do something wrong ?
Only what I get is below result.
here
Petr
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-29-2013 10:14 PM
Petr,
Did you fill the variable "value" with your text? (The comma delimited text you had generated). Then you would just need to click on the link that us generated to open/download the file. I copied the link you posted and it looks like it did generate a sys_id so there should be a new attachment from the script you ran in the sys_attachment table.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-29-2013 11:26 PM
Chris, I have it now.
I have removed that last row to delete record.(I wanted to try if atatched file will be on new incident record - this is fine and work perfect)
Now i have NEW incident and attachment on it.
In attachment i have text "Petr Test" as I have provided that in Variable value.
Chris, how do I get my data(result from my original script I provided at the beggining of this topic) to that new attachment ?
shall I somehow declare below instead of ---- var value = "Petr Test" ?
var value = gs.print(";"+ c.u_code_of_mill +";"+ c.sys_id +";"+ c.u_country_code);
I tried it like below, but content in attachment is empty()
Please help me little how i can get the content to the attached file ...
Thank you
Petr
var c = new GlideRecord('u_mill_code');
c.addQuery('u_code_of_mill');
c.orderBy('u_mill_code');
c.query();
while (c.next()) {
c.autoSysFields(false); // Do not update sys_updated_on, sys_updated_by, and sys_mod_count
c.setWorkflow(false); // Do not run any other business rules
}
var blankInc = new GlideRecord('incident');
blankInc.insert();
var value = gs.print(";"+ c.u_code_of_mill +";"+ c.sys_id +";"+ c.u_country_code);
var attachment = new Attachment();
var attachmentRec = attachment.write("incident", blankInc.sys_id.toString(), "import.txt", "text/plan", value);
var attachRec = new GlideRecord("sys_attachment");
attachRec.addQuery("table_sys_id", blankInc.sys_id.toString());
attachRec.addQuery("file_name", "import.txt");
gs.print(";"+ c.u_code_of_mill +";"+ c.sys_id +";"+ c.u_country_code);
attachRec.query();
attachRec.next();
var url = "sys_attachment.do?sys_id="+attachRec.sys_id.toString()+"&view=true";
gs.print(" here");
