Create or Update Product attributes using X++ in D365 F&O

This code is to update or insert product attributes for a boolean value. Each item will have its own boolean value. We cannot update the existing boolean value for a new item. For the first time, we have to create the attribute value record id for the item and then you can update if you want to make changes to the value based on the record id. 

Please change the code according to your requirement.

    /// <summary>
    ///  To create or update product attribute
    /// </summary>
    ///
    public void createOrUpdateProductAttribute()
    {
        EcoResProduct                       product;
        EcoResDistinctProductVariant        variant;
        EcoResProductInstanceValue          ecoResProductInstanceValue;
        EcoResAttributeType                 ecoResAttributeType;
        EcoResAttribute                     ecoResAttribute;
        EcoResAttributeValue                ecoResAttributeValue;
        EcoResBooleanValue                  ecoResBooleanValue;
        ItemId                              itemId;
        EcoResCategoryName                  categoryName;
        RefRecId                            itemRecID;
        str                                 attributeName;
        Boolean                             attributeValue;

        itemId          = this.ItemNumber; //calling item id
        attributeName   = "@BC:ServiceOrderableLbl"; //In my case my attribute is service orderable
        attributeValue  = this.BCServiceOrderable; //calling attribute value
        categoryName    = this.BCEcoResCategoryName; // calling category name

        product = EcoResProduct::findByProductNumber(itemId);

        //Get Product by Itemid
        if(product)
        {
            ttsbegin;

            if(product.getInstanceRelationType() == tableStr(EcoResDistinctProductVariant))
            {
                variant = product as EcoResDistinctProductVariant;
                product = EcoResProduct::find(variant.ProductMaster);
            }

            //Find records in ecoResProductInstanceValue if not insert new
            ecoResProductInstanceValue = EcoResProductInstanceValue::findByProduct(product.recid);

            if(!ecoResProductInstanceValue)
            {
                //insert records in ecoResProductInstanceValue
                ecoResProductInstanceValue.product = product.Recid;
                ecoResProductInstanceValue.insert();
            }

            //Check if record exists in EcoResAttributeValue
            ecoResAttributeType     = EcoResAttributeType::findByName("Boolean");
            ecoResAttribute             = EcoResAttribute::findByName(attributeName,                       ecoResAttributeType.RecId);
            ecoResAttributeValue    = EcoResAttributeValue::findByInstanceAttribute(ecoResProductInstanceValue.recid, ecoResAttribute.recid, true);
         
            if(ecoResAttributeValue)
            {
                //If EcoResBooleanValue exists, check it doesn't have the same value
                if(EcoResBooleanValue::find(ecoResAttributeValue.Value).BooleanValue != attributeValue)
                {
                    //create a record in EcoresBooleanvalue
                    ecoResBooleanValue.clear();
                    ecoResBooleanValue.BooleanValue = attributeValue;
                    ecoResBooleanValue.insert();
 
                    //Update ecoResAttribute
                    ecoResAttributeValue.Value = ecoResBooleanValue.recid;
                    ecoResAttributeValue.update();                 
                }             
            }

            //if ecoresAttributeValue not exists
            else
            {
                //Always is necessary to insert a new EcoResValue record per attribute (1:1)
                ecoResBooleanValue.clear();
                ecoResBooleanValue.BooleanValue = attributeValue;
                ecoResBooleanValue.insert();

                //insert record in ecoResAttributeValue
                ecoResAttributeValue.clear();
                ecoResAttributeValue.InstanceValue  = ecoResProductInstanceValue.recid;
                ecoResAttributeValue.Attribute      = ecoResAttribute.recid;
                ecoResAttributeValue.Value         = ecoResBooleanValue.Recid;
                ecoResAttributeValue.insert();
             
            }
            ttsCommit;
        }
    }

Comments

Popular posts from this blog

D365FO - Data integration by OData

Layers concept in D365