web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

How to insert attribute default value to a product

(0) ShareShare
ReportReport
Posted on by

Since I was search for inserting default value for product attribute, I have red many post that developers asked for a solution. These pages have useful solutions for update and delete attribute values:

http://axwiki.blogspot.com/2013/10/updating-product-attribute-of-item-in.html

https://community.dynamics.com/ax/b/axwonders/archive/2013/03/22/copy-set-delete-get-product-categories-and-attributes-in-ax-2012-x

But for inserting default value to all product attribute you can use code below :

I am working on website integrated with DAX R3 CU9. Just copy and paste the code and replace 'attributename'.

static void updateproductAttributeJob(Args _args)
{
InventTable InventTable;

EcoResProduct product;// = EcoResProduct::find(InventTable::find(_itemId).Product);

EcoResProductCategory ecoResProductCategory;
EcoResCategory ecoResCategory;
EcoResCategoryAttributeLookup ecoResCategoryAttributeLookup;

EcoResAttribute ecoResAttribute;
EcoResAttributeValue ecoResAttributeValue;
EcoResProductInstanceValue ecoResProductInstanceValue;
EcoResIntValue ecoResTextValue, ecoResTextValue_ForUpdate, ecoResTextValue_ForValidation;

int _attributeValue;
str _attributeName = 'AttributeName';


EcoResIntValue newEcoResIntValue;
EcoResFloatValue newEcoResFloatValue;
EcoResCurrencyValue newEcoResCurrencyValue;
EcoResDateTimeValue newEcoResDateTimeValue;
EcoResTextValue newEcoResTextValue;
EcoResTextValueTranslation ecoResTextValueTranslation;
EcoResBooleanValue newEcoResBooleanValue;
EcoResTextValue enumerationValue;
EcoResEnumerationAttributeTypeValue enumerationAttributeTypeValue;
EcoResAttributeType enumerationDomain;
EcoResAttributeTypeUnitOfMeasure domainUnitOfMeasure;
UnitOfMeasure unitOfMeasure;
EcoResAttribute localEcoResAttribute;
EcoResAttributeType localEcoResAttributeType;
EcoResAttributeDefaultValue ecoResAttributeDefaultValue;
EcoResReferenceValue ecoResReferenceValue;
EcoResValue referencedValue;
//EcoResAttributeValue ecoResAttributeValue;
// EcoResProductInstanceValue ecoResProductInstanceValue;
RetailStandardAttribute retailStandardAttribute;
EcoResProductInstanceValue newEcoResProductInstanceValue;
EcoResAttributeGroupAttribute ecoResAttributeGroupAttribute;
RetailRichMediaType richMediaType;
EcoResCategoryAttributeLookup localEcoResCategoryAttributeLookup;
EcoResCategoryHierarchyRole localEcoResCategoryHierarchyRole;
str richMediaEditFormMenuItemName;
//ecoResAttribute ecoResAttribute;
EcoResProduct callerMasterOrDistinctProduct;
// EcoResProductInstanceValue newEcoResProductInstanceValue;
EcoResAttributeValue newEcoResAttributeValue;
// ecoResValue newEcoResValue;
//EcoResCategory EcoResCategory;
//ecoResProductCategory ecoResProductCategory;
;

while select forUpdate InventTable

{

select ecoResAttribute
where ecoResAttribute.Name == 'Sort Score';
callerMasterOrDistinctProduct = EcoResProduct::findByProductNumber(InventTable.ItemId);

if (EcoResAttributeType::find(ecoResAttribute.AttributeType).DataType == AttributeDataType::Reference)
{
select firstonly localEcoResAttribute
join localEcoResAttributeType
where localEcoResAttributeType.RecId == localEcoResAttribute.AttributeType
join ecoResReferenceValue
where ecoResReferenceValue.RefRecId == localEcoResAttribute.RecId
&& ecoResReferenceValue.RefTableId == localEcoResAttribute.TableId
join ecoResAttributeDefaultValue
where ecoResAttributeDefaultValue.Attribute == ecoResAttribute.RecId
&& ecoResAttributeDefaultValue.Value == ecoResReferenceValue.RecId
outer join ecoResProductInstanceValue
where ecoResProductInstanceValue.Product == callerMasterOrDistinctProduct.RecId
outer join ecoResAttributeValue
where ecoResAttributeValue.Attribute == localEcoResAttribute.RecId
&& ecoResAttributeValue.InstanceValue == ecoResProductInstanceValue.RecId
outer join referencedValue
where referencedValue.RecId == ecoResAttributeValue.Value;

// copy value from referenced attribute if set
if ( referencedValue.RecId)
{
ecoResAttributeValue.Attribute = ecoResAttribute.RecId;
ecoResAttributeValue.Value = referencedValue.pcDuplicate();
ecoResAttributeValue.insert();

}
else if (isConfigurationkeyEnabled(configurationKeyNum(Retail))
&& !ecoResAttributeValue.RecId)
{
select firstonly retailStandardAttribute
where retailStandardAttribute.Attribute == localEcoResAttribute.RecId;
if (retailStandardAttribute)
{
newEcoResTextValue.clear();
newEcoResTextValue.TextValue = RetailAttributeHelper::getStandardAttributeValue(
retailStandardAttribute.StandardAttributeID, callerMasterOrDistinctProduct.RecId);
if (newEcoResTextValue.TextValue)
{
{


ttsbegin;

// Create new EcoResValue record
newEcoResTextValue.insert();

// Create EcoResProductInstanceValue record
newEcoResProductInstanceValue = EcoResProductInstanceValue::findByProduct(callerMasterOrDistinctProduct.RecId);
if (!newEcoResProductInstanceValue.RecId)
{
newEcoResProductInstanceValue.clear();
newEcoResProductInstanceValue.Product = callerMasterOrDistinctProduct.RecId;
newEcoResProductInstanceValue.insert();
}

// Create EcoResAttributeValue record
newEcoResAttributeValue = EcoResAttributeValue::findByInstanceAttribute(newEcoResProductInstanceValue.RecId, ecoResAttribute.RecId);
if(!newEcoResAttributeValue)
{
newEcoResAttributeValue.clear();
newEcoResAttributeValue.Value = newEcoResTextValue.RecId;
newEcoResAttributeValue.InstanceValue = newEcoResProductInstanceValue.RecId;
newEcoResAttributeValue.Attribute = ecoResAttribute.RecId;
newEcoResAttributeValue.insert();
}

ttscommit;
}
}
}
}
}
else
{
localEcoResAttribute = ecoResAttribute;
localEcoResAttributeType = EcoResAttributeType::find(localEcoResAttribute.AttributeType);
// if value doesn't yet exist and attribute was assigned via an attribute group create value based on the group default for the attribute.
// if (!ecoResValue.RecId)
{
SELECT firstonly localEcoResCategoryAttributeLookup
join EcoResCategory
where localEcoResCategoryAttributeLookup.Category == ecoResCategory.RecId
&& localEcoResCategoryAttributeLookup.Attribute == ecoResAttribute.RecId
join localEcoResCategoryHierarchyRole
where ecoResCategory.CategoryHierarchy == localEcoResCategoryHierarchyRole.CategoryHierarchy
join ecoResProductCategory
where ecoResCategory.RecId == ecoResProductCategory.Category
&& EcoResProductCategory.Product == callerMasterOrDistinctProduct.RecId;

if(localEcoResCategoryAttributeLookup.AttributeGroupAttribute)
{
ecoResAttributeGroupAttribute = EcoResAttributeGroupAttribute::find(localEcoResCategoryAttributeLookup.AttributeGroupAttribute);

if (ecoResAttributeGroupAttribute.DefaultValue)
{
newEcoResProductInstanceValue = EcoResProductInstanceValue::findByProduct(callerMasterOrDistinctProduct.RecId);
if (!newEcoResProductInstanceValue.RecId)
{
newEcoResProductInstanceValue.Product = callerMasterOrDistinctProduct.RecId;
newEcoResProductInstanceValue.insert();
}

EcoResAttributeValue::findOrCreateDefault(newEcoResProductInstanceValue.RecId, ecoResAttribute.RecId, ecoResAttributeGroupAttribute);

}
}
}
}


domainUnitOfMeasure = EcoResAttributeTypeUnitOfMeasure::findByAttributeType(localEcoResAttributeType);

// if (!ecoResValue.RecId)
{
// For new attribute values create the value
newEcoResIntValue.clear();
newEcoResIntValue.IntValue = localEcoResAttribute.getDefaultValue();
{


ttsbegin;

// Create new EcoResValue record
newEcoResIntValue.insert();

// Create EcoResProductInstanceValue record
newEcoResProductInstanceValue = EcoResProductInstanceValue::findByProduct(callerMasterOrDistinctProduct.RecId);
if (!newEcoResProductInstanceValue.RecId && callerMasterOrDistinctProduct.RecId)
{
newEcoResProductInstanceValue.clear();
newEcoResProductInstanceValue.Product = callerMasterOrDistinctProduct.RecId;
newEcoResProductInstanceValue.insert();
}

// Create EcoResAttributeValue record
newEcoResAttributeValue = EcoResAttributeValue::findByInstanceAttribute(newEcoResProductInstanceValue.RecId, ecoResAttribute.RecId);
if(!newEcoResAttributeValue)
{
newEcoResAttributeValue.clear();
newEcoResAttributeValue.Value = newEcoResIntValue.RecId;
newEcoResAttributeValue.InstanceValue = newEcoResProductInstanceValue.RecId;
newEcoResAttributeValue.Attribute = ecoResAttribute.RecId;
newEcoResAttributeValue.insert();
}

ttscommit;
}

if (domainUnitOfMeasure.RecId)
{
// Set the unit of measure selection
if (domainUnitOfMeasure.DefaultUnitOfMeasure)
{
unitOfMeasure = UnitOfMeasure::find(domainUnitOfMeasure.DefaultUnitOfMeasure);
newEcoResIntValue.IntUnitOfMeasure = unitOfMeasure.RecId;
newEcoResIntValue.update();

}
}


}

}

info(int2str(I));
info('Done!');

}

*This post is locked for comments

I have the same question (0)
  • 5400 Profile Picture
    7,162 on at

    Can you check this class.

    \Classes\DMFEcoResAttributeTypeEntityClass

  • Community Member Profile Picture
    on at

    seems ax prepared the code in a class before, thanks anyway

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans