Showing posts with label x_plusplus. Show all posts
Showing posts with label x_plusplus. Show all posts

Tuesday, December 12, 2017

AX - How validate ComboBox value (validate() method)

With validate() method you can define check(s) for control on form (return FALSE when is something wrong).
When combobox is bound to Enun, use this form:
public boolean validate()
{
  boolean ret;

  ret = super();

  /* do check */
    
  if ( JmgFeedbackStatus.selection() == JmgFeedbackStatus::Completed ) {
    ret = false;
    warning( "@SYS316081" );
    //throw Global::error( "The parameter value is invalid." );
  }    

  return ret;
}
When combobox is bound to DataSource, you can add validate() directly to datasource field:
public boolean validate()
{
  boolean ret;

  ret = super();

  /* do check */
    
  if ( JmgTmpJobBundleProdFeedback.ReportAsFinished == JmgFeedbackStatus::Completed ) {
    ret = false;
    warning( "@SYS316081" );
    //throw Global::error( "The parameter value is invalid." );
  }  

  return ret;
}

Tuesday, December 5, 2017

AX - How find sales (purchase) price for item

On Released products in AX 2012 you can define Sales price:













So here is code for its reading:
InventTable inventTable = InventTable::find( '5801-105-535-ND_0_0000' );
PriceDisc priceDisc;
PriceCur iPrice;

if ( inventTable ) {

  /* -- find sales price for item, dimension, unit, date and company */
                
  priceDisc = new PriceDisc( ModuleInventPurchSales::Sales, inventTable.ItemId, null, 'ks', 
                             systemdateget(), 1, 'COMPANY_AG' );

  if ( priceDisc.findPrice( CustTable::find( 'COMPANY_AG' ).PriceGroup ) )
    iPrice = priceDisc.price();
  /*else if ( priceDisc.findItemPrice() )
    iPrice = priceDisc.price();*/           

  info( "Sales price: " + num2str( iPrice, 15, 2, 1, 0 ) );                            
}
Output could be:
Sales price: 20.50

Monday, December 4, 2017

AX - How get item cost (std) price

For getting cost price use class InventItemPrice. Second parameter is date for cost price,
then are two parameters for dimension. Last parameter tells if throw error, if cost price will not be found.
InventTable inventTable = InventTable::find( '55555-555555_555' );
InventItemPrice inventItemPrice;

if ( inventTable ) {

  /* -- get price for selected date */
        
  inventItemPrice = InventItemPrice::stdCostFindDate( 
                      inventTable, /*systemDateGet()*/
                      mkDate( 31, 1, 2017 ), 
                      '', null, 'COMPANY', false 
                    );

  info( "Price: " + num2str( inventItemPrice.Price(), 15, 2, 1, 0 ) );                            
}
Output could be:
Price: 11879.30

AX - How check if file exists + YesNo dialog

Link for external WIN API function.
FilenameSave filename;
...
if ( WinAPI::fileExists( filename ) )
  if ( ! Box::yesNo( strfmt( "@SYS60148", filename ), DialogButton::No ) )
  {
    info( "Export cancelled." );
    return false;
  }

AX - How find active BOM version and list its items

Base example shows how to get BOM active version to data, with fromQty=1, no configuration.
InventTable inventTable = InventTable::find( '5801-382-066_0_0200' );
BOMVersion bomVersion;
BOM bom;
    
if ( inventTable ) {
        
  /* -- get active BOM version to date, with fromQty=1, no config */
        
  bomVersion = BomVersion::findActive( inventTable.ItemId, systemdateGet(), 1, null );
        
  if ( bomVersion ) {
            
    /* -- list items and quantity */
            
    while 
    select bom 
    where bom.BOMId == bomVersion.BOMId 
    {            
      info( bom.ItemId + ", " + num2str( bom.BOMQty(), 15, 2, 1, 0 ) + " " + bom.UnitId );
    }
  }

}
Ouput could be:
5801-380-510_0_0000,       1.00 KS
5801-361-927-1_0_0000,     2.00 ks
S-442-900-2347,            1.00 KS
...

AX - How get info about AX user

When you need info about connected user, you can use class UserInfo and xUserInfo:
UserInfo userInfo = xUserInfo::find( false, curUserId() );
    
if ( userInfo ) {
  info( "Name: " + userInfo.name );        
  info( "Language: " + userInfo.language );
  info( "Enabled: " + ( userInfo.enable ? "Yes" : "No" ) );
}
Output:
Name: Aldiss, Brian
Language: en-us
Enabled: Yes
Note: For current user you can use directly:
UserInfo userInfo = xUserInfo::find();

AX - How get info about licence

X++ contains class xSysConfig for providing information about licence. Below code shows info with AX serial number and licence holder.
info( "Licence number: " + xSysConfig::serialnumber() );    
info( "Licence holder: " + xSysConfig::licenseHolder() );    
Output could be:
Licence number: M1218154
Licence holder: Company AG

Monday, November 27, 2017

AX - How generate datetime with to 23:59:59 PM

Use this code (1 hour = 3600 seconds):

utcDateTime dDate = DateTimeUtil::newDateTime( today(),  3600 * 24 - 1 );
    
info( datetime2str( dDate ) );


AX - How add (next) row to StringEdit control

Used for example for logs.
  1. Add StringEdit control to form.
  2. Set name and set Autodeclaration to Yes.
  3. Set Multiline to Yes.
void AddText( str _sValue ) {
  EOutput.text( EOutput.text() + _sValue + num2char(10) );
}

AX - How pass table (datasource) as param to form

In menu item on caller form you must define DataSource property:







This ProdTable will be passed as parameter to called (here) form.

In called form you override init() method:

FormRun callerForm;
FormDataSource callerDataSource;
...
public void init()
{
    super();

    if ( element.args() && element.args().caller() )
    {
        callerForm = element.args().caller();

        if ( callerForm && callerForm.dataSource() ) {

            /* -- save DataSource */

            callerDataSource = callerForm.dataSource();

            /* -- call check method */

            this.check();
        }

    }
}

Thursday, November 23, 2017

AX - How get aggregate select value - max(), count()

For aggregate x++ select functions you can use this form:
calcDate = ( select maxof( calcdate ) from ProdParmBomCalc 
where ProdParmBOMCalc.ProdId == source.TransRefId ).calcdate;
Supported aggregate functions: sum, avg, minof , maxof, count.

For count function you can use this code:
int iCount;

iCount =
  ( select count(recId) from prodRoute
    where
    prodRoute.ProdId == source.ProdId &&
    prodRoute.OprPriority == RouteOprPriority::Primary 
  ).recId;
        
if ( iCount != 0 ) {
  bError = true;            
}

AX - How call form with param

Calling form with passing parameter - here it is one passed value (ProdId type):
void Check()
{
  Args args;

  FormRun formRun;

  /* -- call check form */

  args = new args();

  if ( jmgJobTable )
    args.parm( JmgJobTable.ModuleRefId );
  else
    args.parm( "" );

  ags.name( formstr( CheckBus ) );
  formRun = classFactory.formRunClass( args );
  formRun.init();
  formrun.run();
  formrun.wait();
}
..and here is receiving part - in form init() method:
public void init()
{
  ProdId prodId;

  super();

  if( element.args() )
  {
    /* -- get param value */

    prodId = element.args().parm();     
    EProdId.text( prodId );
    EOutput.text( "" );

    EProdId.setFocus();
  }
}

Wednesday, November 22, 2017

AX - How check if current user is SysAdmin

This code check, if current AX user is in System Administrator group:
if ( isSystemAdministrator() ) { ... }

AX - How check if (current) user is in group

AX 2012 define user groups (System administration\User groups):



if ( UserInfoHelp::userInUserGroup( curUserId(), 'PLANNING' ) ) { ... }

AX - How get enum labels to db table

When you have AX enum, often you need to read its labels externally - for example in SQL question.

In AX exists table SRSAnalysisEnums, which contain enum name, enum value and its label.
select * from SRSAnalysisEnums
where
enumname = 'NoYes' and
languageid in ( 'de', 'en-us' )
Output:
ENUMITEMVALUE ENUMITEMLABEL LANGUAGEID ENUMNAME ENUMITEMNAME RECVERSION RECID

0 Nein de NoYes No 1 5637179205
0 No en-us NoYes No 1 5637144576 1
Ja de NoYes Yes 1 5637179250 1 
Yes en-us NoYes Yes 1 5637144577
For rebuild this table (->new enum in AX) you can use class BIGenerator, method populateSRSAnalysisEnums(). Implicitly are generated enums from tables in AX BI perspectives.
static void cieb_updateSSRSEnums(Args _args)
{
    BIGenerator::populateSRSAnalysisEnums();
    info( "OK" );
}
Explicitly you can change in inner method addFrameworkEnumsToDictEnumSet(), where you can define some others special enums for SRSAnalysisEnums table:
void addFrameworkEnumsToDictEnumSet()
{
    addEnumToDictEnumSet(new DictEnum(enumNum(boolean)));
    addEnumToDictEnumSet(new DictEnum(enumNum(NoYes)));
    addEnumToDictEnumSet(new DictEnum(enumNum(AutoNoYes)));

    /* -- added - start */
    addEnumToDictEnumSet( new DictEnum( enumNum( cieb_technology_sale_type_enum ) ) );
    addEnumToDictEnumSet( new DictEnum( enumNum( cieb_technology_type_enum ) ) );
    addEnumToDictEnumSet( new DictEnum( enumNum( cieb_technology_type1_enum ) ) );
    /* -- added - end */

    addEdtEnumToDictEnumSet(new DictType(extendedTypeNum(BIIsNotApplicable)));
}

Tuesday, November 21, 2017

AX - How check if DateEdit control is empty

if ( EDates.dateValue() == dateNull() ) {
  error( "Date value is empty." );
  EDates.setFocus();
}

AX - empty code

For empty code use method exceptionTextFallThrough():
if ( RDates.selection() == ProdRouteCardForm_Dates::BySchedule ) {
  exceptionTextFallThrough();
}

AX - How to get first and last day in month

Use this code:
int iMonth = 6;
int iYear = 2017;

date firstDate = mkDate( 1, iMonth, iYear );
date lastDate = endMth( firstDate );

info( date2str( firstDate, 321, DateDay::Digits2, DateSeparator::Hyphen, DateMonth::Digits2, 
      DateSeparator::Hyphen, DateYear::Digits4 ) );
info( date2str( lastDate, 321, DateDay::Digits2, DateSeparator::Hyphen, DateMonth::Digits2, 
      DateSeparator::Hyphen, DateYear::Digits4 ) );