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');
  }

  • Pankaj Gogoi Profile Picture
    3,177 on at
    RE: Get record ID and display it on a new field

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

    Please mark the thread as closed.

    Best Regards

    PG

  • CRMexplorer Profile Picture
    760 on at
    RE: Get record ID and display it on a new field

    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
    RE: Get record ID and display it on a new field

    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

  • Suggested answer
    P. R. M Profile Picture
    739 on at
    RE: Get record ID and display it on a new field

    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

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 293,274 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,947 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Product updates

Dynamics 365 release plans