Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics AX (Archived)

Update product categorty by script

Posted on by 95

Hi,

Im trying to update some product categories by a script. But im having truble getting it correct. 

I belive the issue is when i trying to find the recid of category by using the Code.

Some imput whould be much apriciated! 

static void UpdateProductCategoryHierarchy(Args _args)
{
EcoResProduct _EcoResProduct;
EcoResProductCategory _EcoResProductCategory;
EcoResCategoryHierarchy _EcoResCategoryHierarchy;
EcoResCategory _EcoResCategory;

changecompany("12")
{


select forUpdate _EcoResProduct
where _EcoResProduct.DisplayProductNumber == "XXX";        // Item Number
select forUpdate _EcoResProductCategory
where _EcoResProductCategory.Product == EcoResProduct::findByDisplayProductNumber(_EcoResProduct.DisplayProductNumber).RecId;

if (_EcoResProductCategory)
{


ttsBegin;


_EcoResProductCategory.Product = EcoResProduct::findByDisplayProductNumber(_EcoResProduct.DisplayProductNumber).RecId;
EcoResCategoryHierarchy = EcoResCategoryHierarchy::findByName("QQQ");         // Hiearchy name

_EcoResProductCategory.CategoryHierarchy = EcoResCategoryHierarchy.RecId;
_EcoResProductCategory.Category = _EcoResCategory::find('YYY', _EcoResCategory.Code).RecId;        // Trying to find the recid of category by using the Code.

_EcoResProductCategory.update();

ttsCommit;
}
}

}

*This post is locked for comments

  • Suggested answer
    nmaenpaa Profile Picture
    nmaenpaa 101,156 on at
    RE: Update product categorty by script

    I think your issue is here:

    _EcoResCategory::find('YYY', _EcoResCategory.Code).RecId;        

    _EcoResCategory.Code is obviously empty / default value at this point.

  • nmaenpaa Profile Picture
    nmaenpaa 101,156 on at
    RE: Update product categorty by script

    Maybe you should try to resolve it one step at a time. If you can't find the category by RecId, concentrate first on that.

    Try to remove everything else in your code, it will just distract you. Then let's look at the simplified code together. Please also share screenshots of the data you are expecting to find.

    Also, you seem to select/find the same product three times, which makes me believe you have difficulties in understanding how basic select/find operations work. This makes your code difficult to read and troubleshoot.

    You already get an EcoResProduct buffer in the first time you perform select, so no need to find the same record again and again with the "findByDisplayProductNumber" method.

    Occurence 1:

    "select forUpdate _EcoResProduct

    where _EcoResProduct.DisplayProductNumber == "XXX"; "

    Occurence 2:

    "select forUpdate _EcoResProductCategory

    where _EcoResProductCategory.Product == EcoResProduct::findByDisplayProductNumber(_EcoResProduct.DisplayProductNumber).RecId;"

    Occurence 3: "_EcoResProductCategory.Product = EcoResProduct::findByDisplayProductNumber(_EcoResProduct.DisplayProductNumber).RecId;"

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans