How to delete a record in "Workflow Context" when we click on UI Action

chanikya
Mega Sage

Hi Every One ,

Brand new KB Article: Goes through KBApprove workflow.

Upon close of KBApprove, Employee Knowledge workflow starts.

User clicks UI Button to "Republish":

Cancel the Employee Knowledge workflow and start the KBApprove workflow.

Upon close of KBApprove workflow, a new Employee Knowledge workflow starts.

Issue :       when i click on "Republish button" it is creating new KbApprove workflow,   when KB APProve is Activated then earlier created   Employee KB workflow   is to be deleted in Workflow Context , but it is still running, i just want to delete that Second workflow-Employee KB from workflow Context. and Once i   Published KB record then KBAprove is finished that time only Employee KB Should come to Active state.

find_real_file.png

find_real_file.png

find_real_file.png

Employee Knowledge -workflow

find_real_file.png

find_real_file.png

1 ACCEPTED SOLUTION

Ok. Great. So this should work all fine. This should be at the beginning of the KB Approval workflow



var wf =new GlideAggregate('wf_context');


wf.addEncodedQuery('workflow_versionLIKEKB approval V2^active=false^id='+current.sys_id);


wf.addAggregate('COUNT');


wf.query();


var wcounts =0;


var deletecount = 0;


if(wf.next())


  wcounts = wf.getAggregate('COUNT');


var inc= new GlideRecord('wf_context');


inc.addEncodedQuery('workflow_versionLIKEKB approval V2^active=false^id='+current.sys_id);


inc.orderBy('sys_created_on');


inc.query();


while(inc.next())


{


        deletecount = deletecount++;


        if (deletecount<wcounts)


                  inc.deleteRecord();


}



GlideRecord('wf_context');


inc.addEncodedQuery('workflow_versionLIKEEmployee Knowledge^id='+current.sys_id);


inc.orderBy('sys_created_on');


inc.query();


while(inc.next())


{


                  inc.deleteRecord();


}



Please mark this response as correct or helpful if it assisted you with your question.

View solution in original post

15 REPLIES 15

SanjivMeher
Mega Patron

You can write a script in the kbapprove workflow. Query the workflow context by it's name and delete it. Query on wf_context table



Please mark this response as correct or helpful if it assisted you with your question.

Hi,



Using with this script i can able to delete KBApprove workflow if it is more than one KBApprove record in Workflow Context.


Now i want to delete Employee KB workflow from workflow context when KBApprove is activated.



"


Upon close of KBApprove, Employee Knowledge workflow starts.


User clicks UI Button to "Republish":


Cancel the Employee Knowledge workflow and start the KBApprove workflow.


Upon close of KBApprove workflow, a new Employee Knowledge workflow starts.



"




Can   i use this :Query line.-     workflow_versionLIKEEmployee Knowledge -Retied^state=executing^active=true


Script:


var wf =new GlideAggregate('wf_context');


wf.addEncodedQuery('workflow_versionLIKEKB approval V2^active=false');


wf.addAggregate('COUNT');


wf.query();


var wcounts =0;


var deletecount = 0;


if(wf.next())


  wcounts = wf.getAggregate('COUNT');


var inc= new GlideRecord('wf_context');


inc.addEncodedQuery('workflow_versionLIKEKB approval V2^active=false');


inc.orderBy('sys_created_on');


inc.query();


while(inc.next())




{


        deletecount = deletecount++;


        if (deletecount<wcounts)


                  inc.deleteRecord();


}


Try this. Use the document_id part in the kbapproval v2 approval script as well which I missed last time.



GlideRecord('wf_context');


inc.addEncodedQuery('workflow_versionLIKEEmployee Knowledge^document_id='+current.sys_id);


inc.orderBy('sys_created_on');


inc.query();


while(inc.next())


{


                  inc.deleteRecord();


}



Please mark this response as correct or helpful if it assisted you with your question.

Hi Sanjiv,




i have tried with script in different ways also , but not working


Could you please help me on this.



First time i have with this script :   No result


var wf =new GlideAggregate('wf_context');


wf.addEncodedQuery('workflow_versionLIKEKB approval V2^active=false');


wf.addAggregate('COUNT');


wf.query();


var wcounts =0;


var deletecount = 0;


if(wf.next())


  wcounts = wf.getAggregate('COUNT');


var inc= new GlideRecord('wf_context');


inc.addEncodedQuery('workflow_versionLIKEKB approval V2^active=false');


inc.addEncodedQuery('workflow_versionLIKEEmployee Knowledge^document_id='+current.sys_id);


inc.orderBy('sys_created_on');


inc.query();


while(inc.next())


{


        deletecount = deletecount++;


        if (deletecount<wcounts)


                  inc.deleteRecord();


}





Second time i have tried with : But no reult


var wf =new GlideAggregate('wf_context');


wf.addEncodedQuery('workflow_versionLIKEKB approval V2^active=false');


wf.addEncodedQuery('workflow_versionLIKEEmployee Knowledge^document_id='+current.sys_id);


wf.addAggregate('COUNT');


wf.query();


var wcounts =0;


var deletecount = 0;


if(wf.next())


  wcounts = wf.getAggregate('COUNT');


var inc= new GlideRecord('wf_context');


inc.addEncodedQuery('workflow_versionLIKEKB approval V2^active=false');


inc.addEncodedQuery('workflow_versionLIKEEmployee Knowledge^document_id='+current.sys_id);


inc.orderBy('sys_created_on');


inc.query();


while(inc.next())


{


        deletecount = deletecount++;


        if (deletecount<wcounts)


                  inc.deleteRecord();


}




find_real_file.png