Tuesday, 24 December 2013

find On Hand through X++ in ax 2009

static void findOnHand(Args _args)
{ 
InventDim inventDim;
InventDimParm inventDimParm;
Itemid itemid;
InventOnHand inventOnHand = new InventOnHand();
;

// take a sample item for testing
itemid = "20 MM RMC";

// take a combination of dimension , against which you want to find the stock
inventDim.InventLocationId = "GW";

//Set the flag for the selected dimensions as active.
inventDimParm.initFromInventDim(inventDim);

//initialize the inventonhand with item,dimension and dim paramter
inventOnHand = InventOnHand::newParameters(itemid, inventDim, inventDimParm);

// Retrieve the onhand info
info(strfmt("Available Physical: %1", inventOnhand.availPhysical()));
info(strfmt("Physical Invent: %1", inventOnhand.physicalInvent()));
info(strfmt("On order: %1", inventOnhand.onOrder()));

}

Call one form to Other form in ax 2009 through X++

Button click:
void click()
{
    Args     args;
    Formrun  formrun;
    ;
    super();
    args =new Args(formstr(III_secondform))
    args.caller(element);
    args.record(III_customer);
    args.parm(org.text)
    formrun = classfactory.formrunclass(args);
    formrun.init();
    formrun.run();
    formrun.wait();
    formrun.detach();
}

second form int method :-

public void int()
{
   III_Customer  c;
   ;
   super();
   if(!element.args().caller())
   {
      throw error("form cannot be open directly");
   }
   c = element.args().record();
   name.text(c.custname);

}

Get Opening Balance in ax 2009 through X++

real  getOpeningBalance(LedgerAccount  _accountNum)
{

    DimensionCriteria   dimensionCriteriaEmpty;
    Transdate           periodEnd;
    transdate periodStart = LedgerPeriod::findOpeningDate(fromDate);
    LedgerBalanceSum_CurrentMST   ledgerBalance;
    ;
    periodEnd = fromDate - 1;

    ledgerBalance= new LedgerBalanceSum_CurrentMST(periodStart,
                                                   periodEnd,
                                                   dimensionCriteriaEmpty,
                                                   NoYes::Yes,
                                                   Noyes::Yes,
                                                   NoYes::Yes,
                                                   OperationsTax::Current,
                                                   DebCredProposal::None,
                                                   NoYes::Yes,
                                                   NoYes::Yes);
    return ledgerBalance.balance(_accountNum);


}

fld1_1_modified and Fld_7_lookup on the dilaog through X++ ax 2009

static void main(Args _args)
{
    Onhandwithdates   onhandwithdate;
    ;
    onhandwithdate    = new Onhandwithdates();
    if(onhandwithdate.prompt())
    {
        onhandwithdate.run();
    }
}

public Object dialog()
{

    ;
    dialog = super();

    dfitemid     =  dialog.addField(typeid(Itemid),'Itemid');
    dfitemid.fieldControl().mandatory(true);
    // dfitemid.lookupButton(FormLookupButton::Always);
    //dates
    dffromdate   =  dialog.addField(typeid(Transdate),'From date');
    dftodate     =  dialog.addField(typeid(Transdate),'To date');

    //Dimensions
    dfg = dialog.addGroup("Inventory Dimensions");

    dfsizeid  =  dialog.addField(typeid(Itemvalue),'Size');
    dfsizeid.lookupButton(FormLookupButton::Always);

    dfssiteid  =  dialog.addField(typeid(InventSiteId),'Site');
    dfwarehouse  =  dialog.addField(typeid(InventLocationId),'Warehouse');
    dflocationid  =  dialog.addField(typeid(wMSLocationId),'Location');
    return dialog;
}

public boolean getFromDialog()
{
    boolean ret1;
    ret1 = super();

    itemidloc  =   dfitemid.value();
    if( dfitemid.value() == '')
    {
       Throw error('Please fill itemid ');
    }
    fromdate   =   dffromdate.value();
    if((!fromdate) )
    {
        Throw error("Please fill the from date");
    }
    todate     =   dftodate.value();
    if((!todate) )
    {
        Throw error("Please fill the to date");
    }
     if(fromdate > todate || todate > today() )
    {
        Throw error("Please check the fromdate and todate");
    }

    sizeid     =   dfsizeid.value();
    locationid     =   dflocationid.value();
    siteid     =   dfssiteid.value();
    warehouse     =   dfwarehouse.value();
    return ret1;
}

public ClassDescription caption()
{
    ClassDescription retloc;
    retloc = 'Onhand Quantity between dates';
    return retloc;
}

public void dialogPostRun(DialogRunbase _dialog)
{
    ;
    Super(_dialog);
    _dialog.formRun().controlMethodOverload(true);
    _dialog.formRun().controlMethodOverloadObject(this);
}

public container pack()
{
    return [#CurrentVersion,#CurrentList];
}

public boolean unpack(container packedClass)
{
    Version version = RunBase::getVersion(packedClass);
;
    switch (version)
    {
        case #CurrentVersion:
            [version,#CurrentList] = packedClass;
            break;
        default:
            return false;
    }

    return true;
}

void Fld4_1_lookup()
{
    FormStringControl control = dialog.formRun().controlCallingMethod();
    SysTableLookup          sysTableLookup = SysTableLookup::newParameters(tableNum(Inventsize), control);
    Query                   query = new Query();
    ;
    sysTableLookup.addLookupField(fieldNum(Inventsize, itemid));
    sysTableLookup.addLookupField(fieldNum(Inventsize, InventSizeId));
    if(dfitemid.value())
    {
        query.addDataSource(tableNum(Inventsize)).addRange(fieldNum(Inventsize, itemid)).value(dfitemid.value());
    }
    //You can add the ranges for filtering
    sysTableLookup.parmQuery(query);
    sysTableLookup.performFormLookup();

}
.............................................................................completed-----------------------------------------------------------

class SamSelectiveLookup extends RunBase
{
    FormStringControl   custGroupCtrl, customerCtrl;
}
void Customer_lookup()
{
    Query                   query          = new Query();
    SysTableLookup          sysTableLookup = SysTableLookup::newParameters(tableNum(custTable), customerCtrl);
    ;

    sysTableLookup.addLookupField(fieldNum(custTable, accountNum));
    sysTableLookup.addLookupField(fieldNum(custTable, name));

    query.addDataSource(tableNum(custTable)).addRange(fieldNum(CustTable, CustGroup)).value(custGroupCtrl.text());
    sysTableLookup.parmQuery(query);
    sysTableLookup.performFormLookup();
}
protected Object dialog(Dialog dialog, boolean forceOnClient)
{
    DialogRunBase ret;

    ret = super(dialog, forceOnClient);

    custGroupCtrl = ret.formBuildDesign().addControl(FormControlType::String, 'CustGroup');
    custGroupCtrl.extendedDataType(103);

    customerCtrl = ret.formBuildDesign().addControl(FormControlType::String, 'Customer');
    customerCtrl.extendedDataType(99);

    return ret;
}

public void dialogPostRun(DialogRunbase dialog)
{
    ;
    super(dialog);

    dialog.dialogForm().formRun().controlMethodOverload(true);
    dialog.dialogForm().formRun().controlMethodOverloadObject(this);

    custGroupCtrl = dialog.dialogForm().formRun().design().controlName('CustGroup');
    customerCtrl = dialog.dialogForm().formRun().design().controlName('Customer');
}

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

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

static void main(Args _args)
{
    SamSelectiveLookup test1 = new SamSelectiveLookup();
    ;

    if (test1.prompt())
    {
        test1.run();
    }
}

public void run()
{
    try
    {
        ttsbegin;


        ttscommit;
    }
    catch (exception::DeadLock)
    {
        retry;
    }

}
-------------------------------------------------------------------------
public void dialogPostRun(DialogRunbase _dialog)
{
    super(_dialog);

    _dialog.dialogForm().formRun().controlMethodOverload(true);
    _dialog.dialogForm().formRun().controlMethodOverloadObject(this);
}

Note, that in the code above the RunBase class itself is used as event handler (by passing this to the controlMethodOverloadObject() method), but it is possible to create a separate class for handling events.

Afterwards event handlers can be created as method with specific name: _. Control name can be found in the dialog user setup. For example, the following method will handle modify event of the first field control on the dialog:
public void fld1_1_modified()
{
    Object control;

    control = dialog.formRun().controlCallingMethod();

    if(control.modified())
    {
        info(“Modified triggered!”);
    }
 
public Object dialog(DialogRunbase _dialog, boolean _forceOnClient)
{
    DialogRunBase   dialog;
    ;

    dialog = super(_dialog, true);

    dialog.caption('@RID2885');

    dfItem = dialog.addField(typeid(ItemId));
    dfInventLoc = dialog.addField(typeid(InventLocationId));
    dfReplaceCost = dialog.addField(typeid(PdsCost));
    dfItemConfig = dialog.addField(typeid(ConfigId));
    dfColorId = dialog.addField(typeid(InventColorId), '@RID101');

    return dialog;
}
void Fld_7_lookup()

{

    Formrun fr = this.dialogModify().parmDialog();

    Object control = fr.controlCallingMethod();    

    ;

    ConfigTable::lookupConfigIdSimple(control, dfItem.value());    

}
public void dialogPostRun(DialogRunbase _dialog)

{

    ;

     super(_dialog);

    **_dialog.formRun().controlMethodOverload(true);** // Causes Stack Trace error

    _dialog.formRun().controlMethodOverloadObject(this);       

}


Mayby you should call super(_dialog) last in the dialogPostRun method.
Here is an example code for overriding the modified method. Maybe lookup has the same requirements:
public void dialogPostRun(DialogRunbase _dialog)
{
// Must be overriden to enable overriding modified method
;
    _dialog.dialogForm().formRun().controlMethodOverload(true);
    _dialog.dialogForm().formRun().controlMethodOverloadObject(this);
    _dialog.formRun().controlMethodOverload(true);
    _dialog.formRun().controlMethodOverloadObject(this);

    super(_dialog);
}
And for the custom method:
boolean Fld2_1_modified()
{
    FormStringControl c = dialog.formrun().controlCallingMethod();
    boolean ret;
    ;

    ret = c.modified(); // Super() Call the FormControl ->modified

    dlgCustomField.value(MyClass::someMethod(dlgCustomField.value())); // example

    return ret;
}
public Object dialog(DialogRunbase _dialog, boolean _forceOnClient)
{
    DialogRunBase   dialog;
    ;

    dialog = super(_dialog, true);

    dialog.caption('@RID2885');

    dfItem = dialog.addField(typeid(ItemId));
    dfInventLoc = dialog.addField(typeid(InventLocationId));
    dfReplaceCost = dialog.addField(typeid(PdsCost));
    dfItemConfig = dialog.addField(typeid(ConfigId));
    dfColorId = dialog.addField(typeid(InventColorId), '@RID101');

    return dialog;
}
Here's the call to the lookup():
void Fld_7_lookup()
{
    Formrun fr = this.dialogModify().parmDialog();
    Object control = fr.controlCallingMethod();
    ;

    ConfigTable::lookupConfigIdSimple(control, dfItem.value());
}
And this is where it keeps getting the Stack Trace error:
public void dialogPostRun(DialogRunbase _dialog)
{
    ;
     super(_dialog);
    **_dialog.formRun().controlMethodOverload(true);** // Causes Stack Trace error
    _dialog.formRun().controlMethodOverloadObject(this);  
}
Mayby you should call super(_dialog) last in the dialogPostRun method.

Here is an example code for overriding the modified method. Maybe lookup has the same requirements:
public void dialogPostRun(DialogRunbase _dialog)
{
// Must be overriden to enable overriding modified method
;
    _dialog.dialogForm().formRun().controlMethodOverload(true);
    _dialog.dialogForm().formRun().controlMethodOverloadObject(this);
    _dialog.formRun().controlMethodOverload(true);
    _dialog.formRun().controlMethodOverloadObject(this);

    super(_dialog);
}
And for the custom method:
boolean Fld2_1_modified()
{
    FormStringControl c = dialog.formrun().controlCallingMethod();
    boolean ret;
    ;

    ret = c.modified(); // Super() Call the FormControl ->modified

    dlgCustomField.value(MyClass::someMethod(dlgCustomField.value())); // example

    return ret;
}
public void dialogPostRun(DialogRunbase _dialog)
{
    super(_dialog);

    _dialog.dialogForm().formRun().controlMethodOverload(true);
    _dialog.dialogForm().formRun().controlMethodOverloadObject(this);
}

Note, that in the code above the RunBase class itself is used as event handler (by passing this to the controlMethodOverloadObject() method), but it is possible to create a separate class for handling events.

Afterwards event handlers can be created as method with specific name: _. Control name can be found in the dialog user setup. For example, the following method will handle modify event of the first field control on the dialog:
public void fld1_1_modified()
{
    Object control;

    control = dialog.formRun().controlCallingMethod();

    if(control.modified())
    {
        info(“Modified triggered!”);
    }

Access rights to SharePoint in ax 2012

access to SharePoint
1. Open Internet Explorer
2. Click Site Actions > Site Permissions
3. Click Grant Permissions
4. In the Users\Groups box, enter Tony
5. Click Check Names
6. Check Full Control
7. Click OK

Create Purchase Order_and_Invoice in ax 2012

static void PurchOrderCreate(Args _args)
{
NumberSeq numberSeq;
PurchTable purchTable;
PurchLine purchLine;
ttsBegin;
numberSeq = NumberSeq::newGetNum(PurchParameters::numRefPurchId());
numberSeq.used();
purchTable.PurchId = numberSeq.num();
purchTable.initValue();
purchTable.initFromVendTable(VendTable::find('1001'));
if (!purchTable.validateWrite())
{
throw Exception::Error;
}
purchTable.insert();
purchLine.PurchId = purchTable.PurchId;
purchLine.ItemId = '1205';
purchLine.createLine(true, true, true, true, true, true);
ttsCommit;
info(strFmt(
"Purchase order '%1' has been created",
purchTable.PurchId));
}
..............................................
static void PurchOrderPost(Args _args)
{
PurchFormLetter purchFormLetter;
PurchTable purchTable;
purchTable = PurchTable::find('000409');
purchFormLetter = PurchFormLetter::construct(
DocumentStatus::PurchaseOrder);
purchFormLetter.update(
purchTable,
'',
systemDateGet(),
PurchUpdate::All,
AccountOrder::None,
NoYes::No,
NoYes::Yes);
}

---------------------------------------------------------------------------------
static void Dev_CreatePO_and_Invoice(Args _args)
{
NumberSeq numberSeq;
Purchtable Purchtable;
PurchLine PurchLine;
PurchFormLetter purchFormLetter;
;
ttsbegin;
numberSeq = NumberSeq::newGetNumFromCode(purchParameters::numRefPurchaseOrderId().NumberSequence,true);
// Initialize Purchase order values
Purchtable.initValue();
Purchtable.PurchId = numberSeq.num();
Purchtable.OrderAccount = '3000';
Purchtable.initFromVendTable();
if (!Purchtable.validateWrite())
{
throw Exception::Error;
}
Purchtable.insert();
// Initialize Purchase Line items
PurchLine.PurchId = Purchtable.PurchId;
PurchLine.ItemId = 'B-R14';
PurchLine.createLine(true, true, true, true, true, false);
ttscommit;
purchFormLetter = purchFormLetter::construct(DocumentStatus::Invoice);
purchFormLetter.update(purchtable, // Purchase record Buffer
"Inv_"+purchTable.PurchId, // Invoice Number
systemdateget()); // Transaction date
if (PurchTable::find(purchTable.PurchId).DocumentStatus == DocumentStatus::Invoice)
{
info(strfmt("Posted invoiced journal for purchase order %1",purchTable.PurchId));
}
}


Create and posting Sales order through X++ in ax 2012

static void SalesOrderCreate(Args _args)
{
NumberSeq numberSeq;
SalesTable salesTable;
SalesLine salesLine;
ttsBegin;
numberSeq = NumberSeq::newGetNum(SalesParameters::numRefSalesId());
numberSeq.used();
salesTable.SalesId = numberSeq.num();
salesTable.initValue();
salesTable.CustAccount = '1101';
salesTable.initFromCustTable();
if (!salesTable.validateWrite())
{
throw Exception::Error;
}
salesTable.insert();
salesLine.SalesId = salesTable.SalesId;
salesLine.ItemId = '1205';
salesLine.createLine(true, true, true, true, true, true);
ttsCommit;
info(strFmt(
"Sales order '%1' has been created", salesTable.SalesId));
}

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

(replace SO-101248 with your number):
static void SalesOrderPost(Args _args)
{
SalesFormLetter salesFormLetter;
salesTable salesTable;
salesTable = SalesTable::find('SO-101248');
salesFormLetter = SalesFormLetter::construct(
DocumentStatus::PackingSlip);
salesFormLetter.update(
salesTable,
systemDateGet(),
SalesUpdate::All,
AccountOrder::None,
NoYes::No,
NoYes::Yes);
}

-------------------------------------------------------------------------------
static void createSalesTable(CustAccount _custAccount)
{
SalesTable salesTable;
NumberSeq NumberSeq;
;
NumberSeq = NumberSeq::newGetNumFromCode(SalesParameters::numRefSalesId().numberSequence);
salesTable.SalesId = NumberSeq.num();
salesTable.initValue();
salesTable.CustAccount = _custAccount;
salesTable.initFromCustTable();
salesTable.insert();
}
Example: Create a Sales Line
static void createSalesLine(SalesId _salesId, ItemId _itemId)
{
SalesLine salesLine;
;
salesLine.clear();
salesLine.SalesId = _salesId;
salesLine.ItemId = _itemId;
salesLine.createLine(NoYes::Yes, // Validate
NoYes::Yes, // initFromSalesTable
NoYes::Yes, // initFromInventTable
NoYes::Yes, // calcInventQty
NoYes::Yes, // searchMarkup
NoYes::Yes); // searchPrice
}
//Code for posting Sales order Invoice
static void createSalesOrder(Args _args)
{
SalesFormLetter formLetterObj;
formLetterObj = SalesFormLetter::construct(DocumentStatus::Invoice);
formLetterObj.update(SalesTable::find( gSO-101248  ));
}