The CreatorCon Call for Content is officially open! Get started here.

When Incident closed the associated problem should be closed automatically...?

Jaya Chandra Re
Tera Contributor

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.

JayaChandraRe_0-1714305094138.png

JayaChandraRe_1-1714305118101.png

i have added infoMessages for testing and those are triggered correctly.

 

JayaChandraRe_2-1714305156194.png

 Thanks in advance

 

1 ACCEPTED SOLUTION

Ashutosh C J
Tera Guru

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

AshutoshCJ_0-1714313183756.png

 

 

 

    var probRcd = new GlideRecord('problem');
    probRcd.addActiveQuery();
    probRcd.addQuery("sys_id", current.problem_id);
    probRcd.query();
    if (probRcd.next()) {
        probRcd.state = '107';
        probRcd.update();
    }

 

 

View solution in original post

10 REPLIES 10

Community Alums
Not applicable

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 

SarthakKashya2_0-1714308602126.png

 

Please mark my answer correct and helpful if this works for you

 

Thanks and Regards 

Sarthak

 

 

Ashutosh C J
Tera Guru

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

AshutoshCJ_0-1714313183756.png

 

 

 

    var probRcd = new GlideRecord('problem');
    probRcd.addActiveQuery();
    probRcd.addQuery("sys_id", current.problem_id);
    probRcd.query();
    if (probRcd.next()) {
        probRcd.state = '107';
        probRcd.update();
    }

 

 

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.

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);

AshutoshCJ_1-1714329518960.png

Please mark my solution as accepted or helpful to help the community better

 

 



Its giving me a clear picture on addQuery API. thanks a lot for this explanation. Really appreciated for your efforts and explanation.