Dec 28, 2015

Financial Dimension Values from DefaultDimension

1. static void getsingleDimensionValue(Args _args)
{
    SalesTable                         salesTable = SalesTable::find("DAT-000002");
    Name                               _dimensionName = "Department";    
    DimensionAttributeValueSetStorage  dimStorage;
    str                                dimValue;
    Counter                            counter;
    ;

    dimStorage = DimensionAttributeValueSetStorage::find(salesTable.DefaultDimension);
                         
    for (Counter=1 ; Counter<= dimStorage.elements() ; Counter++)
    {
        if(DimensionAttribute::find(dimStorage.getAttributeByIndex(Counter)).Name == _dimensionName)
        {
            dimValue = dimStorage.getDisplayValueByIndex(Counter);
            info(dimValue);
        }
    }
}

2. DimensionValue getDimensionValue(RefRecID dimensionSetRecID,Name attributeName) 
{
    
    DimensionAttributeValueSet      dimAttrValueSet;
    DimensionAttributeValueSetItem  dimAttrValueSetItem;
    DimensionAttributeValue         dimAttrValue;
    DimensionAttribute              dimAttribute;
    
    dimAttrValueSet = DimensionAttributeValueSet::find(dimensionSetRecID);
    
    select dimAttrValueSetItem
        where   dimAttrValueSetItem.DimensionAttributeValueSet      == dimAttrValueSet.RecId
    join dimAttrValue    
        where   dimAttrValue.RecId                                  == dimAttrValueSetItem.DimensionAttributeValue
    join dimAttribute        
        where   dimAttribute.RecId                                  == dimAttrValue.DimensionAttribute
        &&      dimAttribute.Name                                   == attributeName;
    
    return dimAttrValue.getValue();        
}  

Create New Financial dimension with x++ in axapta

DimensionStorage dimensionStorage;
DimensionAttribute deparmentDimensionAttribute;
DimensionAttributeValue newDepartmentValue;

// Find the deparment dimension attribute
deparmentDimensionAttribute = DimensionAttribute::findByName("Department");  

// Find the new department value we want to put in the new combination.
newDepartmentValue = DimensionAttributeValue::findByDimensionAttributeAndValue(deparmentDimensionAttribute, "abc");

// Load a DimensionStorage instance with the old combination and update the first segment with new value
dimensionStorage = DimensionStorage::findById(ledgerTable.LedgerDimension);
dimensionStorage.setSegment(1,DimensionStorageSegment::constructFromValue("abc", newDepartmentValue));
ledgerJournalTrans.LedgerDimension = dimensionStorage.save();

Financial Dimension on Dialog through SysOperationFrameWork

 ===========================Contract Class================================
[
    DataContractAttribute,
    SysOperationContractProcessingAttribute(classstr(FindimUpUIBuilder), SysOperationDataContractProcessingMode::CreateUIBuilderForRootContractOnly)
]
public class CVRRentalDueDateReminderContract
{
    int numberOverdueDays;
    int overrideNumOfDays;
    str packedQuery;
    Name primaryDimensionFocus;
    // string value of range
    SerializedDimensionRange    serializedDimensionRanges;
    FreeText                    dimensionRange;
    InventDimViewContract       inventdimViewContract;

}


public Query getQuery()
{
    return new Query(SysOperationHelper::base64Decode(packedQuery));

}

[
    DataMemberAttribute(identifierStr(DimensionRange))
]
public FreeText parmDimensionRange(FreeText _dimensionRange = dimensionRange)
{
    dimensionRange = _dimensionRange;
    return dimensionRange;

}

public Map parmDimensionRangeMap(Map _dimensionRangeMap = null)
{
    XmlDocument xDoc;
    Map dimRangeMap;

    if (prmisDefault(_dimensionRangeMap))
    {
        if (serializedDimensionRanges)
        {
            xDoc = new XmlDocument();
            xDoc.loadXml(serializedDimensionRanges);

            dimRangeMap = Map::createFromXML(xDoc.root() as XmlNode);
        }
        else
        {
            dimRangeMap = new Map(Types::Int64, Types::String);
        }
    }
    else
    {
        if (_dimensionRangeMap == null)
        {
            _dimensionRangeMap = new Map(Types::Int64, Types::String);
        }
        else
        {
            this.parmSerializedDimensionRanges(_dimensionRangeMap.xml());
        }
        dimRangeMap = _dimensionRangeMap;
    }
    return dimRangeMap;
}



[DataMemberAttribute('InventDimViewContract')]
public InventDimViewContract parmInventDimViewContract(InventDimViewContract _inventDimViewContract = inventDimViewContract)
{
    inventDimViewContract = _inventDimViewContract;
    return inventDimViewContract;
}

 
[DataMemberAttribute('OverdueDays'),
SysOperationDisplayOrderAttribute('2')]
public int parmNumberOverdueDays(int _numberOverdueDays = numberOverdueDays)
{
;
    numberOverdueDays = _numberOverdueDays;
    return numberOverdueDays;
}

[DataMemberAttribute('OverrideNumOfDays'),
SysOperationDisplayOrderAttribute('1')]
public int parmOverrideNumOfDays(int _overrideNumOfDays = overrideNumOfDays)
{
    overrideNumOfDays = _overrideNumOfDays;
    return overrideNumOfDays;
}


[   DataMemberAttribute('PrimaryDimensionDimensionFocus'),
    SysOperationLabelAttribute(literalstr("@SYS329752")),
    SysOperationHelpTextAttribute(literalstr("@SYS307658")),
    SysOperationDisplayOrderAttribute('3')
]
public Name parmPrimaryDimensionFocus(Name _primaryDimensionFocus = primaryDimensionFocus)
{
    primaryDimensionFocus = _primaryDimensionFocus;
    return primaryDimensionFocus ;// ? primaryDimensionFocus : "MA+BU+DEPT" ;
}

 

[DataMemberAttribute,
AifQueryTypeAttribute('_packedQuery', querystr(CustTable))]
public str parmQuery(str _packedQuery = packedQuery)
{
;
    packedQuery = _packedQuery;
    return packedQuery;
}
 

[
    DataMemberAttribute(identifierStr(SerializedDimensionRanges))
]
public SerializedDimensionRange parmSerializedDimensionRanges(SerializedDimensionRange _serializedDimensionRanges = serializedDimensionRanges)
{
    serializedDimensionRanges = _serializedDimensionRanges;
    return serializedDimensionRanges;
}


public void setQuery(Query _query)
{
    packedQuery = SysOperationHelper::base64Encode(_query.pack());
}

===========================Service Class==================================

public class CVRRentalDueDateReminderService extends SysOperationServiceController
{
}


[SysEntryPointAttribute(true)]
public void checkDueDates(CVRRentalDueDateReminderContract _dueDateReminderContract)
{
    QueryRun      queryRun;
    CustTable     custTable;
    Map           dimensionRangeMap;
    MapEnumerator rangeMapEnumerator;
    RecId         dimAttrRecId;
    str           dimensionCriteria;



    dimensionRangeMap = _dueDateReminderContract.parmDimensionRangeMap();

     rangeMapEnumerator = dimensionRangeMap.getEnumerator();

     while (rangeMapEnumerator.moveNext())
    {
         dimAttrRecId = rangeMapEnumerator.currentKey();
        dimensionCriteria = rangeMapEnumerator.currentValue();
        info(dimensionCriteria);

    }
    // Get the query from the data contract
    queryRun = new QueryRun(_dueDateReminderContract.getQuery());

    while (queryRun.next())
    {
        custTable = queryRun.get(tableNum(CustTable));
        info(custTable.AccountNum + "Name = " + custTable.name());

    }
}




public CVRRentalDueDateReminderContract getCVRRentalDueDateReminderContract()
{
    return this.getDataContractObject('_CVRRentalDueDateReminderContract') as CVRRentalDueDateReminderContract;
}

public static CVRRentalDueDateReminderService construct()
{
    return new CVRRentalDueDateReminderService();
}

public static void main(Args _args)
{
    identifierName                         className, methodName;
    SysOperationExecutionMode              mode;
    CVRRentalDueDateReminderContract  contract;
    CVRRentalDueDateReminderService  reminderService ;//= CVRRentalDueDateReminderService::construct();

    [className, methodName, mode] = SysOperationServiceController::parseServiceInfo(_args);
    reminderService                    = new CVRRentalDueDateReminderService(className, methodName, mode);

    reminderService.parmDialogCaption("Vivek Testing for fin Dimension");
    contract = reminderService.getDataContractObject('CVRRentalDueDateReminderContract');
    reminderService.startOperation();
}

=========================UI Builder Class=================================


public class FindimUpUIBuilder extends SrsReportDataContractUIBuilder
{

    CVRRentalDueDateReminderContract  contract;

    DialogField dialogFieldPrimaryFocus;
    DialogGroup dialogDimensionsGroup;
    DialogField dialogFieldDetailLevel;
    Map     dimControlsMap;
    Map dimControlMap;
    Map dimRangeMap;
}

public void build()
{
     DialogField                 dimField;
    str                         thisVal;
    str                         thisLabel;
    DimensionAttributeSetItem   dimensionAttributeSetItem;
    DimensionAttribute          dimensionAttribute;
    DimensionEnumeration        dimAttrSetForLedger;
    super();
    contract = this.dataContractObject() as CVRRentalDueDateReminderContract;

    dialogDimensionsGroup = this.dialog().addGroup("@GLS101565");
    dialogDimensionsGroup.hideIfEmpty(false);

    dialogDimensionsGroup = this.dialog().addGroup("@GLS101565");
    dialogDimensionsGroup.hideIfEmpty(false);

        // Control map maps dimension attributes to physical controls
        dimControlMap = new Map(Types::Int64, Types::Class);

        // DimRangeMap map dimension attributes to ranges
       // dimRangeMap = contract.parmDimensionRangeMap();
        dimAttrSetForLedger = DimensionCache::getDimensionAttributeSetForLedger();

        while select DimensionAttribute from dimensionAttributeSetItem where dimensionAttributeSetItem.DimensionAttributeSet == dimAttrSetForLedger
            join * from dimensionAttribute where dimensionAttribute.RecId == dimensionAttributeSetItem.DimensionAttribute
        {
               //  thisVal = DimensionAttribute::find(dimensionAttributeSetItem.DimensionAttribute).Name;

                thisVal = '';

            thisLabel = dimensionAttribute.localizedName();

            dimField = this.dialog().addFieldValue(extendedTypeStr(String255), thisVal, thisLabel);
            dimField.registerOverrideMethod(methodstr(FormStringControl, lookup), methodstr(LedgerTrialBalanceUIBuilder, lookupDimensionAttribute), this);

            dimControlMap.insert(dimensionAttributeSetItem.DimensionAttribute, dimField);
        }
        //IfFin dim show based on dimension set you should pass set name in below parm
         this.updateForFocus(contract.parmPrimaryDimensionFocus());
}
///
/// Implements a custom lookup for dimension attribute.
///
///
/// The FormStringControl for which the lookup fields must be associated.
///
///
/// It uses the DimensionAttribute table for lookup.
///
private void lookupDimensionAttribute(FormStringControl _dimensionValue)
{
    Args                    e;
    FormRun                 lookupFormRun;
    DimensionAttribute      dimAttr;
    Object                  object;

    if (_dimensionValue != null)
    {
        // Construct arguments for the custom lookup
        e = new Args();
        e.name(formStr(DimensionDefaultingLookup));
        e.lookupValue(_dimensionValue.text());
        e.caller(_dimensionValue);

        // Find the dimension attribute associated with the string control which called this method
        dimAttr = DimensionAttribute::findByLocalizedName(_dimensionValue.label());
        e.lookupField(dimAttr.ValueAttribute);
        e.record(dimAttr);

        // Run the custom lookup and init the lookup form
        lookupFormRun = classfactory.formRunClass(e);
        lookupFormRun.init();

        // Specify this is the callback on the lookup form by casting the
        // form to an object and late binding the setResultCallback method
        if (SysFormRun::hasMethod(lookupFormRun, identifierStr(setResultCallback)))
        {
            object = lookupFormRun;
            object.setResultCallback(this);
        }

        _dimensionValue.performFormLookup(lookupFormRun);
    }
}

///
/// Override this method in order to register the dialog field methods to capture events.
///
public void postRun()
{
    DialogField dialogFieldSecondaryFocus;
    Dialog dialogLocal = this.dialog();

    super();

    // This method should be called in order to handle events on dialogs.
   // dialogLocal.dialogForm().formRun().controlMethodOverload(false);

    // dimension sets
   // dialogFieldPrimaryFocus = this.bindInfo().getDialogField(this.dataContractObject(), methodstr(LedgerTrialBalanceContract, parmPrimaryDimensionFocus));
   // dialogFieldPrimaryFocus.registerOverrideMethod(methodstr(FormStringControl, lookup), methodstr(LedgerTrialBalanceUIBuilder, dimensionSetLookup), this);
   // dialogFieldPrimaryFocus.registerOverrideMethod(methodstr(FormStringControl, modified), methodstr(LedgerTrialBalanceUIBuilder, dimensionSetModified), this);
}

public void setDimensionAttributeValue(DimensionAttribute _dimensionAttribute, RecId _dimAttrValueRecId, str _value)
{
    // Force a save of the set(s) as the user may leave the tab at any point and no event will be raised to force a savedimControlMap
}
 
///
/// Updates the dialog fields for the dimension hierarchy.
///
///
/// The dimension hierarchy name.
///
public void updateForFocus(Name _dimensionHierarchyName = '')
{
    RecId                   dimHierarchyRecId;
    MapEnumerator           controlMapEnumerator;
    DimensionHierarchyLevel dimHierarchyLevel;
    boolean                 setHasDimension;
    DialogField             dimField;
    RecId                   dimAttrRecId;
    boolean                 anyDimensionsVisible;

     dimHierarchyRecId = DimensionHierarchy::findByTypeAndName(DimensionHierarchyType::Focus, _dimensionHierarchyName).RecId;

        // Clear out existing dialog fields
        controlMapEnumerator = dimControlMap.getEnumerator();
        while (controlMapEnumerator.moveNext())
        {
            dimAttrRecId = controlMapEnumerator.currentKey();
            dimField = controlMapEnumerator.currentValue();

            setHasDimension = false;

            if (dimHierarchyRecId)
            {
                    select firstOnly RecId from dimHierarchyLevel where
                        dimHierarchyLevel.DimensionHierarchy == dimHierarchyRecId &&
                        dimHierarchyLevel.DimensionAttribute == dimAttrRecId;

                setHasDimension = (dimHierarchyLevel.RecId != 0);
            }
            else
            {
                 select firstOnly RecId from dimHierarchyLevel where
                        dimHierarchyLevel.DimensionAttribute == dimAttrRecId;

                setHasDimension = (dimHierarchyLevel.RecId != 0);
            }

            dimField.visible(setHasDimension);
            anyDimensionsVisible = anyDimensionsVisible || setHasDimension;
        }

        dialogDimensionsGroup.visible(anyDimensionsVisible);

}

 

 

Dec 10, 2015

Cross reference update Job for AX objects

static void XRefUpdateBatch(Args _args)
{
    ;
    xRefUpdate::truncateXrefTables();
    xRefUpdateIL::updateAllXref(true, false, true);
    info("Cross reference update batch job created.");
}

Dec 9, 2015

Toggling administrative privileges in AX 2012 R3 through code

static void ToggleAdminMode(Args _args)
{
    ;   
    SecurityUtil::sysAdminMode(!SecurityUtil::sysAdminMode());
    info(strFmt('Admin mode: %1', SecurityUtil::sysAdminMode()));
}

Note :- Once you restart the AX client, your privileges will be restored to normal.

Dec 7, 2015

Generate Xml file for XDS class in AX


static void GenerateXSDSchema_AxdCustTable(Args _args)
{

AxdCustTable    AxdCustTable;
XML             xml;
XMLDocument     xmlDocument;

;
AxdCustTable = AxdBase::newClassId(classnum(AxdCustTable));
xml          = AxdCustTable.getSchema();
xmlDocument  = XMLDocument::newXML(xml);
 
//TODO Select file to write XML output to
xmlDocument.save("D:\\XSDSchema_AxdCustTable.xml");

}