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 :
Power Platform Community / Forums / Power Pages / Count of views of a re...
Power Pages
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.
Categories:
  • Fubar Profile Picture
    17 Super User 2026 Season 1 on at

    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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Leaderboard > Power Pages

#1
M Bilal Khan Profile Picture

M Bilal Khan 110

#2
Mohsin Ali Profile Picture

Mohsin Ali 30

#2
sannavajjala87 Profile Picture

sannavajjala87 30 Super User 2026 Season 2

Last 30 days Overall leaderboard