- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-28-2024 04:56 AM
I am creating new problem record when incident created with BR. its working as expected. but the issue is when i resolve the incident the associated problem should be closed automatically. but its my code is not working as expected. below i am sharing my script and conditions for the reference. Please any help would be appreciated. T
After BR - Update , Table- Incident.
i have added infoMessages for testing and those are triggered correctly.
Thanks in advance
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-28-2024 07:08 AM - edited 04-28-2024 07:09 AM
Hi @Jaya Chandra Re
Use the below script but lets try to understand few things
1) We should use gr.addQuery("sys_id", current.problem_id) because you can see problem_id is present on the incident form and when you are querying problem table, you should compare the sys_id of problem with the sys_id present in problem_id
2)You can open the dictionary of state field in problem table. Go to the choices and bring the table field. Among all the choices that are closed, only 107 is a match for problem table. Please refer the attached screenshot
3) Once you have confirmed everything as mentioned above, use the below script. It will be after update BR and condition also remains same
var probRcd = new GlideRecord('problem');
probRcd.addActiveQuery();
probRcd.addQuery("sys_id", current.problem_id);
probRcd.query();
if (probRcd.next()) {
probRcd.state = '107';
probRcd.update();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-28-2024 06:02 AM
Hi @Jaya Chandra Re ,
You can only change the Problem state to closed if it is in resolved state.
Else you can not change the problem state.
If your problem state is resolved than you can use the below code
Create onBefore BR on Incident table and add below code
(function executeRule(current, previous /*null when async*/ ) {
// Add your code here
gs.log("Call BR Close Problem = " + current.state);
if (current.state == 7 || current.state == '7') {
gs.log("Inside if poblem = " + current.problem_id.state);
// current.setValue('problem_id.state', '107');
var gr = new GlideRecord('problem');
gr.addQuery('first_reported_by_task', current.sys_id);
gr.query();
if (gr.next()) {
gs.print("Number = " + gr.number);
gr.resolution_code = 'fix_applied';
gr.close_notes = "test";
gr.state = 106;
}
}
})(current, previous);
I'm adding image of BR for reference
Please mark my answer correct and helpful if this works for you
Thanks and Regards
Sarthak
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-28-2024 07:08 AM - edited 04-28-2024 07:09 AM
Hi @Jaya Chandra Re
Use the below script but lets try to understand few things
1) We should use gr.addQuery("sys_id", current.problem_id) because you can see problem_id is present on the incident form and when you are querying problem table, you should compare the sys_id of problem with the sys_id present in problem_id
2)You can open the dictionary of state field in problem table. Go to the choices and bring the table field. Among all the choices that are closed, only 107 is a match for problem table. Please refer the attached screenshot
3) Once you have confirmed everything as mentioned above, use the below script. It will be after update BR and condition also remains same
var probRcd = new GlideRecord('problem');
probRcd.addActiveQuery();
probRcd.addQuery("sys_id", current.problem_id);
probRcd.query();
if (probRcd.next()) {
probRcd.state = '107';
probRcd.update();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-28-2024 07:42 AM
Hello @ashutosh,
Thanks a lot for Explanation, its working.
i have one more query. as per 1st point you said --> if i want to close related incidents automatically when Problem got closed. So, the Query will be Prob.addQuery("problem_id",current.sys_id);
Is it right..?
i am facing confusion while writing addquery conditions between the tables. how to overcome this issue, what are the points we need to know when writing "addQuery". Please explain little bit more on this point. it could be helpful for me in future.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-28-2024 11:42 AM - edited 04-28-2024 11:51 AM
Hi @Jaya Chandra Re ,
1) You are right but first thing is you should name your variable a bit more specific as for above case, you can name the glide record as incRcds or incidentRcds etc so that it becomes incRcds.addQuery("problem_id",current.sys_id);
Note- I've used plural since multiple incidents can be closed
2) To help you with your confusion in the begining, you can try to build the query in respective table for example lets take your scenario :- if problem closes, close all the incidents associated with it
Follow following steps :-
a) Go to the respective table to which you are gliding(in this case, its the incident table)
b) Build your query in the filter condition(look at the picture). Then right click on the breadcrumb and copy the query.
c) Here you have 2 choices either you can use the encoded query or you can use add query. If you use encoded query, your code will look like incRcds.addEncodedQuery("problem_id=c92c2fd3b7b023002bc49a91ee11a9ce");
If you use addQuery, your code looks like incRcds.addQuery("problem_id", "c92c2fd3b7b023002bc49a91ee11a9ce");
What will help you from this is, you will come to know how the query looks like and with this you can make the above one as dynamic by adding current.sys_id which will look like incRcds.addQuery("problem_id", current.sys_id);
Please mark my solution as accepted or helpful to help the community better
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-29-2024 12:01 AM
Its giving me a clear picture on addQuery API. thanks a lot for this explanation. Really appreciated for your efforts and explanation.