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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

set default value on a field

(0) ShareShare
ReportReport
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 

I have the same question (0)
  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    So here are a few thoughts:

    1. Function Name according to your code should be "setTextField" and not "addPreSearch".

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

    3. If the value you want to set is lookup then the value you assign should look like

    [{id: "Guid of the record here", name: "Display Name of the record here", entityType: "logical name of the field here"}]

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

  • DiePic Profile Picture
    448 on at

    Hi a33ik 

    thanks for your kindly reply.

    Really I'm reading a lot of rows about this, but it's not explained for people who is starting  .... or maybe I'm not so good for this .... I find a lot of difficulty to understand how to use it. 

    If you have any links where I can learn, it will be appreciated.

    Please, can give me some more details on point 2 and 3?

    Thanks in advance

  • Eiken Profile Picture
    Microsoft Employee on at

    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);
    
    }

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    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").

  • DiePic Profile Picture
    448 on at

    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

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Then everything should be correct.

  • DiePic Profile Picture
    448 on at

    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.

  • Suggested answer
    Eiken Profile Picture
    Microsoft Employee on at

    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
    448 on at

    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
    Microsoft Employee on at

    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.

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
ManoVerse Profile Picture

ManoVerse 168 Super User 2026 Season 1

#2
NeerajPawar Profile Picture

NeerajPawar 65

#3
Jimmy Passeti Profile Picture

Jimmy Passeti 51 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans