Change or Remove a dimension value on an Item through X++ in AX 2012

Using the below job you can update dimension values on an item.

</pre>

static void UpdateDepartmentMISC_Items(Args _args)
{

InventTable inventTable;
DimensionAttributeValueSetStorage dimStorage;
DimensionAttribute dimAttDept, dimAttMisc;
DimensionAttributeValue dimAttributeValueDept, dimAttributeValueMisc;

DataAreaId company;
str misc, dept;
ItemId itemId;
int counter = 0;

#define.DepartmentDimensionToUpdate('Department')
#define.MISCDimensionToUpdate('MISC')

info(strFmt('Start time: %1', time2StrHM(timeNow())));

changeCompany(company)

{

inventTable = null;

dimStorage = null;

dimAttMisc = null;

dimAttDept = null;

dimAttributeValueDept = null;

dimAttributeValueMisc = null;

while select forUpdate inventTable

where inventTable.ItemId == itemId

&& inventTable.dataAreaId == curext()

{

counter++;

try

{

ttsBegin;

dimStorage = DimensionAttributeValueSetStorage::find(inventTable.DefaultDimension);

dimAttDept = DimensionAttribute::findByName(#DepartmentDimensionToUpdate);

dimAttMisc = DimensionAttribute::findByName(#MISCDimensionToUpdate);

dimAttributeValueDept = DimensionAttributeValue::findByDimensionAttributeAndValue(dimAttDept, dept, true, true);

&nbsp;

if (misc)

{

dimAttributeValueMisc = DimensionAttributeValue::findByDimensionAttributeAndValue(dimAttMisc, misc, true, true);

dimStorage.addItem(dimAttributeValueMisc);

}

&nbsp;

dimStorage.removeDimensionAttributeValue(dimAttributeValueDept.RecId);

inventTable.DefaultDimension = dimStorage.save();

inventTable.doUpdate();

ttsCommit;

}

catch(Exception::Error)

{

error(strFmt("Error to update dimension for item %1", inventTable.itemid));

}

}

}

&nbsp;

info(strFmt("Records updated: %1", Counter));

info(strFmt('End time; %1', time2StrHMS(timeNow())));

}
<pre>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s