Code snippet: How to update print management settings through X++?

Sometimes there is a need for a bulk update of print management settings and it is quite simple after understanding how this area works. Here you can find a code snippet for updating the sales order packing slip printing settings for the specific customer. In this example, we are looking for all the customers assigned to site Z1209S, which do not have their packing slip printer settings overridden (and disabled).

static void DisableCustSalesPackingSlipPrinting(Args _args)
{
    Dialog                      dialog;
    DialogField                 dialogField;

    QueryRun                    qr;
    Query                       q = new Query();
    QueryBuildDataSource        qbds;
    QueryBuildRange             qbr;

    CustTable                   custTable;
    PrintMgmtDocInstance        printMgmtDocInstance;
    PrintMgmtSettings           printMgmtSettings;
    PrintMgmtDocInstance        printMgmtDocInstance_Default;
    PrintMgmtSettings           printMgmtSettings_Default;

    // Define the "nodeType" - CustTable for customers.
    PrintMgmtNodeType           nodeType        = PrintMgmtNodeType::CustTable;
    // Define the "documentType" - packing slip is updated here.
    PrintMgmtDocumentType       documentType    = PrintMgmtDocumentType::SalesOrderPackingSlip;
    // Define the "instanceType" - we are defining the "original" document settings. 
    PrintMgmtDocInstanceType    instanceType    = PrintMgmtDocInstanceType::Original;

    // We are selecting the "default" settings which can be overridden. 
    select firstOnly * from printMgmtDocInstance_Default
        where   printMgmtDocInstance_Default.ReferencedTableId  == 0
        &&      printMgmtDocInstance_Default.ReferencedRecId    == 0
        &&      printMgmtDocInstance_Default.NodeType           == nodeType
        &&      printMgmtDocInstance_Default.DocumentType       == documentType
        &&      printMgmtDocInstance_Default.PrintType          == instanceType;

    select firstOnly * from printMgmtSettings_Default
        where   printMgmtSettings_Default.ParentId == printMgmtDocInstance_Default.RecId;

    if (!printMgmtDocInstance_Default || !printMgmtSettings_Default)
    {
        throw error('Default settings were not found.');
    }

    qbds = q.addDataSource(tableNum(CustTable));

    /*
    You can add your criteria here. 
    */
    qbr = qbds.addRange(fieldNum(CustTable, InventSiteId));
    qbr.value(queryValue('Z1209S'));
    
    // We are looking for all customers assigned to site Z1209S, which don't have overridden printer settings.
    qbds = qbds.addDataSource(tableNum(PrintMgmtDocInstance));
    qbds.relations(false);
    qbds.addLink(fieldNum(CustTable, TableId), fieldNum(PrintMgmtDocInstance, ReferencedTableId));
    qbds.addLink(fieldNum(CustTable, RecId), fieldNum(PrintMgmtDocInstance, ReferencedRecId));
    qbds.joinMode(JoinMode::NoExistsJoin);

    qbr = qbds.addRange(fieldNum(PrintMgmtDocInstance, NodeType));
    qbr.value(queryValue(nodeType));

    qbr = qbds.addRange(fieldNum(PrintMgmtDocInstance, DocumentType));
    qbr.value(queryValue(documentType));

    qbr = qbds.addRange(fieldNum(PrintMgmtDocInstance, PrintType));
    qbr.value(queryValue(instanceType));

    // This is the settings which determines if the document is printed. 
    qbr = qbds.addRange(fieldNum(PrintMgmtDocInstance, Suppress));
    qbr.value(queryValue(NoYes::Yes));

    qr = new QueryRun(q);

    while (qr.next())
    {
        custTable = qr.get(tableNum(CustTable));

        select firstonly forupdate * from printMgmtDocInstance
            where   printMgmtDocInstance.ReferencedTableId  == custTable.TableId
            &&      printMgmtDocInstance.ReferencedRecId    == custTable.RecId
            &&      printMgmtDocInstance.NodeType           == nodeType
            &&      printMgmtDocInstance.DocumentType       == documentType
            &&      printMgmtDocInstance.PrintType          == instanceType;

        if (printMgmtDocInstance)
        {
            printMgmtDocInstance.ttsbegin();

            printMgmtDocInstance.Suppress = true;
            printMgmtDocInstance.update();

            printMgmtDocInstance.ttscommit();

            info(strFmt('%1: %2 # Printing settings have been updated.',
                tableId2pname(tableNum(CustTable)),
                custTable.AccountNum));
        }
        else
        {
            printMgmtDocInstance.clear();

            buf2Buf(printMgmtDocInstance_Default, printMgmtDocInstance);

            printMgmtDocInstance.ReferencedTableId  = custTable.TableId;
            printMgmtDocInstance.ReferencedRecId    = custTable.RecId;
            printMgmtDocInstance.Suppress           = true;

            printMgmtDocInstance.insert();

            printMgmtSettings.clear();

            buf2Buf(printMgmtSettings_Default, printMgmtSettings);

            printMgmtSettings.ParentId = printMgmtDocInstance.RecId;

            printMgmtSettings.insert();

            info(strFmt('%1: %2 # Printing settings have been added and overridden.',
                tableId2pname(tableNum(CustTable)),
                custTable.AccountNum));
        }
    }
}

Leave a Reply