Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Power Pages - Customize & Extend
Unanswered

Count of views of a record

(0) ShareShare
ReportReport
Posted on by 56
$(document).ready(function() {
    debugger;
    var currentViewCount = $("#new_viewcount").val();
    var newViewCount = currentViewCount === "" ? 1 : parseInt(currentViewCount) + 1;
    $("#new_viewcount").val(newViewCount);

    // Save view count
    saveViewCount(newViewCount);
});

function saveViewCount(viewCount) {
    var entityId = Xrm.Page.data.entity.getId();
    var entityName = Xrm.Page.data.entity.getEntityName();
   
    var entity = {};
    entity.new_viewcount = viewCount;
   
    var req = new XMLHttpRequest();
    req.open("PATCH", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/" + entityName + "(" + entityId.replace("{", "").replace("}", "") + ")", true);
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
    req.onreadystatechange = function() {
        if (this.readyState === 4) {
            req.onreadystatechange = null;
            if (this.status === 204) {
                console.log("View count updated successfully.");
            } else {
                console.error("Failed to update view count.");
                console.error(this.statusText);
            }
        }
    };
    req.send(JSON.stringify(entity));
}
 
The above mentioned code is to calculate the view count of a record views by users in order page and it is giving me this "ReferenceError: Xrm is not defined at saveViewCount" so can any one provide solution for this and I need to calculate count of record views on click of view record action button by the number of users in the list view and view count is also not getting saved to the data base.
  • Fubar Profile Picture
    7,960 Super User 2025 Season 1 on at
    Re: Count of views of a record

    Power Pages does not expose the Xrm object, also to do what you want to do you would make use of the Power Pages WebAPI - make use of the Safe Ajax wrapper in the second link (also has a simple update snippet below it) 

    https://learn.microsoft.com/en-us/power-pages/configure/web-api-overview

    https://learn.microsoft.com/en-us/power-pages/configure/write-update-delete-operations#web-api-ajax-samples

     

    Generally the guid of the record will be the Request object by default in a parameter named 'id' (but could be something else if configured to not be the default)

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

Michael Gernaey – Community Spotlight

We are honored to recognize Michael Gernaey as our June 2025 Community…

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard >

Featured topics