Fix Script

Chad Wilhelm1
Tera Expert

Hello,

On the problem table there is a field called known error which is true/false.   I am trying to set this for records where a Knowledge Article was created in the KE Knowledge Base.   Any assistance would be appreciated. Thanks!

var knowledge = new GlideRecord('kb_knowledge');
knowledge.addEncodedQuery("source.sys_class_name=problem^kb_knowledge_base=e33ab413dbfe16401c76d9fdbf96194a");
knowledge.query();
while(knowledge.next()){
var problem = new GlideRecord('problem');
problem.get(knowledge.problem.syd_id);
while(problem.next()){
problem.setValue ('known_error', true);
problem.update();
}
}

1 ACCEPTED SOLUTION

Mark Stanger
Giga Sage

You can try this script.  I've commented the 'update' at the end out so that you can test and see that you're getting the right stuff.  Run it from 'Scripts -> Background' to see what the output is and make sure you're getting the correct items before actually doing the update.

var knowledge = new GlideRecord('kb_knowledge');
knowledge.addEncodedQuery("source.sys_class_name=problem^kb_knowledge_base=e33ab413dbfe16401c76d9fdbf96194a");
knowledge.query();
gs.print('Knowledge article count: ' + knowledge.getRowCount());
while(knowledge.next()){
    var problem = new GlideRecord('problem');
    problem.addQuery('sys_id', knowledge.source.syd_id);
    problem.query();
gs.print('Problem count should equal KB count: ' + problem.getRowCount());
    while(problem.next()){ 
        problem.setValue('known_error', true);
        problem.setWorkflow(false); // Don't run business rules, etc.
        problem.autoSysFields(false); // Don't update system fields
        //problem.update();
gs.print('Problem: ' + problem.number);
    }
}

View solution in original post

2 REPLIES 2

Mark Stanger
Giga Sage

You can try this script.  I've commented the 'update' at the end out so that you can test and see that you're getting the right stuff.  Run it from 'Scripts -> Background' to see what the output is and make sure you're getting the correct items before actually doing the update.

var knowledge = new GlideRecord('kb_knowledge');
knowledge.addEncodedQuery("source.sys_class_name=problem^kb_knowledge_base=e33ab413dbfe16401c76d9fdbf96194a");
knowledge.query();
gs.print('Knowledge article count: ' + knowledge.getRowCount());
while(knowledge.next()){
    var problem = new GlideRecord('problem');
    problem.addQuery('sys_id', knowledge.source.syd_id);
    problem.query();
gs.print('Problem count should equal KB count: ' + problem.getRowCount());
    while(problem.next()){ 
        problem.setValue('known_error', true);
        problem.setWorkflow(false); // Don't run business rules, etc.
        problem.autoSysFields(false); // Don't update system fields
        //problem.update();
gs.print('Problem: ' + problem.number);
    }
}

Hello Mark,

This worked 99% I had to tweek the problem addQuery to use the number field instead of the sys_id.   Thanks for your assistance!

Chad