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 value for fields using JavaScript

(0) ShareShare
ReportReport
Posted on by 75

Hello guys,

This is actually can consider related to my previous thread, about Web resources for Open a new form then Save -> https://community.dynamics.com/crm/f/microsoft-dynamics-crm-forum/430042/open-create-form-and-save-instantly

But it may not necessary also.

Issue is about specifying a default value for my fields, while having this kind of script ->

function OpenAndSaveForm(executionContext) {
//Get the form context
var formContext = executionContext.getFormContext();
// Get the form type
var formType = formContext.ui.getFormType();
if(formType == 1) { //create form
    try {

             formContext.data.entity.save();
        }
    catch (e) {
        Xrm.Utility.alertDialog(e.message);
        }
   }
}

So because I want to have a default value in my 2 fields (xyz_description and xyz_date), I was adding these to statements :

function OpenAndSaveForm(executionContext) {
//Get the form context
var formContext = executionContext.getFormContext();
// Get the form type
var formType = formContext.ui.getFormType();
if(formType == 1) { //create form
    try {

        formContext.getAttribute("xyz_description").setValue("Orders"); 

        formContext.getAttribute("xyz_date").setValue(new Date());  


        formContext.data.entity.save();
        }
    catch (e) {
        Xrm.Utility.alertDialog(e.message);
        }
   }
}

The lines in red are my addition, but currently produces an error saying "Cannot read property 'setValue' of null"

What would be the cause of it ? Is it correct that inside those "formContext.getAttribute" is my field name ? or should be other which makes it error. ?

Kindly advice.

Thanks

I have the same question (0)
  • Suggested answer
    meelamri Profile Picture
    13,218 User Group Leader on at

    Hi MedWong,

    Your code seems to be correct. However, I think that there is an issue regarding the field name "xyz_description". In this context, I recommend you to use a Chrome extension that will facilitate your developments: https://chrome.google.com/webstore/detail/dynamics-365-power-pane/eadknamngiibbmjdfokmppfooolhdidc

    In the example below, I use the "Schema Name in Brackets" feature, which allows me to have the right schema name for the Description field. You should use this schema name when calling the function formContext.getAttribute('description').setValue(value).

    pastedimage1626623423690v1.png

  • MedWong Profile Picture
    75 on at

    Hi,

    Will try that, although actually I didn't use Chrome when customize this, I'm using Edge. And gonna be another journey to learn how to use that extension.

    However before that, what is exactly schema name ? is that the "xyz" ? and I believe I did use bracket.

    Thanks,

  • Suggested answer
    meelamri Profile Picture
    13,218 User Group Leader on at

    Please refer to this blog for more details about field's naming (Display Name, Schema Name and Logicial Name) : taagung.com/.../

    For your code, I believe you need to use "formContext.getAttribute('description').setValue(value)"

  • MedWong Profile Picture
    75 on at

    Hmmm.. thanks,

    So in my understanding, this schema name is related to database, and it can be changed during creation only, am I right to say that?

    However, I tried to used the same but still an error.

    Isn't right to say that in the Table - Tab Columns, where you usually have grid for "Display name" - "Name", then the schema name you can reach by clicking the column (field) ?

    So if I have something like this :

    pastedimage1626627743452v1.png

    when I click "Description", then it will pop up a window ->

    pastedimage1626627800808v2.png

    The highlighted is the schema name ?

    Thanks

  • Suggested answer
    meelamri Profile Picture
    13,218 User Group Leader on at

    Hi, 

    you need to use this one: 

    pastedimage1626628554623v1.png

  • MedWong Profile Picture
    75 on at

    hhmm... that actually my first tried. the "xyz_description" which I type on the first thread is actually "jm_description"

    So like this in actual :

    function OpenAndSaveForm(executionContext) {

    //Get the form context

    var formContext = executionContext.getFormContext();

    // Get the form type

    var formType = formContext.ui.getFormType();

    if(formType == 1) { //create form

       try {

           formContext.getAttribute("jm_description").setValue("Orders");

           formContext.getAttribute("jm_date").setValue(new Date());  

           formContext.data.entity.save();

           }

       catch (e) {

           Xrm.Utility.alertDialog(e.message);

           }

      }

    }

    Thanks

  • meelamri Profile Picture
    13,218 User Group Leader on at

    Do you have a new issue at this point?

  • MedWong Profile Picture
    75 on at

    Yes, it always give me the error "Cannot read property 'setValue' of null.

    pastedimage1626630244016v1.png

    But if I remove those 2 lines, the error is disappeared and the form open (and save) as intended, only for those fields are empty.

    Thanks

  • necsa Profile Picture
    3,455 on at

    Hi,

    Your code is correct. I think your registration of Javascript is wrong. Please check it. I try with the same code and same field and it is working.

  • Verified answer
    necsa Profile Picture
    3,455 on at

    Are both fields on the Form or not?

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!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
ManoVerse Profile Picture

ManoVerse 184 Super User 2026 Season 1

#2
11manish Profile Picture

11manish 125

#3
CU11031447-0 Profile Picture

CU11031447-0 100

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans