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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

CRM 2011: Change Value OnCreate on Form with JS

(0) ShareShare
ReportReport
Posted on by
I want to change the value of a field with javascript on the OnSave Event of a Form in CRM 2011. This should only occur if the entity is created. This means I check the FormMode for OnCreate and then try to change a field value with: Xrm.Page.data.entity.attributes.get("field").setValue("asd"); Xrm.Page.data.entity.save(); or Xrm.Page.getAttribute("field").setValue("asd"); Xrm.Page.data.entity.save(); neither of them is working. How do I save/change the value OnCreate with JavaScript in a CRM 2011 Form?

*This post is locked for comments

I have the same question (0)
  • Community Member Profile Picture
    on at

    seems I cannot edit the formatting:

    I want to change the value of a field with javascript on the OnSave Event of a Form in CRM 2011. This should only occur if the entity is created.

    This means I check the FormMode for OnCreate and then try to change a field value with:

    Xrm.Page.data.entity.attributes.get("field").setValue("asd");

    Xrm.Page.data.entity.save();

    or

    Xrm.Page.getAttribute("field").setValue("asd");

    Xrm.Page.data.entity.save();

    neither of them is working. How do I save/change the value OnCreate with JavaScript in a CRM 2011 Form?

  • jlattimer Profile Picture
    24,564 on at
    function OnSave {
        var formType = Xrm.Page.ui.getFormType();
        if (formType === 1) {
            Xrm.Page.getAttribute("new_field").setValue("asd");
        }
    }
    

    Are you sure the code that is setting the field value is getting executed? If you aren't sure you might throw some alert messages to verify. If you are executing this code during the OnSave event of the form there isn't really a need to call the save method manually. 

    Is the field you are trying to set read-only? if so, you would need to force the field to submit using:

    Xrm.Page.getAttribute("new-field").setSubmitMode("always");
  • Community Member Profile Picture
    on at

    my actual code look like

    var field = Xrm.Page.getAttribute('asd);

    if (field != null) {

                   field.setSubmitMode("always");

                   field.setValue(value + "-" + fieldName.getValue());

               }

    I verified that the code is running. It looks like the Value is written on the form, but then the form refreshes/reloads and my custom value is gone.

    I am calling this in a ajax request success function. May this be the problem?

  • Verified answer
    jlattimer Profile Picture
    24,564 on at

    Yes - that very well could be your issue. If your ajax request is running asynchronously the save of the page is likely completing before the call fully completes.  Make sure your ajax request is running synchronously so that all processing finishes before the save happens.

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

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
ScottDurow Profile Picture

ScottDurow 2

#2
GJones Profile Picture

GJones 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans