How to Update on Incident table from Problem table ?

pranjal32
Tera Contributor

How can I achieve this scenario -- If a Problem Record is related to a Particular Incident then Incident record's short_description should get updated as same as the related problem record's short_description ? 

I have done it for one record I want to do it for multiple record .

 

var inc = new GlideRecord('incident');
inc.addEncodedQuery('number=INC0007002');
inc.query();
if(inc.next()){
    var prb = new GlideRecord('problem');
    prb.addEncodedQuery('first_reported_by_taskLIKEINC0007002');
    prb.query();
    if(prb.next()){
        inc.short_description = prb.short_description;
        inc.category='hardware'
        inc.update();
    }
}
6 REPLIES 6

Community Alums
Not applicable

Hi @pranjal32 , try below code

 

    var prb = new GlideRecord('problem');
    prb.addNotNullQuery('first_reported_by_task');
    prb.query();
    while (prb.next()) {
        var inc = new GlideRecord('incident');
        inc.addQuery('sys_id', prb.getValue('first_reported_by_task'))
        inc.query();
        while(inc.next()) {
            inc.short_description = prb.short_description;
            inc.category='hardware'
            inc.update();
          }
    }

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @pranjal32 

Please have a look in to Incident properties:

LearnNGrowAtul_0-1701874256875.png

 

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

Anand Kumar P
Giga Patron

Hi @pranjal32 ,

You can use below script

var inc = new GlideRecord('incident');
inc.addEncodedQuery('your_query_here'); //active true incidents
inc.query();

while (inc.next()) {
var prb = new GlideRecord('problem');
prb.addEncodedQuery('first_reported_by_task=' + inc.number);
prb.query();

if (prb.next()) {
inc.short_description = prb.short_description;
inc.category = 'hardware'; 
inc.update();
}
}

Thanks,

Anand

pranita-24
Tera Guru

Hi @pranjal32 
I hope below code will help you to achieve your scenario


var inc = new GlideRecord('incident');
inc.addEncodedQuery('YOUR_CONDITION_HERE');// Add your specific condition like type is email, phone whatever
inc.query();

// Loop through matching Incident records
while (inc.next()) {
var prb = new GlideRecord('problem');
prb.addQuery('first_reported_by_task', inc.number);
prb.query();

// If a related Problem record is found, update the Incident
if (prb.next()) {
inc.short_description = prb.short_description;
inc.category = 'hardware'; 
inc.update();
}
}


Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks