Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 Community / Forums / Sales forum / Issue with Dynamics Sa...
Sales forum
Answered

Issue with Dynamics Sales custom script

Posted on by 10

Hello everyone,

I'm having an issue with a custom script in Dynamics Sales and was hoping to get some help troubleshooting it.

I have a custom script that is triggered on the change of a lookup field called "cr452_tipodemadeira" and is supposed to set the value of another field called "productnumber" to either the value of the "name" attribute or the "cr452_prefixo" attribute of the selected record in the "cr452_tipodemadeira" lookup.

Here are the two versions of the script I've tried:

javascriptCopy code
function COD_MSE() { var woodType = Xrm.Page.getAttribute("cr452_tipodemadeira").getValue(); if (woodType && woodType.length > 0) { var nome = woodType[0].name; Xrm.Page.getAttribute("productnumber").setValue(nome); } else { console.log("Nenhum tipo de madeira selecionado."); } }
javascriptCopy code
function COD_MSE() { var woodType = Xrm.Page.getAttribute("cr452_tipodemadeira").getValue(); if (woodType && woodType.length > 0) { var nome = woodType[0].cr452_prefixo; Xrm.Page.getAttribute("productnumber").setValue(nome); } else { console.log("Nenhum tipo de madeira selecionado."); } }

The first version of the script works as expected, but the second version doesn't set the value of the "productnumber" field. I have also added the "cr452_prefixo" attribute to the view associated with the lookup field, but that didn't solve the issue.

Any ideas on what could be causing this issue?

Thank you in advance for your help!

Categories:
  • Suggested answer
    Leco Lv Profile Picture
    Leco Lv on at
    RE: Issue with Dynamics Sales custom script

    Hi partner,

    Andrew is right about the cause of the problem. As he enumerated, you can achieve your needs in a no-code way.

  • Verified answer
    a33ik Profile Picture
    a33ik 84,321 Most Valuable Professional on at
    RE: Issue with Dynamics Sales custom script

    Hello,

    Your woodType value can return one of 3 values for the lookup - id (guid of the record), name - display name of the related record and entityTyoe - schema name of the related entity.

    If you want to populate the field with the value of the related record you will have to use Xrm.WebApi to query the related record and use the queried value to populate the field. The easiest way to "pre-cook" you querying script is to use Dataverse Rest Builder tool - github.com/.../DRB

    I'm still not sure why exactly you want to use specifically this approach because there are multiple ways to implement your requirements in a no-code way:

    * use Quick View Form

    * use Calculated Field

    * use the Workflow that will handle the change of the lookup and update your field

    * use Form Control PCF control - nishantrana.me/.../

Helpful resources

Quick Links

Dynamics 365 Community Update – Sep 9th

Welcome to the next edition of the Community Platform Update. This is a weekly…

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,263 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,112 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans