Thursday 26 June 2014

Run base batch in ax 2009

class ListBatch extends RunBaseBatch
{
    transdate  fromdate,Todate;
}

public container pack()
{
   return conNull();
}
public boolean unpack(container packedClass)
{
   return true;
}

server static ListBatch construct()
{
    return new ListBatch();
}

static ClassDescription description()
{
    return "Inquiry Batch";
}

public static void main(Args _args)
{
    ListBatch    listBatch;
    ;
    listBatch=  ListBatch::construct();

    if (listBatch.prompt())
        listBatch.run();
}
public void run()
{
    int                         i,j;
    TMLCartonInfo   TMLCartonInfo,TMLCartonInfolocal1,TMLCartonInfosmall;
    transdate              Datevalue;
    utcdatetime           formdate1 ,Todate1;
    int                         counter;
    date                      dateInUserTimeZone;
    TMLCartonInfo   TMLCartonInfolocal;
    timeOfDay            time1,time2;
    utcdatetime           fromshiftdate1,toshiftdate1;
    ;

    select firstonly TMLCartonInfolocal1 order by PackDateTime asc;
    fromdate      = DateTimeUtil::date(applyUserTimeZone(TMLCartonInfolocal1.PackDateTime));
    select firstonly TMLCartonInfosmall order by PackDateTime desc;
    Todate        = DateTimeUtil::date(applyUserTimeZone(TMLCartonInfosmall.PackDateTime));

    time1         = str2time( "00:00:00" );
    time2         = str2time( "23:59:59" );
    formdate1 = DateTimeUtil ::newDateTime( fromdate, time1);
    Todate1   = DateTimeUtil ::newDateTime( Todate, time2);

    fromshiftdate1 = DateTimeUtil::addDays(DateTimeUtil::newDateTime(fromdate, time1),-1);
    toshiftdate1   = DateTimeUtil::addDays(DateTimeUtil::newDateTime(Todate, time2),1);

        while SELECT * FROM TMLCartonInfo index hint SerialIdX order by PackDateTime asc where TMLCartonInfo.PackDateTime >= fromshiftdate1
                                                                      &&  TMLCartonInfo.PackDateTime <= toshiftdate1
                                                                      &&  TMLCartonInfo.PackDate == Datevalue
        {
            if(applyUserTimeZone( TMLCartonInfo.PackDateTime) >= formdate1 && applyUserTimeZone(TMLCartonInfo.PackDateTime)<= Todate1 )
            {
                dateInUserTimeZone = DateTimeUtil::date(applyUserTimeZone( TMLCartonInfo.PackDateTime));


                if(!TMLCartonInfo.PackDate)
                {
                    counter++;
                    //info(strFmt('%1-%2',dateInUserTimeZone,TMLCartonInfo.RecId));
                    ttsBegin;
                    select forupdate TMLCartonInfolocal index hint SerialIdX  where TMLCartonInfolocal.RecId == TMLCartonInfo.RecId;
                    TMLCartonInfolocal.PackDate = dateInUserTimeZone;
                    TMLCartonInfolocal.doUpdate();
                    ttsCommit;
                }
                else
                {
                    break;
                }
            }
        }
        info(strFmt('%1',counter));
}
public boolean runsImpersonated()
{
    // false means that the batch must run on a client.
    return true;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class EmailsendWithAttachmentBatch extends RunBaseBatch
{
 
    TransDate                       date1;
}
public container pack()
{
   return conNull();
}

public void run()
{
    str                         messageBody;
    InteropPermission           permission = new InteropPermission(InteropKind::ComInterop);
    SysMailer                   mailer;
    str                         Body;
    str                         ToAddress;
    str                         FromAddress = "aslam.p@infotech.net";
    str                         Subject = "Alerts";
    str                         pdfFileName;
    ;

    pdfFileName = strfmt('C:\\Users\\adminsystem\\Desktop\\A.txt'); //Create file in server system or share folder
    messageBody = "Test";
    toaddress   =  "aslam.p@infotech.net";
    Body = strfmt("%1", messageBody) ;
    CodeAccessPermission::revertAssert();
    permission.assert();
    mailer = new SysMailer();
    mailer.quickSend(fromaddress,toaddress,subject,Body,"",pdfFileName);
}

public boolean unpack(container packedClass)
{
   return true;
}

server static EmailsendWithAttachmentBatch construct()
{
    return new  EmailsendWithAttachmentBatch();
}

// Here goes a description of the class
static ClassDescription description()
{
    return "@SYS70984";
}

static void main(Args args)
{
    EmailsendWithAttachmentBatch emailsendWithAttachment;
;
    emailsendWithAttachment= EmailsendWithAttachmentBatch::construct();

    if (emailsendWithAttachment.prompt())
        emailsendWithAttachment.run();
}

https://community.dynamics.com/ax/f/33/t/78511.aspx

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public static void scheduleBatch()
{
    BatchHeader           batchHeader;
    BatchInfo             localBatchInfo;
    YourRunBaseBatchClass yourRunBaseBatchClass;
    SysRecurrenceData     sysRecurrenceData =
             SysRecurrence::defaultRecurrence();
    ;

    yourRunBaseBatchClass =
        YourRunBaseBatchClass::construct();

    // retry 3 times
    sysRecurrenceData =
        SysRecurrence::setRecurrenceEndAfter(
                        sysRecurrenceData, 3);
    // retry after 1 minute
    sysRecurrenceData =
        SysRecurrence::setRecurrenceUnit(sysRecurrenceData,
                             SysRecurrenceUnit::Minute, 1);
 
    localBatchInfo = yourRunBaseBatchClass.batchinfo();
    localBatchInfo.parmGroupId("YourBatchGroupId");
    batchHeader = batchHeader::construct();
    batchHeader.addTask(yourRunBaseBatchClass);
    batchHeader.parmRecurrenceData(sysRecurrenceData);
    batchHeader.save();
}

.........................................................................................................................................
static void SR_SetupServerBatchJobs(Args _args)
{
BatchHeader header;
SysRecurrenceData sysRecurrenceData;
Batch batch;
BatchJob batchJob;
Tutorial_RunBaseBatch tutorial_rbb;
BatchInfo processBatchInfo;
BatchRetries noOfRetriesOnFailure = 4;
;

// Create the tutorial_RunBaseBatch job, only if one does not exist
select batch where batch.ClassNumber == classnum(Tutorial_RunBaseBatch);
if(!batch)
{
// Setup the tutorial_RunBaseBatch Job
header = BatchHeader::construct();
tutorial_rbb = Tutorial_RunbaseBatch::construct();
processBatchInfo = tutorial_rbb.batchInfo();
processBatchInfo.parmRetriesOnFailure(noOfRetriesOnFailure);
header.addTask(tutorial_rbb);

// Set the recurrence data
sysRecurrenceData = SysRecurrence::defaultRecurrence();
SysRecurrence::setRecurrenceStartDateTime(sysRecurrenceData, DateTimeUtil::addSeconds(DateTimeUtil::utcNow(), 20));
SysRecurrence::setRecurrenceNoEnd(sysRecurrenceData);
SysRecurrence::setRecurrenceUnit(sysRecurrenceData, SysRecurrenceUnit::Minute);
header.parmRecurrenceData(sysRecurrenceData);
// Set the batch alert configurations
header.parmAlerts(NoYes::No, NoYes::Yes, NoYes::No, NoYes::Yes, NoYes::Yes);
header.save();

// Update the frequency to run the job to every two minutes
ttsbegin;
select forupdate batchJob
join batch
where batchJob.RecId == batch.BatchJobId
&& batch.ClassNumber == classnum(Tutorial_RunBaseBatch);

sysRecurrenceData = batchJob.RecurrenceData;
sysRecurrenceData = conpoke(sysRecurrenceData, 8, [2]);
batchJob.RecurrenceData = sysRecurrenceData;
batchJob.update();
ttscommit;
}

}

.........................................................................................................................................
class Test_runbase extends RunBaseBatch
{
     #define.CurrentVersion(1)
    #define.Version1(1)
    #localmacro.CurrentList

    #endmacro
}

public boolean canGoBatchJournal()
{
    return true;
}

public container pack()
{
    return conNull();
}

public boolean unpack(container packedClass)
{
 
    return true;
}

static ClassDescription description()
{
    return "Test posting";
}

public static void main(Args _args)
{
    Test_runbase    test_runbase= new Test_runbase();
    ;
    if(test_runbase.prompt())
        test_runbase.run();

}

public void run()
{
    LedgerJournalTable      jourTable;
    LedgerJournalTrans      jourTrans;
    LedgerJournalTableData  jourTableData;
    LedgerJournalTransData  jourTransData;
    LedgerJournalStatic     jourStatic;
    DimensionDynamicAccount ledgerDim;
    DimensionDynamicAccount offsetLedgerDim;
    LedgerjournalCheckPost  LedgerjournalCheckPost;
    CustReceipts            custreceipts;
    ;

    //info("Batch starting");
    while select forupdate custreceipts
      where custreceipts.Posted == NoYes::No
    {
        ttsBegin;
        ledgerDim = DimensionStorage::getDynamicAccount(custreceipts.CustId,LedgerJournalACType::Cust);
        offsetLedgerDim = DimensionStorage::getDynamicAccount("USA OPER",LedgerJournalACType::Bank);
        jourTableData = JournalTableData::newTable(jourTable);
        jourTable.JournalNum = jourTableData.nextJournalId();
        jourTable.JournalType = LedgerJournalType::Payment;
        jourTable.JournalName = 'ARPay';
        jourTableData.initFromJournalName(LedgerJournalName::find(jourTable.JournalName));
        jourStatic = jourTableData.journalStatic();
        jourTransData = jourStatic.newJournalTransData(jourTrans,jourTableData);
        jourTransData.initFromJournalTable();
        jourTrans.CurrencyCode = 'USD';
        jourTrans.initValue();
        jourTrans.TransDate = systemDateGet();
        jourTrans.AccountType = LedgerJournalACType::Cust;
        jourTrans.LedgerDimension = ledgerDim;
        //jourTrans.Txt = TransTxt.valueStr();
        jourTrans.OffsetAccountType = LedgerJournalACType::Bank;
        jourTrans.OffsetLedgerDimension = offsetLedgerDim;
        jourTrans.AmountCurCredit = any2real(custreceipts.Amount);
        jourTransData.create();
        jourTable.insert();
        ttsCommit;


        LedgerjournalCheckPost      =   LedgerjournalCheckPost::newLedgerJournalTable(jourTable,NoYes::Yes);
        LedgerjournalCheckPost.run();
        info(strFmt("Journal '%1' has been created", jourTable.JournalNum));

         ttsBegin;
        custreceipts.Posted = NoYes::Yes;
        custreceipts.doUpdate();
        ttsCommit;
        Info("posted");

    }
}
public boolean runsImpersonated()
{
    // false means that the batch must run on a client.
    return true;
}
.............................................................................................................................
Class Runbasebatch extends RunBaseBatch
{
    RunBase_UpdateTable             rBUpdateTable;
    TransDate                       date1;
}

public container pack()
{
   return conNull();
}

public void run()
{
    //super();
 date   dateInUserTimeZone;
    ;
    ttsBegin;


    while select forupdate rBUpdateTable
    {
        dateInUserTimeZone = DateTimeUtil::date(applyUserTimeZone(rBUpdateTable.ValidDateTime));
        rBUpdateTable.NewDate = dateInUserTimeZone; //DateTimeUtil::date(applyUserTimeZone(rBUpdateTable.ValidDateTime));
        rBUpdateTable.update();
    }

    ttsCommit;


}

public boolean runsImpersonated()
{
    // false means that the batch must run on a client.
    return true;
}

public boolean unpack(container packedClass)
{
   return true;
}

server static Runbasebatch construct()
{
    return new Runbasebatch();
}

static void main(Args args)
{
    Runbasebatch    runbasebatch;
;
    runbasebatch=  Runbasebatch::construct();

    if (runbasebatch.prompt())
        runbasebatch.run();
}

No comments:

Post a Comment