Running a Connected Query in Scheduled Mode

In scheduled mode, the connected query is run by Process Scheduler. In the case of RunMode_Sched_File, the output data is written to an XML file. In the case of RunMode_Sched_Web, the output data is accessible via a hyperlink in Process Monitor. In scheduled mode, the  runProcessInfo parameter is required; the Prompts parameter should be set to Null.

The following example represents a PeopleCode step to be run via Application Engine:

import PT_CONQRS:CONQRSMGR;
import PT_CONQRS:SCHED_INFO;

Local PT_CONQRS:CONQRSMGR &cConQrsInst;
Local boolean &result;
Local string &PRCSFILENAME;
Local string &sPrcsName = "PSCONQRS";
Local string &sPrcsType = "Application Engine";
Local number &nOrigPSMessagesMode = %Session.PSMessagesMode;
%Session.PSMessagesMode = 1;

/* While working with connected queries, it is recommended to use a try- */
/* catch block                                                           */

try
   /* Create and open an object. System tries to find a connected query  */
   /* with private ownership first for current user. If not found, it    */
   /* uses a public ownership                                            */
   &ConQrsInst = create PT_CONQRS:CONQRSMGR("", PSCONQRS_AET.CONQRSNAME);
   &result = &ConQrsInst.Open(&ConQrsInst.Const.InitExisting);
   &str = &ConQrsInst.ErrString;
   If &str <> "" Then
      WriteToLog(%ApplicationLogFence_Error, &ConQrsInst.ErrString);
      %Session.PSMessagesMode = &nOrigPSMessagesMode;
      Exit (1);
   End-If;
   If &result Then
      /* Get an empty SCHEDINFO structure and populate it.               */
      &schedInfo = &ConQrsInst.SchedInfo;
      &schedInfo.DIRLOCATION = %FilePath;
      &schedInfo.OUTDESTTYPE = String(%OutDestType);
      &schedInfo.RUN_CNTL_ID = PSCONQRS_AET.RUN_CNTL_ID;
      &schedInfo.PROCESS_INSTANCE = PSCONQRS_AET.PROCESS_INSTANCE;
      &schedInfo.OPRID = PSCONQRS_AET.OPRID;
      If %OutDestType = 2 Then /* FILE */
         Local string &sqlFile = "SELECT OUTDEST FROM PS_PRCSRUNCNTLDTL ⇒
WHERE OPRID=:1 and RUNCNTLID =:2 and PRCSTYPE =:3 and PRCSNAME =:4";
         SQLExec(&sqlFile, PSCONQRS_AET.OPRID, PSCONQRS_AET.RUN_CNTL_ID, ⇒
&sPrcsType, &sPrcsName, &PRCSFILENAME);
      End-If;
      &schedInfo.PRCSFILENAME = &PRCSFILENAME;
      /* Indicate the source of the scheduled request as CQR.            */
      &schedInfo.AE_ID = &ConQrsInst.Const.SchedRequest_CQR;
      /* Schedule the query to run. Note that for scheduled connected    */
      /* queries, the Prompts parameter is Null.                         */
      &result = &ConQrsInst.Run( Null, &schedInfo);
   End-If;
   If &result Then
      %Session.PSMessagesMode = &nOrigPSMessagesMode;
      Exit (0);
   Else
      /* Check for errors  */
      &str = &ConQrsInst.ErrString;
      If &str <> "" Then
         WriteToLog(%ApplicationLogFence_Error, &ConQrsInst.ErrString);
      End-If;
      /* check session message for errors */
      If %Session.PSmessages.Count > 0 Then
         &PSMessages = %Session.PSmessages;
         For &i = 1 To &PSMessages.Count
            If (&PSMessages.Item(&i).MessageType <= 1) Then
               &MsgSetNbr = &PSMessages.Item(&i).MessageSetNumber;
               &MsgNbr = &PSMessages.Item(&i).MessageNumber;
               WriteToLog(%ApplicationLogFence_Error, MsgGet(&MsgSetNbr, ⇒
&MsgNbr, "Message Not Found : " | &MsgSetNbr | "," | &MsgNbr));
            End-If;
         End-For;
      End-If;
   End-If;
   %Session.PSMessagesMode = &nOrigPSMessagesMode;
catch Exception &Err
   WriteToLog(%ApplicationLogFence_Error, &Err.ToString());
end-try;