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 365 | Integration, Dataverse...
Suggested Answer

Get record ID and display it on a new field

(0) ShareShare
ReportReport
Posted on by 760

Hi,

I need to get the record id and display it in my new field. I have this simple JavaScript, I don't know why it is not working. I've also added in "OnSave" in form.

The new field remains blank/empty whenever a new record was created and saved

function getID()
{
 var recordID =Xrm.Page.data.entity.getId();
Xrm.Page.getAttribute("new_field").setValue('recordID');
  }

I have the same question (0)
  • Suggested answer
    P. R. M Profile Picture
    739 on at

    Hi Jane,

    On Save function executes in Asynchronouslys. So, ID might not be generated by the time of function calling. Please use SetTimeout to overcome this. PFB for code:

    function getID() {

       if (Xrm.Page.data.entity.getId() == "") {

           setTimeout(function () {

               getID();

           }, 500);

       }

       else {

           var recordID = Xrm.Page.data.entity.getId();

           Xrm.Page.getAttribute("new_field").setValue('recordID');

       }

    }

    Lets give a try and let me know if any clarifications required

  • Pankaj Gogoi Profile Picture
    3,177 on at

    Hi Jane,

    I think it's good to handle using a Plugin at post operation.

    During save the ID is not generated so you will get empty value. You can call this function onLoad, so after saving the form it will reload and you will have the value but you need to save the form again to update the "new_field" value.

    Here is the code if you are calling on save. I haven't test the code but it should work.

    function getId(executionContext){
        var formContext = executionContext.getFormContext();
        formContext.data.save().then(function(){
            if(formContext.getAttribute("new_field").getValue() == null){
                var id = formContext.data.entity.getId();
                var recordID =Xrm.Page.data.entity.getId();
                Xrm.Page.getAttribute("new_field").setValue(recordID);
                
            }        
        });
    }

    if it is not working onSave, then call this function onChange of modifiedon field. This gets updated each time save call has returned.

    https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/reference/formcontext-data/save

    Hope this helps

    Best Regards

    PG

  • CRMexplorer Profile Picture
    760 on at

    Hi,

    Thank you ! it still empty tho. I've just changed the event to onload and that one works

  • Pankaj Gogoi Profile Picture
    3,177 on at

    Glad it worked. You can also try out the onChange event on modifiedon field.

    Please mark the thread as closed.

    Best Regards

    PG

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 365 | Integration, Dataverse, and general topics

#1
Martin Dráb Profile Picture

Martin Dráb 58 Most Valuable Professional

#2
#ManoVerse Profile Picture

#ManoVerse 42

#3
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 38 Super User 2025 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans