Hi all, today I’d like to share with you a script (in JavaScript code) I created to force Change records, it can be used for several purposes, in my case I wanted to close all old Changes stuck in a specific phase (I omitted to write the full query, you could set any query you want depending on your needs).
Please remember that you can execute this script via Schedule, Script Library, Process, Script or anything else you prefer 😉
function CloseChange(query) { var fChm= new SCFile("cm3r"); var rc = fChm.doSelect("query); if (rc == RC_SUCCESS) { while (rc == RC_SUCCESS) { fChm.status="closed" fChm.open=false; var arrDesc = new Array(); arrDesc.push("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * "); arrDesc.push(system.functions.tod().toString() + " - Change Closed by system"); arrDesc.push("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * "); arrDesc.push(""); var lng = fChm.description.length(); for (i = 0; i < lng; i++) { arrDesc.push(system.functions.nullsub(fChm.description[i], "")); } fChm.description = arrDesc; fChm.close_time = system.functions.tod(); fChm.doUpdate(); CreateApprovalLog(fChm); print("Change " + fChm.number + " closed by system."); rc = fChm.getNext(); } } } //function to create the ApprovalLog record function CreateApprovalLog(record) { var fApp = new SCFile("ApprovalLog"); fApp.file_name = "cm3r"; fApp.unique_key = record.number; fApp.category = record.category; fApp.current_phase = record.current_phase; fApp.action = "CLOSE"; fApp.date = system.functions.tod(); fApp.operator = "SYSTEM"; fApp.operator_fullname = "SYSTEM CLOSURE PROCESS"; var arrComm = new Array(); arrComm.push("Change moved to closed status."); fApp.comments = arrComm; fApp.doInsert(); }
As you can see in the previous code, I created also a CreateApprovalLog() function in order to create the activity for the changes being closed by this script (if you don’t need it, you can skip the row which calls this function).
Cheers and enjoy the code 😉
Leave a comment