Trying to Schedule VBScript that prints Sales Orders

We are on Sage 100 Advanced v2017 and I have a VBScript that prints Sales Orders using the SO_SalesOrderPrinting_UI object.

Does anyone know if this object requires an active Windows session in order to run? 

The script runs perfectly when I run it manually.  It also runs correctly when I schedule it with Task Schedule to "run only when user is logged on".  However, we want to schedule this to run when I'm not logged on, and that doesn't work.  Our IT has narrowed down to the script, because we can get other VBScript files to run correctly with this setting, such as a script that creates another file.  

I've scheduled VBScripts to run that create Sales Orders from files etc, but I've never tried this printing object on the scheduler.  So I'm wondering if this is the issue - after all, it does have UI right in its name.  

If this is not going to run without a Windows session, does anyone have a solution for printing Sales Orders on a schedule?  

(Background:  some sales orders require 2 different forms to be printed, and the CSR forgets.  So we have a script on SO entry that checks the SO lines for the criteria that tells us it needs to be printed, and then writes the SO number to a UDT.  The scheduled script is to pick up the SO's from the UDT and print them.)

  • 0

    Are you using a mapped drive path at all?  Those do not exist without a user session.

  • 0 in reply to Kevin M

    No, not mapped.  We were able to read from and write to the path with a VBScript that just did file object methods to verify.  

  • 0 in reply to hyanaga

    Local print queue, not redirected?

  • 0

    Along the lines of what Kevin said, where is the print output going to - a printer, to Paperless, to Deferred? Since there presumably could be normal UI that appears with form printing, these things come to mind IF they are relevant for you:

    oSS.nTerminateUI() 'an oldie so not sure if it's still supported or has been deprecated.
    Also obviously suppress any explicit MessageBox's, ProgressBar's, other UI you are issuing yourself.

    If printing to Paperless:
    If oReport.PDFEnabled Then oReport.PDFSilent = 1
    'Assuming oReport points to SO_SalesOrderPrinting_Rpt (NOT SO_SalesOrderPrinting_UI)

  • 0 in reply to Alnoor

    I was not able to get the SO_SalesOrderPrinting_Rpt  object to work for me, maybe because I am using the QuickPrint method.  I will post my code to see if anyone has suggestions.

  • 0 in reply to hyanaga

    ** EDIT ** Changed oReport.QuickPrint to oReport.sQuickPrint



    I'm guessing you just need to do add this:

    oReport.sQuickPrint = sOrderNo 'where sOrderNo is your Sales Order No

    If it's not there it will attempt to chain into the next form/report. E.g. it may attempt to show you the "Do you want to print Picking Sheets Y/N ? " messsage. Setting the QuickPrint property will bypass that.

  • 0 in reply to Alnoor

    Here is my whole script, with the nTerminateUI() function added.  (It did not fix the issue.)  FYI - there is a loop in here that reads the SO# from a custom table, so this is more than just the printing code:


    'Set values for MAS connection
    ServerPath = "E:\SAGE\2017 Sage 100 Advanced\MAS90\Home"
    UserName = "Scripts"
    Password = "12345678"
    Company = "TST"
    dDate = Date()
    ModuleDate = CStr(Year(dDate)) & Right("0" & CStr(Month(dDate)),2) & Right("0" & CStr(Day(dDate)),2)

    Set oScript = CreateObject("ProvideX.Script")
    oScript.Init(ServerPath)

    Dim oSession 'As Object
    Set oSession = oScript.NewObject("SY_SESSION")

    retVal = oSession.nSetUser(UserName, Password)

    retVal = oSession.nSetCompany(Company)

    retVal = oSession.nSetDate("C/M", ModuleDate)

    retVal = oSession.nSetModule("C/M")

    retVal = oSession.nSetProgram(oSession.nLookupTask("CM_UDTMaint_UI"))

    Set soTable = oScript.NewObject("CM_UDTMaint_Bus", oSession, "SO_UDT_WT_PRINTING")

    retVAL = soTable.nMoveFirst()

    Do While Not CBool(soTable.nEOF)
    delete = true
    retVal = oSession.nSetProgram(oSession.nLookupTask("SO_SalesOrderPrinting_UI"))

    Set soPRINT = oScript.NewObject("SO_SalesOrderPrinting_rpt", oSession)
    oSession.nTerminateUI()
    retVal = soPRINT.nSetKeyValue("ModuleCode$", "S/O")
    retVal = soPRINT.nSetKeyValue("ReportID$", "SO_SalesOrderPrinting_UI")

    retval = soTable.nGetValue("UDF_SONUMBER$",soNo)

    retVal = soPRINT.nSelectReportSetting("WORK TICKET CS")

    soPRINT.sQuickPrint = soNo

    soPRINT.nReportType = 6
    retval = soPRINT.nInitReportEngine()
    retVal = soPRINT.nSetExportOptions(5,"\\bci-fs02\MAS\" + soNo + ".pdf")

    retVal = soPRINT.nProcessReport("EXPORT")
    if retVal = 0 then
    err = soPRINT.sLastErrorMsg + "on Export; "
    retval = soTable.SetValue("UDF_PRINTER$", err)
    end if


    retVal = soPRINT.nProcessReport("PRINT")
    if retVal = 0 then
    err = err + soPRINT.sLastErrorMsg + "on Print; "
    retval = soTable.SetValue("UDF_PRINTER$", err)
    delete = false
    else
    Set oSO = oScript.NewObject("SO_SalesOrder_bus",oSession)
    retVal = oSO.AsObject(oSO.ScriptObject).DeactivateProcedure("PostWrite")
    retVal = oSO.nSetKeyValue("SalesOrderNo$",soNo)
    retVal = oSO.nSetKey()
    retVal = oSO.nSetValue("UDF_WORK_TICKET_PRINTED$","Y")
    retVal = oSO.nWrite()
    If retVal = 0 and delete then
    err = err + soSO.sLastErrorMsg + "on SO Save"
    retval = soTable.SetValue("UDF_PRINTER$", err)
    End if
    Set oSO = nothing
    Set soPRINT = nothing

    end if

    If delete then
    soTable.nDelete()
    End if

    soTable.nMoveNext()
    Loop

  • 0 in reply to hyanaga

    I see you are already setting the QuickPrint property to the SO number on the _RPT object (via soPRINT) so that's good. Also you have nReportType = 6 for the UI suppress on export so that's good too.

    Is the WORK TICKET CS form Paperless enabled or does the nProcessReport("PRINT") go to an actual printer? If actual printer that printer name for that WORK TICKET CS form code has to be valid for the Windows user specified inside the Windows Task Scheduler job when it's running unattended. I noted a way below to hard code the printer name if that's applicable for you.

    If Paperless enabled you can add: 
    If soPRINT.PDFEnabled Then soPRINT.PDFSilent = 1 

    Also I noticed for soPRINT you are doing only 2 nSetKeyValue's and the accompanying nSetKey, nSetValue, and nWrite are missing. Note: I haven't tested this so this may not be 100% right but this is how I would initially approach it:

    repSetting = "WORK TICKET CS"

    retVal = soPRINT.nSelectReportSetting(repSetting)

    retVal = soPRINT.nSetKeyValue("ModuleCode$", "S/O")
    retVal = soPRINT.nSetKeyValue("CompanyKey$", oSession.sCompanyKey)
    retVal = soPRINT.nSetKeyValue("ReportID$", "SO_SalesOrderPrinting_ui")
    retVal = soPRINT.nSetKeyValue("ReportSetting$", repSetting)
    retVal = soPRINT.nSetKeyValue("RowKey$", "00001")
    retVal = soPRINT.nSetKey()

    retVal = soPRINT.nSetValue("SelectField$", "Order Number")
    retVal = soPRINT.nSetValue("SelectFieldValue$", "Order Number")
    retVal = soPRINT.nSetValue("Tag$", "TABLE=SO_SALESORDERHEADER; COLUMN=SALESORDERNO$;")
    retVal = soPRINT.nSetValue("Operand$", "=")
    retVal = soPRINT.nSetValue("Value1$", soNo)
    retVal = soPRINT.nSetValue("KeyReference$", "")

    soPRINT.sQuickPrint = soNo
    If soPRINT.PDFEnabled Then soPRINT.PDFSilent = 1 

    retVal = soPRINT.nWrite()

    'optional line to hard code a printer prior to the nProcessReport("PRINT")
    'retVal = soPRINT.nValidatePrinter("enter printer name")
    ' e.g. retVal = soPRINT.ValidatePrinter("HP LaserJet 5") 'now check the retVal

     

  • 0 in reply to Alnoor

    This is what I have used for "quick printing" to an exported file.

    I've used your values for the ReportSetting and ExportPath.


    sReportSetting = "WORK TICKET CS"
    sExportPath = "\\bci-fs02\MAS\" + soNo + ".pdf"
    If oSession.nSetProgram(oSession.nLookupTask("SO_SalesOrderQuickPrint_UI")) > 0 Then
       Set oSO_SalesOrderPrinting_Rpt = oSession.oNewObject("SO_SalesOrderPrinting_Rpt")
       If oSO_SalesOrderPrinting_Rpt.nInitReportEngine() > 0 Then
          oSO_SalesOrderPrinting_Rpt.nReportType = 6
          If oSO_SalesOrderPrinting_Rpt.nSelectReportSetting(sReportSetting) > 0 Then
             oSO_SalesOrderPrinting_Rpt.sQuickPrint = soNo
             If CBool(oSO_SalesOrderPrinting_Rpt.nPDFEnabled) Then oSO_SalesOrderPrinting_Rpt.nPDFSilent = 1
             retVal = 0 : retVal = oSO_SalesOrderPrinting_Rpt.nSetExportOptions(5, sExportPath)
             retVal = 0 : retVal = oSO_SalesOrderPrinting_Rpt.nProcessReport("EXPORT")
          End If
       End If
       Set oSO_SalesOrderPrinting_Rpt = Nothing
    End If


    Depending on whether or not you have the "Keep Open After Printing" enabled you may need to run through the routine again to print it to a printer using Alnoor's suggested "nValidatePrinter" lines.