Showing posts with label Import User Mail ids in Ax 2012. Show all posts
Showing posts with label Import User Mail ids in Ax 2012. Show all posts

Friday, 14 September 2018

Import User Mail ids in Ax 2012

static void  Na_AlertUserEmailImporting(Args _args)
{
    Dialog                                    dialog;
    Dialogfield                             dialogfield;

    SysExcelApplication             application;
    SysExcelWorkbooks              workBooks;
    SysExcelWorkbook               workBook;
    SysExcelWorksheets             workSheets;
    SysExcelWorksheet              workSheet;
    SysExcelCells                       cells;

    UserInfo                                  userInfo;
    SysUserInfo                             sysUserInfo;

    LogisticsElectronicAddress      logisticsElectronicAddress;
    LogisticsLocation                     logisticsLocation;
    HcmWorker                              hcmWorker;
    DirPerson                                 dirPerson;
    DirPartyTable                           dirPartyTable;
    DirPartyContactInfoView        contactView;
    DirParty                                   dirParty;

    DirPersonRecId                      dirPersonRecid;
    DirPersonUser                        personUser;

    Email                                      usermail;
    Name                                     Userid;
    Filename                                fileName;
    COMVariantType                  type;
    int                                          row = 1   ;
    int                                         recordcnt;

    str COMVariant2Str(COMVariant _cv, int _decimals = 0,int _characters = 0,int _separator1 = 0,int _separator2 = 0)
        {
            switch(_cv.variantType())
            {
                case (COMVariantType::VT_BSTR):
                    return _cv.bStr();
                case (COMVariantType::VT_R4):
                    return num2str(_cv.float(),_characters,_decimals, _separator1,_separator2);
                case (COMVariantType::VT_R8):
                    return num2str(_cv.double(),_characters,_decimals,_separator1,_separator2);
                case (COMVariantType::VT_DECIMAL):
                    return num2str(_cv.decimal(),_characters,_decimals, _separator1, _separator2);
                case (COMVariantType::VT_DATE):
                    return date2str(_cv.date(),123,2,1,2, 1,4);
                case (COMVariantType::VT_EMPTY):
                    return "";
                default:
                    throw error(strfmt("@SYS26908",_cv.variantType()));
            }
            return "";
        }


    application =   SysExcelApplication::construct();
    workBooks   =   application.workbooks();
    dialog      = new Dialog("FileOpen");
    dialogfield = dialog.addField(extendedTypeStr(Filenameopen), "File Name");
    dialog.run();

    if (dialog.run())
    {
        filename = (dialogfield.value());
    }
    try
    {
        workBooks.open(fileName);
    }
    catch (Exception::Error)
    {
        throw error("File Cannot be opened");
    }

    workBook    =   workBooks.item(1);
    workSheets  =   workBook.worksheets();
    workSheet   =   workSheets.itemFromNum(1);
    cells       =   workSheet.cells();
    do
    {
        row++;
        Userid          = cells.item(row, 1).value().bStr();
        usermail        = COMVariant2Str(cells.item(row, 2).value());

        select id,name from userInfo where userInfo.id == Userid
                join id from sysUserInfo where sysUserInfo.Id == userInfo.id;

        if(userInfo.Id)
        {
            select forUpdate sysUserInfo where sysUserInfo.id == userInfo.id;
            ttsBegin;
            sysUserInfo.Email = usermail;
            sysUserInfo.update();
            ttsCommit;
            select * from hcmWorker
                exists join personUser
                    where hcmWorker.Person == personUser.PersonParty
                       && personUser.User == userInfo.id;
            if(hcmWorker.RecId)
            {
                dirPersonRecid   = hcmWorker.Person;
                DirParty = DirParty::constructFromPartyRecId(dirPersonRecid);

                contactView.clear();
                contactView.LocationName                =   userInfo.name;
                contactView.Locator                            =   usermail;
                contactView.Type                                =    LogisticsElectronicAddressMethodType::Email;
                contactView.Party                                =    dirPersonRecid;
                contactView.IsPrimary                         =    NoYes::No;
                contactView.IsPrivate                           =   NoYes::Yes;
                dirParty.createOrUpdateContactInfo(contactView);
                info(strFmt("User-%1 Email- %2 is successfully Updated",userInfo.id,usermail));
            }
            else
            {
                info(strFmt("User-%1 Email- %2 is not Updated in worker",userInfo.id,usermail));
            }


        }
        type = cells.item(row+1, 1).value().variantType();
    }
    while (type != COMVariantType::VT_EMPTY);
    application.quit();
    workbooks.close();
}