Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 Community / Forums / Sales forum / set default value on a...
Sales forum
Answered

set default value on a field

Posted on by 448

Hi all

this is my approach to events and javascript on D365s fields. 

I want set the default value of this lookup field.

pastedimage1670063238950v1.png    pastedimage1670064837689v1.png

this is my library/javascript (cr07a_ATHsetLookupFieldUtentiCollegatiOpportunity)

//-------------------------------------------------------------------------------------------
// setTextField 
//-------------------------------------------------------------------------------------------

function setTextField(e){

// Get the Form Context
var formContext = e.getFormContext();

// Set the 'User Scope'[dev1_idcollegamentocr07a_athambuteopportunity] to a variable
var ValDefault = "Customer Relationship Management";     <-- (here I try also with IDvalue [078d7702-2581-ec11-8d21-0022489ea185])
formContext.getAttribute("dev1_idcollegamentocr07a_athambuteopportunity").setValue(ValDefault);

}

but the default "Customer Relationship Management" is not show on field.

Where I'm mistaken?

thanks for any reply and suggestion 

  • Verified answer
    Eiken Profile Picture
    Eiken on at
  • DiePic Profile Picture
    DiePic 448 on at
    RE: set default value on a field

    Hi Eiken 

    that was a good study session for me ... thanks a lot.

    Now I must resolve the error on the script, that change data every time that I open the page and when the field is compiled, I wan't .

    Can you give me some link to study as well javascript for D365s?

  • Suggested answer
    Eiken Profile Picture
    Eiken on at
    RE: set default value on a field

    Hi,
    You are welcome.
    You didn't find all events list because you select the wrong item which should be added the event.

    pastedimage1670404101413v1.png

    This may help you find the right form. Then the On Load event will show on the right.

  • DiePic Profile Picture
    DiePic 448 on at
    RE: set default value on a field

    Hi Eiken 

    thanks for your kind help.

    I dont find the event OnLoad on "Event Type", in the list appears only the event "In case of modify" .... where I'm mistaken?

    What I must to do to have all events listed?

    pastedimage1670401723098v1.png

  • Suggested answer
    Eiken Profile Picture
    Eiken on at
    RE: set default value on a field

    Hi,

    1.First go to the Edit form page and select the main form.

    pastedimage1670394651153v1.png

    2.Click the event and add On Load event.

    pastedimage1670394806218v2.png

  • DiePic Profile Picture
    DiePic 448 on at
    RE: set default value on a field

    Hi Eiken 

    As indicated from a33ik I must assign the value on load of the form.... please can you indicate me how to?

    thanks for your help.

  • a33ik Profile Picture
    a33ik 84,321 Most Valuable Professional on at
    RE: set default value on a field

    Then everything should be correct.

  • DiePic Profile Picture
    DiePic 448 on at
    RE: set default value on a field

    Hi a33ik 

    I miss your question (Also I have a question - when do you want to assign that default value? It's logical to do that on the form onload.)

    Yes I want assign default value on load form

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,321 Most Valuable Professional on at
    RE: set default value on a field

    Answering your questions.

    "Pass Execution Context":

    pastedimage1670253400552v1.png

    In regards to your code - id should be the Guid (like "dc45213e-bf48-ed11-bba1-0022482dbb3a") of the record and entityType should be the Logical Name (like "account").

  • Eiken Profile Picture
    Eiken on at
    RE: set default value on a field

    Hi,

     

    You need to check the following four points.

    1.  In order for javascript to work, we should add the event to the field which is regarded as a trigger.

     For example ,you want to set the default value to field A when the field B change, then you can add the event to the field B.

            2.  Check the "Pass Execution Context as a First Parameter" checkbox in the even handler.

            3.  The Function Name should be “setTextField” not “addPreSearch” according to your code.

     pastedimage1670230178315v1.png

             4.  Because you’re setting the default value for a look up field, we can’t set a string for it but an array instead.

    I tried to modify your code for your reference.

     

    function setTextField(e){
    
    // Get the Form Context
    
    var formContext = e.getFormContext();
    
    var ValDefault=new Array();
    
    ValDefault[0] = new Object();
    
    ValDefault[0].id ="The Guid of the record";
    
    //(Guid is the unique id of the record, you can find it by click on the table corresponding to the lookup field, or find it in the URL of the record which you selected as the default value)
    
    ValDefault[0].entityType ="The logical name of the entity which associates with the lookup field ";
    
    ValDefault[0].name = " Customer Relationship Management";    
    
    // Set the 'User Scope'[dev1_idcollegamentocr07a_athambuteopportunity] to a variable
    formContext.getAttribute("dev1_idcollegamentocr07a_athambuteopportunity").setValue(ValDefault);
    
    }

Helpful resources

Quick Links

Dynamics 365 Community Update – Sep 16th

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,339 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,177 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans