Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Answered

Set value for fields using JavaScript

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

  • MedWong Profile Picture
    MedWong 75 on at
    RE: Set value for fields using JavaScript

    Hello Necdet,

    It works! So that is the actually the cause, one of the fields is not on the form. Should've realize that sooner but I missed that.

    Thank you very much everyone, you all've been so great!

    rgds,

  • Verified answer
    necsa Profile Picture
    necsa 3,455 on at
    RE: Set value for fields using JavaScript

    Are both fields on the Form or not?

  • necsa Profile Picture
    necsa 3,455 on at
    RE: Set value for fields using JavaScript

    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.

  • MedWong Profile Picture
    MedWong 75 on at
    RE: Set value for fields using JavaScript

    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

  • meelamri Profile Picture
    meelamri 13,204 User Group Leader on at
    RE: Set value for fields using JavaScript

    Do you have a new issue at this point?

  • MedWong Profile Picture
    MedWong 75 on at
    RE: Set value for fields using JavaScript

    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

  • Suggested answer
    meelamri Profile Picture
    meelamri 13,204 User Group Leader on at
    RE: Set value for fields using JavaScript

    Hi, 

    you need to use this one: 

    pastedimage1626628554623v1.png

  • MedWong Profile Picture
    MedWong 75 on at
    RE: Set value for fields using JavaScript

    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
    meelamri 13,204 User Group Leader on at
    RE: Set value for fields using JavaScript

    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
    MedWong 75 on at
    RE: Set value for fields using JavaScript

    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,

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans