- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-24-2022 02:07 AM
Hello
I have a task to fill field "short_description" (on every incident, where short_description is empty) on "Incident" table with "producer" field name from table "sc_item_produced_record", by using background script, but im new in scripting and i stucked.
Short description from incident must be filled with this field
I started like this, but i have no clue how to go on
Please, assist.
Regards
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-24-2022 02:34 AM
Hello @Miko_aj Buczek ,
So current is an object which you can use when you are writing the same script in business rule .
But in your case it is background script so current object is not accessible here so what you can do is for testing purpose copy the sys_id of a sample incident record like below
1)Go to incident table and search for your incident record and do a right click on the incident number and click on copy sys_id(see below screenshot)
Then come to your back ground script and paste your incident sys_id in the place of current.sys_id;
var gr = new GlideRecord('incident');
gr.addEncodedQuery('short_descriptionISEMPTY');
gr.addQuery('sys_id','your_incident_sys_id');
gr.query();
if(gr.next())
{
var pr = new GlideRecord('sc_item_produced_record');
pr.addQuery('task', gr.sys_id);
pr.query();
if(pr.next()) {
gr.short_description = pr.producer.name.toString();
gr.update();
}
}
HOPE THIS HELPS
Mark my answer correct if this helps you
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-24-2022 02:20 AM - edited 11-24-2022 02:26 AM
Hi @Miko_aj Buczek ,
You are trying this in a background script, hence current.sys_id will not give anything. Try this:
var gr = new GlideRecord('incident');
gr.addEncodedQuery('short_descriptionISEMPTY');
gr.query();
while(gr.next())
{
var prod = new GlideRecord('sc_item_produced_record');
prod.addQuery('task', gr.sys_id);
prod.query();
if(prod.next()) {
gr.short_description = prod.producer.name;
gr.update();
}
}
Please mark my response as helpful/correct if this helps
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-24-2022 02:24 AM
Hi @Miko_aj Buczek ,
Please try the below code,
var gr=new GlideRecord('incident');
gr.addEncodedQuery('short_descriptionISEMPTY');
gr.query();
while(gr.next()){
var prod=new GlideRecord('sc_item_produced_record');
prod.addQuery('task',gr.sys_id);
prod.query();
if(prod.next()){
gr.short_description=prod.producer.name;
gr.update();
}
}
Mark this as helpful if this helps.
Regards,
Jhansi.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-24-2022 02:28 AM
Hi @Miko_aj Buczek,
Try this updated scripts -
var sagar = new GlideRecord("incident");
sagar.addQuery("short_descriptionISEMPTY");
sagar.query();
gs.info(sagar.getRowCount());
while (sagar.next()) {
// gs.info(sagar.number);
var pagar = new GlideRecord("sc_item_produced_record");
pagar.addQuery("task.sys_id", sagar.getUniqueValue());
pagar.query();
// gs.info(pagar.getRowCount());
if (pagar.next()) {
// gs.info(pagar.task.number);
sagar.short_description = pagar.short_description;
sagar.setWorkflow(false); // disable all business rules during this script execution
sagar.autoSysFields(false); // disable updating system fields.
sagar.update();
}
}
Thanks,
Sagar Pagar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-24-2022 02:31 AM
try this optimized code
1) always wrap code inside function
2) always use try catch block for exception handling
(function(){
try{
var gr=new GlideRecord('incident');
gr.addEncodedQuery('short_descriptionISEMPTY');
gr.query();
while(gr.next()){
var prod = new GlideRecord('sc_item_produced_record');
if(prod.get('task',gr.getUniqueValue())){
gr.short_description = prd.producer.name;
gr.setWorkflow(false); // to avoid BR from triggering
gr.updateMultiple();
}
}
}
catch(ex){
gs.info(ex);
}
})();
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader