Javascript for a dynamic PDF stamp dialog box not working

1.4k views Asked by At

I have been trying to create a dynamic PDF-XChange stamp with 4 dynamic text fields (Text1, Text2, Text3 and Text4) on the stamp that the user can edit in a dialog before the stamp is placed. What I want is for the user to select the stamp and the dialog popup to open with 4 user input fields:

  1. RMA Status - user to add the status (named RMAS)
  2. A free text field for the user to enter anything (named FREE)
  3. The user's name and business team, pulled from the user's Identity info (named TEAM)
  4. Today's date, editable incase they are stamping something after the fact (named DATE)

I had it working where it popped up a dialog box 4 times asking the user each question in a row, but a single dialog box with 4 text fields is what has been requested. I just can't seem to work out how my code below needs to change to work, but I'm very new to this.

Currently it only opens the last of the questions (date), unpopulated by my JavaScript date code just before dialog activation, none of the other questions. If I remove the date element, it asks the question before it (name and business unit), but it won't show a dialog with all 4 questions pre-populated using the data processed at the end of the code, which has been tested separately and is working.

My code is commented below. If anyone can help me to get the dialog to open with the 4 text fields pre-populated I would love to see where I went wrong. And if you can help me get it adding the data to the stamp's Text1 to Text4 boxes I'd be over the moon!

// Dialog Definition 
var oDlg = {
    
    RMAStatus: "",
    FreeText: "",
    NameAndUnit: "",
    TodaysDate: "",

    description:
    {
        name: "Stamp details",
        elements:
        [
            {
                type: "view",
                elements:
                [
                    {
                        type: "view",
                        elements:
                        [
                            {
                                item_id: "lbl1",
                                type: "static_text",
                                name: "&RMA Stage",
                            },
                            {
                                width: 200,
                                height: 22,
                                type: "edit_text",
                                item_id: "rmas",
                            }
                        ],
                        type: "view",
                        elements:
                        [
                            {
                                item_id: "lbl1",
                                type: "static_text",
                                name: "&Free text field",
                            },
                            {
                                width: 200,
                                height: 88,
                                type: "edit_text",
                                item_id: "free",
                            }
                        ],
                        type: "view",
                        elements:
                        [
                            {
                                item_id: "lbl1",
                                type: "static_text",
                                name: "&Your name and team",
                            },
                            {
                                width: 200,
                                height: 22,
                                type: "edit_text",
                                item_id: "team",
                            }
                        ],
                        type: "view",
                        elements:
                        [
                            {
                                item_id: "lbl1",
                                type: "static_text",
                                name: "&Stamp date",
                            },
                            {
                                width: 200,
                                height: 22,
                                type: "edit_text",
                                item_id: "date",
                            }
                        ]
                    },
                    {
                        type: "ok_cancel",
                    }
                ]
            }
        ]
    },
    initialize: function(dialog) { // where the data in dialog elements is set when the dialog is first activated
        dialog.load({
            "rmas":this.RMAStage,  
            "free":this.FreeText,
            "team":this.CorrectName,
            "date":this.todaysDate,
        });
    },
    commit: function(dialog) { // where element data is acquired when the user presses the OK button
        var data = dialog.store();
        this.RMAStage = data["rmas"];
        this.FreeText = data["free"];
        this.CorrectName = data["team"];
        this.todaysDate = data["date"];
    }
};

/*
 * Pre-fill the dialog text fields with the data below
 */
/* RMA STAGE (for RMAS to be populated) */
oDlg.RMAStage = "RMA Stage";

/* FREE TEXT FIELD (for FREE to be populated)  */
oDlg.FreeText = "";

/* NAME AND UNIT (for TEAM to be populated)  */
var IdentityName, IdentityNameSplit, Unit;

/* Set Organisation Unit */
Unit = "Parks & Landscapes Team";  // Unlikely to change

/* Find correctly formatted username from Identity info */
if((identity.name != null) && !/^\s*$/.test(identity.name))
    IdentityName = identity.name;
else
    IdentityName = identity.loginName.replace(/\./g," ").replace(/\./g," ").replace(/\b(\w)/g,function(Word,cFst){return cFst.toUpperCase()});

if (IdentityName.indexOf(', ') > -1) { // If the result is "Surname, Firstname" swap the names to make "Firstname Surname"
    IdentityNameSplit = IdentityName.split(', ');
    oDlg.NameAndUnit = IdentityNameSplit[1] + " " + IdentityNameSplit[0] + ", " + Unit;
}
else
    oDlg.NameAndUnit = IdentityName + ", " + Unit;

/* FORMATTED DATE FIELD (for DATE to be populated)  */
var stampDate;
stampDate = new Date();
oDlg.TodaysDate = util.printd("dd mmmm, yyyy", stampDate);

// Start dialog function
app.execDialog (oDlg);
1

There are 1 answers

0
Skry On BEST ANSWER

Nailed it! In case anyone else wants to copy/paste this solution for themselves:

var pagDlg =
{

    result: "cancel",
    DoDialog: function () { return app.execDialog(this); },
    //definitions
    strDefFmt: "dd-mm-yyyy",
    strFmt: "dddd d mmmm, yyyy",
    vDate: new Date,
    strDate: "",
    iOChecked: false,
    abwChecked: false,
    //
    SetListSel: function (list, path) {
        if (path.length == 0) return;
        eval("list[\"" + ((path.join != "function") ? path : path.join("\"][\"")) + "\"] = 1");
    },
    GetListSel: function (oLstResult) {
        for (var item in oLstResult) {
            if ((typeof oLstResult[item] == "number") && (oLstResult[item] > 0))
                return item;
        }
        return "";
    },
    formatDate: function (d, f) {
        return util.printd(f, d);
    },
    //Initialize values that will be used in the pop-up dialogue
    initialize: function (dialog) {
        var unit = "Technical Services & Design";
        this.strDate = this.formatDate(this.vDate, this.strDefFmt);
        //this.strDate = this.formatDate(this.vDate, "dd mmmm, yyyy");
        //
        var dlgInit =
        {
            "date": this.strDate,
            "free": "Enter your description text here.", //enable this checkbox option as default
            "abw_2": IdentityName + ", " + unit,
            "rma": "RMA Stage",
        };
        dialog.load(dlgInit);
        iOChecked = dlgInit["free"]; // initialise and keep it the same as the value in dlgInit
        abwChecked = dlgInit["abw_2"];
        rmaChecked = dlgInit["rma"];
    },
    commit: function (dialog) { // Called when OK button is pressed
        var oRslt = dialog.store();
        var path = new Array();
        var d = oRslt["date"];  //OK Go date
        //
        if (typeof d == "string")
            d = util.scand(this.strDefFmt, d);
        if (d != null)
        {
            var cur = new Date;
            d = new Date(d.getFullYear(), d.getMonth(), d.getDate(), cur.getHours(), cur.getMinutes(), cur.getSeconds());
        }
        this.vDate = d;
        //

        this.rmaChecked = oRslt["rma"];
        this.iOChecked = oRslt["free"];
        this.abwChecked = oRslt["abw_2"];
        this.strDate = oRslt["date"];
    },
    //Description of the layout of the Dialoge Window
    description:
    {
        name: "Stamp details",
        width: 450,
        elements:
        [
            {
                type: "view",
                width: 450,
                elements:
                [
                    {
                        type: "view",
                        align_children: "align_row",
                        elements:
                        [
                            {
                                item_id: "lbl1",
                                name: "RMA Stage",
                                type: "static_text",
                                width: 100,
                            },
                            {
                                item_id: "rma",
                                type: "edit_text",
                                width: 300,
                            },
                        ]
                    },
                    {
                        type: "view",
                        align_children: "align_row",
                        elements:
                        [
                            {
                                item_id: "lbl1",
                                name: "Free text",
                                type: "static_text",
                                width: 100,
                                alignment: "align_top",
                                align_children: "align_top",
                            },
                            {
                                item_id: "free",
                                type: "edit_text",
                                multiline: true,
                                width: 300,
                                height: 88,
                            },
                        ]
                    },
                    {
                        type: "view",
                        align_children: "align_row",
                        elements:
                        [
                            {
                                item_id: "lbl1",
                                name: "Name and team",
                                type: "static_text",
                                width: 100,
                            },
                            {
                                item_id: "abw_2",
                                type: "edit_text",
                                width: 300,
                            },
                        ]
                    },
                    {
                        type: "view",
                        align_children: "align_row",
                        elements:
                        [
                            {
                                item_id: "lbl1",
                                name: "Date",
                                type: "static_text",
                                width: 100,
                            },
                            {
                                item_id: "date",
                                type: "edit_text",
                                DateEdit: true,
                                width: 100,
                            }
                        ]

                    },
                    {
                        type: "ok_cancel",
                    },
                ]
            }
        ]
    }
};
/* NAME */
var IdentityName, IdentityNameSplit;

/* Find correctly formatted username from Identity info */
if((identity.name != null) && !/^\s*$/.test(identity.name))
    IdentityName = identity.name;
else
    IdentityName = identity.loginName.replace(/\./g," ").replace(/\./g," ").replace(/\b(\w)/g,function(Word,cFst){return cFst.toUpperCase()});

if (IdentityName.indexOf(', ') > -1) { // If the result is "Surname, Firstname" swap the names to make "Firstname Surname"
    IdentityNameSplit = IdentityName.split(', ');
    IdentityName = IdentityNameSplit[1] + " " + IdentityNameSplit[0];
}

if (event.source.forReal && (event.source.stampName == "#T2CKw07Lo6sr42d6F3ao#0"))
{
    if (pagDlg.DoDialog() == "ok")
    {
        var d = pagDlg.vDate; 

        this.getField("Text1").value = pagDlg.rmaChecked; // RMA Stage
        this.getField("Text2").value = pagDlg.iOChecked; // Free text
        this.getField("Text3").value = pagDlg.abwChecked; // Name and team
        this.getField("Text4").value = util.printd(pagDlg.strFmt, d); // Date
    }
    else
    {
        app.alert("Stamp cancelled", 3);
    }
}
else
{
    var d = pagDlg.vDate;
    event.value = util.printd(pagDlg.strFmt, d);
}

Hopefully someone will find this useful.