Monday, February 17, 2014

Create XML through X++


class ExportToXML
{
}
public static void main(Args _args)
{
    #AviFiles
    smmCampaignSelection smmCampaignSelection;
    SysOperationProgress    progress = new SysOperationProgress();
    int                     total, updateCount, i;
    ContactPerson           contactPerson;
    XmlDocument doc;
    XmlElement nodeXml;
    XmlElement nodeSubs;
    XmlElement nodeSub;
    XmlElement nodeTable;
    XmlElement nodeEmail;
    XmlElement nodeName;
    XmlElement nodeCustomFields;
    XmlElement nodeCustomField;
    XmlElement nodeKey;
    XmlElement nodeValue;
    XmlElement nodeReSub;
    XmlElement nodeQueueSub;
    XmlElement nodeRestartSub;

    boolean _includeInvalid = false;
    smmCampaignTable _smmCampaignTable;

    #define.filename('D:\\campaignMultipleSubs.xml')
    /*#define.filename(strfmt("%1\%2_%3_%4_%5.csv", @"\\gs1melb17\axenvironments$\Prod\Campaigns\",
                                          curUserId(),
                                          _smmCampaignTable.CampaignId,
                                          strkeep(date2str(systemdateget(),321,2,1,2,1,4),"0123456789"),
                                          strkeep(time2str(timenow(),1,1),"0123456789"))) //TODO: To parameterise
*/
    ;
    _smmCampaignTable = smmCampaignTable::find('8012');

    if (!_includeInvalid)
        select count(Recid) from smmCampaignSelection where
            smmCampaignSelection.CampaignId == _smmCampaignTable.CampaignId
            //&& smmCampaignSelection.GS1_CampaignStatus != GS1_CampaignStatus::Invalid
            ;
    else
        select count(Recid) from smmCampaignSelection
            where smmCampaignSelection.CampaignId == _smmCampaignTable.CampaignId
            ;

    total = smmCampaignSelection.RecId;

    progress.setCaption("Export Communication List to Web");
    progress.setAnimation(#AviUpdate);
    progress.setTotal(total);

    ttsbegin;
    doc = XmlDocument::newBlank();
    nodeXml = doc.createElement('xml');

    doc.appendChild(nodeXml);
    nodeTable = doc.createElement('AddSubscribers');
    nodeXml.appendChild(nodeTable);
    nodeSubs = doc.createElement('Subscribers');
    nodeTable.appendChild(nodeSubs);
    i = 0;
    while select forupdate smmCampaignSelection
        where smmCampaignSelection.CampaignId == _smmCampaignTable.CampaignId
    {
        i++;
        //if (!_includeInvalid && smmCampaignSelection.GS1_CampaignStatus == GS1_CampaignStatus::Invalid)
            //continue;

        progress.setText(strfmt("Processing %1 of %2..", updateCount, total));
        progress.setCount(updateCount, 1);

        //smmCampaignSelection.gs1_ValidateLine();

        if (smmCampaignSelection.ContactPersonId == "274227")
            debug::assert(true);

        contactPerson = ContactPerson::find(smmCampaignSelection.ContactPersonId);

        if(contactPerson)
        {
            nodeSub = doc.createElement('Subscriber');
            nodeSubs.appendChild(nodeSub);

            /*apiKey,
                                                   _smmCampaignTable.GS1_CampaignListId,
                                                   contactPerson.Email,
                                                   contactPerson.Name,
                                                   contactPerson.ContactPersonId,
                                                   smmCampaignSelection.CampaignId,
                                                   _smmCampaignTable.CampaignGroupId);
                                                   */

            //ContactPerson Email
            nodeEmail = doc.createElement('EmailAddress'); //fieldstr(ContactPerson, Email));
            nodeEmail.appendChild(doc.createTextNode(contactPerson.Email));
            nodeSub.appendChild(nodeEmail);
            //ContactPerson Name
            nodeName = doc.createElement('Name'); //fieldstr(ContactPerson, Name));
            nodeName.appendChild(doc.createTextNode(contactPerson.Name));
            nodeSub.appendChild(nodeName);
            //CustomeFields
            //if(i==1)
            {
                nodeCustomFields = doc.createElement('CustomFields');
                nodeSub.appendChild(nodeCustomFields);
            }
            //CustomField
            nodeCustomField = doc.createElement('CustomField');
            nodeCustomFields.appendChild(nodeCustomField);
            //smmCampaignSelection CampaignId
            //nodeKey = doc.createElement(fieldstr(smmCampaignSelection, CampaignId));
            nodeKey = doc.createElement('Key');
            nodeKey.appendChild(doc.createTextNode(smmCampaignSelection.CampaignId));
            nodeCustomField.appendChild(nodeKey);
            //smmCampaignTable CampaignGroupId
            //nodeValue = doc.createElement(fieldstr(smmCampaignTable, CampaignGroupId));
            nodeValue = doc.createElement('Value');
            nodeValue.appendChild(doc.createTextNode(_smmCampaignTable.CampaignGroupId));
            nodeCustomField.appendChild(nodeValue);

            updateCount++;
        }
    }
    nodeReSub = doc.createElement('Resubscribe');
    nodeReSub.appendChild(doc.createTextNode('true'));
    nodeTable.appendChild(nodeReSub);

    nodeQueueSub = doc.createElement('QueueSubscriptionBasedAutoResponders');
    nodeQueueSub.appendChild(doc.createTextNode('false'));
    nodeTable.appendChild(nodeQueueSub);

    nodeRestartSub = doc.createElement('RestartSubscriptionBasedAutoresponders');
    nodeRestartSub.appendChild(doc.createTextNode('true'));
    nodeTable.appendChild(nodeRestartSub);

    doc.save(#filename);
    //return updateCount ? true : false;
    info(updatecount ? 'true' : 'false');
}

No comments:

Post a Comment