To generate dimension and find the account value - KGSPMIDimensionUtility

KGSPMIDimensionUtility

/// <summary>
/// Utility class for Dimensions.
/// </summary>


 
class KGSPMIDimensionUtility{
public static RefRecId generateDynamicAccount(LedgerJournalACType _type, str _account)



{


DimensionDynamicAccountServiceProvider dimensionDynamicAccountServiceProvider;DimensionAttributeValueContract ledgerContract;
DimensionStorageResult dimensionStorageResult;


DimensionDynamicAccountContract contract;


contract = new DimensionDynamicAccountContract();
contract.parmAccountType(_type);
contract.parmCompany(curExt());


ledgerContract = DimensionAttributeValueContract::construct(enum2Str(_type), _account);
contract.parmPrimaryDimensionAttributeValue(ledgerContract);
dimensionDynamicAccountServiceProvider = DimensionDynamicAccountServiceProvider::newForDynamicAccountContract(contract);

dimensionStorageResult = dimensionDynamicAccountServiceProvider.resolve();


return dimensionStorageResult.parmSavedRecId();
}


public static RefRecId generateLedgerDimension(container _con)
{


LedgerAccountContract LedgerAccountContract = new LedgerAccountContract();
DimensionAttributeValueContract ValueContract;
List valueContracts = new List(Types::Class);
dimensionAttributeValueCombination dimensionAttributeValueCombination;
int counter = 1;
LedgerRecId ledgerRecId;
DimensionStorage dimStorage;
LedgerAccountContract.parmMainAccount(conPeek(_con, counter));
counter++;


while(counter <= conlen(_con))
{
ValueContract = new DimensionAttributeValueContract();

//Add to list only when dimension value is available

if(conPeek(_con, (counter + 1)))
{
ValueContract.parmName(conPeek(_con, counter));
counter++;
ValueContract.parmValue(conPeek(_con, counter));
counter++;
valueContracts.addEnd(ValueContract);
}
}
LedgerAccountContract.parmValues(valueContracts);
dimStorage = DimensionServiceProvider::buildDimensionStorageForLedgerAccount(LedgerAccountContract);

dimensionAttributeValueCombination = DimensionAttributeValueCombination::find(dimStorage.save());
ledgerRecId = dimensionAttributeValueCombination.RecId;

return ledgerRecId;
}
 

public static RefRecId generateDefaultDimension(container _con)
{
DimensionNameValueListContract dimensionNameValueListContract;

DimensionAttributeValueContract dimensionAttributeValueContract;
DimensionStorageResult dimensionStorageResult;
DimensionNameValueListServiceProvider dimensionNameValueListServiceProvider;
int counter = 1;
str name, value;
dimensionNameValueListContract = new DimensionNameValueListContract();
dimensionNameValueListContract.parmValues(new List(Types::Class));
while(counter <= conlen(_con)) {
name = conpeek(_con,counter);
counter++;
value = conpeek(_con,counter);
counter++;
dimensionAttributeValueContract = DimensionAttributeValueContract::construct(name, value); dimensionNameValueListContract.parmValues().addEnd(dimensionAttributeValueContract);
}



//resolve the dimension
dimensionNameValueListServiceProvider = DimensionNameValueListServiceProvider::newForDimensionNameValueListContract(dimensionNameValueListContract); dimensionStorageResult = dimensionNameValueListServiceProvider.resolve();
   
return dimensionStorageResult.parmSavedRecId(); }



public static str getDimensionDisplayValue(RefRecId _dimRecId) {
str displayValue;

DimensionAttributeValueSetItem dimSetItem;
while select DisplayValue from dimSetItem
order by dimSetItem.RecId
where dimSetItem.DimensionAttributeValueSet == _dimRecId {
displayValue += dimSetItem.DisplayValue + '-'; }
        return displayValue;


}
 

public static String255 getDimensionvalue(RefRecId _dimension, Name _dimName)
{
DimensionAttributeValueSetStorage dimStorage;




dimStorage = DimensionAttributeValueSetStorage::find(_dimension);
return dimStorage.getDisplayValueByDimensionAttribute(DimensionAttribute::findByName(_dimName).RecId); }


public static container getLedgerDimContainerFromLedgerFormat(str _dispalyValue, DimensionDataEntityStructureType _dimensionDataEntityStructureType = DimensionDataEntityStructureType::DataEntityLedgerDimensionFormat) {
List dimensionNames;

ListEnumerator lstEnumdimensionNames;
container acctPattern, dimension;
int counter = 1;
dimensionNames = DimensionResolver::splitByDimensionIntegrationDelimiter(DimensionHierarchy::getDisplayStringDimensionIntegrationStructure(_dimensionDataEntityStructureType));
dimension = str2con_RU(_dispalyValue, '-'); acctPattern = conNull();
lstEnumdimensionNames = dimensionNames.getEnumerator();
while(lstEnumdimensionNames.moveNext()) {


if( lstEnumdimensionNames.current() == "@KGSPMI:KGSPMIMainAccount") {
acctPattern = conIns(acctPattern, 1, conPeek(dimension, counter));

}
else

{
if(conPeek(dimension, counter)) {
acctPattern = conIns(acctPattern, (conLen(acctPattern) + 1), lstEnumdimensionNames.current());


acctPattern = conIns(acctPattern, (conLen(acctPattern) + 1), conPeek(dimension, counter)); }
}
counter++;
}
return acctPattern;            }



public static boolean validateLedgerDimContainerFromLedgerFormat(str _dispalyValue, DimensionDataEntityStructureType _dimensionDataEntityStructureType = DimensionDataEntityStructureType::DataEntityLedgerDimensionFormat) {
List dimensionNames;

ListEnumerator lstEnumdimensionNames;
container dimension;
int counter = 1;
str dimensionValue;
boolean ret = true;
dimensionNames = DimensionResolver::splitByDimensionIntegrationDelimiter(DimensionHierarchy::getDisplayStringDimensionIntegrationStructure(_dimensionDataEntityStructureType));
dimension = str2con_RU(_dispalyValue, '-'); lstEnumdimensionNames = dimensionNames.getEnumerator();
while(lstEnumdimensionNames.moveNext()) {
dimensionValue = conPeek(dimension, counter);
            if(lstEnumdimensionNames.current() == "@KGSPMI:KGSPMIMainAccount")


{
if(!MainAccount::findByMainAccountId(dimensionValue)) {
                    ret = checkFailed(strfmt("@KGSPMI:KGSPMIMainAccountNotAvailable", dimensionValue)); }
}



else
{
if(conPeek(dimension, counter)) {
                    if(!DimensionAttributeValue::findByDimensionAttributeAndValue(DimensionAttribute::findByName(lstEnumdimensionNames.current()), dimensionValue)) {
                        ret = checkFailed(strfmt("@KGSPMI:KGSPMIDimensionNotInSystem", lstEnumdimensionNames.current(), dimensionValue)); }
}
}
counter++;
}
        return ret;




}
}

Comments

Popular posts from this blog

Data entites method calling sequence in D365FO

How to Extend Sales Order Update Functionality to Custom Fields in D365 Finance and Operations