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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

JavaScript In Onload

(0) ShareShare
ReportReport
Posted on by

I use the code Xrm.Page.data.entity.save() during OnLoad so that the record is automatically created. This is needed as a workflow is triggered when the record is created to update some fields.

1. Is there a better way of doing this ? When I click the + to add a record on the subgrid, I am shown a new page but it seems the records is not created until I click save. This is causing confusion for many users as they are not aware that they have to click save for some fields to auto populate.

2. If there is no better way of doing this how can I stop the JavaScript from firing when the record has been created as if I click refresh / F5 it creates multiple records.

Thanks.

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Royal King Profile Picture
    27,686 on at

    I would suggest to pre-populate data using javascript in onload event rather auto saving the record just to pre-populate some data on the form.

    If you save the form in onload it will create lot of unwanted record in the system over the period of time. If you still want to go with approach

    Call the save method only if form type is 1  if(xrm.page.ui.getformtype() ==1)  { } this will avoid creating new records when user opens existing record or refreshes.

  • Community Member Profile Picture
    on at

    Thanks for that. How do I pre populate. I would need to get contact data. The form to be populated does have a relation and has the contact lookup field.

    Thanks.

  • Royal King Profile Picture
    27,686 on at

    Whatever logic you have in the workflow to populate contact  do the same in jvascript.

  • Community Member Profile Picture
    on at

    Yes that would be the plan, but I don't know JS :(

  • Suggested answer
    Nuno Profile Picture
    2,005 on at

    If you have the data you need to populate the form in related entity contact, you can get the data from contact with CrmRestKit:

    CrmRestKit.Retrieve('Contact', contactId, ['field1', 'field2'])

    Here you can find how to work with CrmRestKit

    www.dynamic-crm.com/javascript-in-crm-2011

    crmrestkit.codeplex.com/documentation

    Then the set the value of each field: Xrm.Page.getAttribute(field).setValue(value)

    msdn.microsoft.com/.../gg334409.aspx

  • Community Member Profile Picture
    on at

    Interesting discussion.

    harihar

  • Saroj Das Profile Picture
    3,355 on at

    Hello henda, you can check the form type before executing the javascript code. Please check the link it might help.

    msdynamicscrmblog.wordpress.com/.../get-form-types-and-modes-in-dynamics-crm-2013

    regards,

    Saroj

  • Community Member Profile Picture
    on at

    When I use the following code I get an error On Load saying xrm is undefined.

    function Save()

    {

     if(Xrm.Page.ui.getFormType()==1)

     {

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

     }

    }

    I have just done a repair install of my CRM as things were not working to well. I have since re-applied SP1. Does this error have to do with any missing patch ?

    Its more likely the JS code as I'm no expert when it comes to JS.

    Thanks.

  • Suggested answer
    Saroj Das Profile Picture
    3,355 on at

    Hi Henda, If you are running your code in IFrame, then please add the below code.

    <script type="text/javascript" src="../../../ClientGlobalContext.js.aspx"></script>

    Regards,

    Saroj

  • Community Member Profile Picture
    on at

    AFAIK, its not an IFrame. The JS is stored in a Web Resource and used on the standard form.

    The code: Xrm.Page.data.entity.save(); worked before I had to repair CRM so I'm thinking something is missing.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans