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

chanikya
Kilo 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

Hi Sanjiv,



Can i expect any updates please


Sorry I am travelling. Try this. You need both the scripts



Use this



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.

Hi   Sanjeev,



I have one query here,


With little confusion,





When we click on "REPUBLISH" then only "employee Kb" workflow should be deleted, KBapprove workflow should active.



We are running above script in workflow Runscript--when we click on "REPUBLISH" button then "KBApprover" workflow activated , in that workflow we whave Runscript it will be activated when Kbapprove workflow is going to end.so what happens if we have any privously closed kbapprove records in workflow context that records will be deleted recently   closed kbapprove record available r...right.



If we see when KBApprove ended EmployeeKb should be activated...as per the script it is deleted.





OurThread is:         if new KBAPPROVER workflow activated that time only it will be deleted....when kbapprover is ended, EmployeeKb active....


Hi Chanikya,



In KB Approver workflow, the script i provided   should be the first activity after begin. This will delete at old KB Approver workflows and the Employee Knowledge. Once the KB Approver workflow ends, I think you would be setting the approval state to approved. the New Employee Knowledge workflow should begin again based on the condition state=approved set in the Employee Knowledge workflow.



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

Hi Sanjeev,




this is my workflow conditions,


find_real_file.png



find_real_file.png