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

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Default Dates in Model...
Power Apps
Answered

Default Dates in Model Driven App forms

(0) ShareShare
ReportReport
Posted on by 409

@HSheild (as ever) provided me with a great solution to inheriting a date from a parent to a child in a sub-form.

 

https://powerusers.microsoft.com/t5/Common-Data-Service-for-Apps/Default-date-and-testing-of-date-logic/m-p/465071

 

I now discover that something on the face of it which is more straightforward than that is...not so straighforward.

 

I am trying to populate some default values:

 

I see that in creating a new field in an entity, I can assign a default value to an Option Set type, but not to any other kind of field type? I want the form to default to "Today" in the case of a date field and a specific item in the case of some lookup fields.

 

I have looked at Calculated Fields =Now() but of course that just sets the field on save, not offers a default.

 

Any ideas?

 

Ged

I have the same question (0)
  • GarethPrisk Profile Picture
    2,828 on at
    Take a look at Business Rules.
    They have Default actions, execute in real-time for the user, and are low/no-code to implement.
    Not at a computer, otherwise I'd post some examples. Sorry!
  • ghdunn Profile Picture
    409 on at

    Thanks @GarethPrisk 

     

    I want to populate the values on the opening of the form, not on the closing.  Is that possible with Business Rules?  It seems to me the only condition is a test of a field value, which suggests to me it is on closing?

     

    Thanks

     

    Ged

  • Verified answer
    v-siky-msft Profile Picture
    on at

    @ghdunn 

    Please try to Create a new Web Resource to hold the JScript. This JScript Library should contain the following code:

     

    // Prefill a Date Field with Todays Date 
    
    function SetToday(date)
    { 
    var isCreateForm = Xrm.Page.ui.getFormType() == 1;
    var dateField = Xrm.Page.getAttribute(date);
     if (isCreateForm) // Check that this is a new Record
     { dateField.setValue(new Date()); // Set the Date field to Today
     dateField.setSubmitMode("always"); // Save Disabled Fields
     }
    }

     

    To install:
    1.  Create a Web Resource and add the above script to the Web Resource
    2.  Add the Web Resource as a Form library and set the forms OnLoad event to the SetToday Function.
             a.   Customize the form, select FormProperties, Events Tab. 
             b.   Add a Library, Under EventHandlers, Control = Form, Event=OnLoad
             c.   In the function field, type in "SetToday"
             d.   In the box that says "comma separated list of parameters that will be passed to the function" add the Schema Name of the field you wish to default as a parameter.   Schema Name must be in double quotes (ie.  "myfield_schemaname")
    Snipaste_2020-02-14_15-24-49.pngSnipaste_2020-02-14_15-23-51.pngSnipaste_2020-02-14_15-23-26.pngSnipaste_2020-02-14_15-25-18.png
    Sik
  • ghdunn Profile Picture
    409 on at

    @v-siky-msft @GarethPrisk 

     

    Great guys..thank you.  Will have a look at that over the weekend and leave feedback.

     

    Ged

  • v-siky-msft Profile Picture
    on at

    Hi @ghdunn 

     

    How about the workaround? Is there any progress?

    Sik

  • ghdunn Profile Picture
    409 on at

    Sik

    @v-siky-msft @GarethPrisk 

     

    So very sorry guys...just got tied up in something else and will be OOO for a couple of days.  I am sure this will work and will test as soon as I am able. Rest assured, I will leave feedback as soon as I am confident.  Sorry i haven't turned this around as quickly as I planned.

     

    Ged

  • Ranithas Profile Picture
    34 on at

    @v-siky-msft is there a way to set default to week number instead of date?

    Please advice. Thank you. 

  • samg1 Profile Picture
    4 on at

    Hi, 

    I have followed the above steps but getting error while loading form. 

    Error Message "Cannot read properties of null (reading 'setValue')".

    Can someone help me with this!

     

    samg1_0-1663331268976.png

     

  • Sonepar_Wissem Profile Picture
    20 on at

    I'm having the same error. Did you figure it out ??

  • Sonepar_Wissem Profile Picture
    20 on at

    I think I got this figured out.

     

    First of all you should use the logical name instead of the schema name. You can get the logical name by going to

    • Solution > Tables > {Your table name} > Columns
    • Select the 3 dots beside the Display Name of the column you need to set
    • Advanced > Tools > Copy logical Name.

    Also, Xrm.Page is deprecated, so you should pass your execution context as parameter and select that option when adding your Web resource. Your JS file will look like this:

     

    function setToday(executionContext, columnLogicalName)
    { 
    	var formContext = executionContext.getFormContext();
    	var isCreateForm = formContext.ui.getFormType() == 1;
    	if (isCreateForm) {
    		var dateAttr = formContext.getAttribute(columnLogicalName);
    		dateAttr.setValue(new Date());
    	}
    }

     

     

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

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 711 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 319 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 268

Last 30 days Overall leaderboard