Tuesday, 17 April 2018

Account Payable settle payment with invoice x++

Hello Guys..!

Here I'm sharing code for Settle Vendor Invoice/Payment through X++  

Code:
-----------
static void VendorSettlement(Args _args)
{
VendTable vendTable;
VendTrans invVendTrans, payVendTrans;
SpecTransManager manager;
CustVendTransData custVendTransData;
;
vendTable = VendTable::find("12345");
// Find the oldest unsettled invoice
select firstonly invVendTrans  order by TransDate asc
                            where invVendTrans.AccountNum == vendTable.AccountNum &&
                            invVendTrans.TransType == LedgerTransType::Purch &&
                            !invVendTrans.closed;
// Find the oldest unsettled payment
select firstonly payVendTrans order by TransDate asc
                    where payVendTrans.AccountNum == vendTable.AccountNum &&
                    payVendTrans.TransType == LedgerTransType::Payment &&
                    !payVendTrans.closed;
ttsbegin;
// Create an object of the CustVendTransData class with the invoice transaction as parameter
custVendTransData = CustVendTransData::construct(invVendTrans);
// Mark it for settlement
custVendTransData.markForSettlement(vendTable);
// Create an object of the CustVendTransData class with the payment transaction as parameter
custVendTransData = CustVendTransData::construct(payVendTrans);
//mark it for settlement
custVendTransData.markForSettlement(vendTable);
ttscommit;
// Settle all marked transactions
if(VendTrans::settleTransact(vendTable, null, true,SettleDatePrinc::DaysDate, systemdateget()))
info("Transactions settled");
}

reference Click Here

Account Payable create a payment journal x++

  LedgerJournalTable jourTable;
    LedgerJournalTrans jourTrans;
    LedgerJournalTableData jourTableData;
    JournalTransData jourTransData;
    LedgerJournalStatic jourStatic;
    DimensionDynamicAccount ledgerDim;
    DimensionDynamicAccount offsetLedgerDim;
    NumberSeq numberseq;

ttsBegin;
    ledgerDim = DimensionStorage::getDynamicAccount('29901-00162',LedgerJournalACType::Vend);
    offsetLedgerDim = AxdDimensionUtil::getLedgerAccountId(['1213011','1213011',1,'Vendor','29901-00162']); //DimensionStorage::getDynamicAccount('NBE.EGP',LedgerJournalACType::Bank);

    jourTableData = JournalTableData::newTable(jourTable);
    jourTable.JournalNum = jourTableData.nextJournalId();
    jourTable.JournalType = LedgerJournalType::Payment;
    jourTable.JournalName = '207';
    jourTableData.initFromJournalName(
    LedgerJournalName::find(jourTable.JournalName));
    jourStatic = jourTableData.journalStatic();
    jourTransData = jourStatic.newJournalTransData(
    jourTrans,
    jourTableData);
    jourTransData.initFromJournalTable();

    jourTrans.CurrencyCode ='EGP';
    jourTrans.initValue();
   numberseq = NumberSeq::NewGetVoucherFromCode('207');
    jourTrans.TransDate = systemDateGet();
    jourTrans.AccountType = LedgerJournalACType::Vend;
    jourTrans.LedgerDimension = ledgerDim;
    jourTrans.Txt = 'Vendor payment journal demo';
    jourTrans.OffsetAccountType = LedgerJournalACType::Ledger;
    jourTrans.OffsetLedgerDimension = offsetLedgerDim;
    jourTrans.AmountCurDebit = 1000;
    //jourTransData.create();
    jourTable.insert();
    jourTransData.insert();
    ttsCommit;
    info(strFmt(
    "Journal '%1' has been created", jourTable.JournalNum));
    ttsCommit;

Tuesday, 26 December 2017

X++ Run an SRS report through code


Here below run an SRS reort through code and add parameters to filter

 SrsReportRunController  controller;
     Map queryContracts;
     Query query;
     QueryBuildRange range;
     MapEnumerator mapEnum;
     controller = new SrsReportRunController();
 


    controller.parmReportName('GGPaymentJournal.GM_AR_Payment');
 
    queryContracts = controller.parmReportContract().parmQueryContracts();
    mapEnum = queryContracts.getEnumerator();
 
    while (mapEnum.moveNext())
    {
        query = mapEnum.currentValue();
        range = SysQuery::findOrCreateRange(query.dataSourceTable(tablenum(LedgerJournalTrans)),fieldNum(LedgerJournalTrans,JournalNum));
        range.value('20000-0021');
    }
 
    //controller.startOperation();
    controller.runReport();

Sunday, 25 December 2016

Transaction Log - AX 2012 - Cannot create a record in Audit trail (TransactionLog)

 If you got the following error you might have for problems in the system sequence table.


 Just run the following in SSMS:



select * from SYSTEMSEQUENCES where id = -2

 and then

select * from TRANSACTIONLOG where CREATEDTRANSACTIONID > returnedID order by CREATEDTRANSACTIONID asc


 If you have any records returned by the second statement then you should look into why you have a lower system number saved.


To over come this issue, please find the below code modifications in Tables-->TransactionLog-->Methods-->create.
The existing Code:

if (appl.lastTransactionIdCreated() != appl.curTransactionId())
{
transactionLog.Type = _type;
transactionLog.Txt = _txt;

transactionLog.insert();

appl.lastTransactionIdCreated(transactionLog.CreatedTransactionId);

appl.transactionlogUpdateTTSControl().revoke();
}



The changes to make: 

if (appl.lastTransactionIdCreated() != appl.curTransactionId())
{
if (!TransactionLog::find(appl.curTransactionId()))
{
transactionLog.Type = _type;
transactionLog.Txt = _txt;

transactionLog.insert();

appl.lastTransactionIdCreated(transactionLog.CreatedTransactionId);
}
appl.transactionlogUpdateTTSControl().revoke();
}

http://axfaq.blogspot.com.eg/2013/09/transaction-log-ax-2012-cannot-create.html

Wednesday, 27 July 2016

Axapta 2012 set financial dimension x++

 InventTable _InventTable; 
    RecId _RecId;   
    DimensionAttributeValueSetStorage  dimStorage;
    Counter               i;
    DimensionAttribute         _dimAttribute;
    DimensionAttributeValue       _dimAttributeValue;
   
    
  while  select forUpdate _InventTable where _InventTable.ProdPoolId == '0101'
    {
    _RecId = _InventTable.RecId;
    
      dimStorage = DimensionAttributeValueSetStorage::find(_RecId);
    
     _dimAttribute = DimensionAttribute::findByName("AF_Department",false);
     _dimAttributeValue = DimensionAttributeValue::findByDimensionAttributeAndValue(_dimAttribute,"501",true,true);
     dimStorage.addItem(_dimAttributeValue);
      
     _dimAttribute = DimensionAttribute::findByName("AF_Product_Section",false);
     _dimAttributeValue = DimensionAttributeValue::findByDimensionAttributeAndValue(_dimAttribute,"01",true,true);
     dimStorage.addItem(_dimAttributeValue);    
        
     _dimAttribute = DimensionAttribute::findByName("AF_Product_Line",false);
     _dimAttributeValue = DimensionAttributeValue::findByDimensionAttributeAndValue(_dimAttribute,"0111",true,true);
     dimStorage.addItem(_dimAttributeValue);     
        
    _InventTable.DefaultDimension = dimStorage.save();
    _InventTable.update();
    info('Done ' + _InventTable.ItemId );
    }
    

Sunday, 24 July 2016

AX 2012 The simplest way to lookup the financial dimension in a form

In this article, giving an example for defining the lookup for dimensions in simple way.
This example will cover only for user Defined dimensions.
For this
1)      Created a form and added two string controls.
2)      Named as
Control: Name: Dimensionlist, Auto declaration – Yes
Control: Name: DimValues
3)      Override the lookup method for Dimensionlist

public void lookup()
{
Query           query;
SysTableLookup  sysTableLookup;
super();
sysTableLookup = SysTableLookup::newParameters(tableNum(DimensionAttribute), this);
sysTableLookup.addLookupfield(fieldNum(DimensionAttribute, Name));
query = new Query();
query.addDataSource(tableNum(DimensionAttribute)).
addRange(fieldNum(DimensionAttribute, Type)).
value(queryValue(DimensionAttributeType::CustomList));
sysTableLookup.parmQuery(query);
sysTableLookup.performFormLookup();
}

------------------------------------------------------------------------------

4)      Override the lookup method for DimValues
public void lookup()
{
DimensionAttribute                  dimensionAttribute;
DimensionAttributeDirCategory       dimAttributeDirCategory;
Query                               query = new Query();
SysTableLookup                      sysTableLookup;
dimensionAttribute = DimensionAttribute::findByName(Dimensionlist.text());
super();
// Only user defined dimensions will have a financial tag category
if (dimensionAttribute.Type == DimensionAttributeType::CustomList)
{
select firstonly DirCategory from dimAttributeDirCategory where dimAttributeDirCategory.DimensionAttribute == dimensionAttribute.RecId;
sysTableLookup = SysTableLookup::newParameters(tableNum(DimensionFinancialTag), this);
// Add name field to be shown in the lookup form.
sysTableLookup.addLookupfield(fieldNum(DimensionFinancialTag, Value));
sysTableLookup.addLookupfield(fieldNum(DimensionFinancialTag, Description));
query = new Query();
query.addDataSource(tableNum(DimensionFinancialTag)).
addRange(fieldNum(DimensionFinancialTag, FinancialTagCategory)).
value(queryValue(dimAttributeDirCategory.DirCategory));
sysTableLookup.parmQuery(query);
// Perform the lookup.
sysTableLookup.performFormLookup();
}
}

Monday, 18 July 2016

Read XML string (elements, attributes, texts) [AX2012]

static void readXMLString(Args _args)
{
    // Define XML Document and its nodes
    XmlDocument     doc;
    XmlNodeList     xmlScriptList;
    XmlNodeList     xmlProjectList;
    XmlElement      nodeScript;
    XmlElement      nodeProject;
    XmlElement      nodeNote;
    XMLParseError   xmlError;
    // Define temporary variables
    str _xmlMsg;
    int i, j;

    ;

    // XML string to read
    _xmlMsg = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><XMLScript Version=\"2.0\"><Project Name=\"Holidays\"><Format>C:\NotesDesign\Note.jpg</Format><PrintSetup><Printer>PDFPrinter</Printer></PrintSetup><Note Date=\"2014/12/10\" ID=\"N001\"><To>Tim</To><Body>Make sure all tasks are completed before holidays!</Body></Note><Note Date=\"01/01/2015\" ID=\"N002\"><To>Sophie</To><Body>Don't forget your Christmas shopping this weekend!</Body></Note></Project></XMLScript>";

    // Create the XML Document
    doc = new XmlDocument();
    doc.loadXml(_xmlMsg);

    // Verify XML Document Structure
    xmlError  = doc.parseError();
    if(xmlError && xmlError.errorCode() != 0)
    {
        throw error(strFmt("XML Error: %1", xmlError.reason()));
    }

    // Get the root element and its child nodes
    nodeScript = doc.getNamedElement("XMLScript");
    xmlScriptList = nodeScript.childNodes();

    for(i=0; i < xmlScriptList.length(); i++)
    {
        nodeProject = xmlScriptList.item(i);
        xmlProjectList = nodeProject.childNodes();

        info("### Project information: ");

        // Print out node and attribute values
        info(strFmt("XML Document Version: %1", nodeScript.getAttribute("Version")));
        info(strFmt("Project Name: %1", nodeProject.getAttribute("Name")));
        info(strFmt("Format: %1", nodeProject.getNamedElement("Format").text()));
        info(strFmt("PrintSetup (innerXML): %1", nodeProject.getNamedElement("PrintSetup").innerXml()));
        info(strFmt("Printer (text): %1 OR Printer (innerText): %2", 
                    nodeProject.getNamedElement("PrintSetup").getNamedElement("Printer").text(), 
                    nodeProject.getNamedElement("PrintSetup").innerText()));

        // Loop through the repeating nodes
        info("### Notes: ");       
        for (j=2; j < xmlProjectList.length(); j++)
        {
            nodeNote = xmlProjectList.item(j);

            info(strFmt("ID: %1, Date: %2", nodeNote.getAttribute("ID"), nodeNote.getAttribute("Date")));
            info(strFmt("To: %1", nodeNote.getNamedElement("To").text()));
            info(strFmt("Body: %1", nodeNote.getNamedElement("Body").text()));
        }
    }    
}