Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

fetchXML not storing value on field

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

Hi experts,

I created a javascript web resource to store the count of the number of cases created in the past 90 days and display on custom field new_casecount in the account record.

Here is my code. However, I'm getting error:

Screen-Shot-2018_2D00_04_2D00_25-at-5.08.08-PM.png

Here is my code:

function CountCasesPast90Days() 
{
	var GUIDvalue = Xrm.Page.data.entity.getId();
	alert(GUIDvalue);
	var fetchXml = "<fetch mapping='logical' distinct='false' aggregate='true'>"
	+ "<entity name='incident'>"
	+ "<attribute name='title' alias='recordcount' aggregate='count' />"
	+ "<filter type='and'>"
	+ "<condition attribute='createdon' operator='last-x-days' value='90' />"
	+ "</filter>"
	+ "<link-entity name='account' from='accountid' to='customerid' link-type='inner' alias='ab'>"
	+ "<filter type='and'>"
	+ "<condition attribute='accountid' operator='eq' value='"+GUIDvalue+"' />"
	+ "</filter>"
	+ "</link-entity>"
	+ "</entity>"
	+ "</fetch>";
	alert(fetchXml);
    Xrm.Page.getAttribute(new_casecount).setValue(fetchXml);

}


Both my alert messages are showing data but at the last line, im getting error.

Thanks,

Jon

*This post is locked for comments

  • Shaminderpal Singh Profile Picture
    Shaminderpal Singh 1,565 on at
    RE: fetchXML not storing value on field

    Welcome Jon!!

    Glad to know it solved your issue :)

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: fetchXML not storing value on field

    Hi Shaminder,

    Thank you so much!! It works now! Really appreciate your help till I got it right.

    Best,

    Jon

  • Verified answer
    Shaminderpal Singh Profile Picture
    Shaminderpal Singh 1,565 on at
    RE: fetchXML not storing value on field

    Replace the below

    var totalcount = results.value.length;

    with

    var totalcount = results.value[0].recordcount;

    -Shaminder

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: fetchXML not storing value on field

    Hi Shaminder,

    Thanks for your reply.

    Now, it is storing only "1" in the records has more associated cases created in the last 90 days.

    Even if an has 3 cases in last 90 days it is only storing 1.

    Thanks,

    Jon

  • Verified answer
    Shaminderpal Singh Profile Picture
    Shaminderpal Singh 1,565 on at
    RE: fetchXML not storing value on field

    Hi Jon

    Replace incident with incidents in below line

      var entityname = "incidents";

    -Shaminder

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: fetchXML not storing value on field

    Hi Shaminder,

    Thanks for your reply.

    Now, I'm not getting any error onLoad but the value is still not showing on the custom field. 

    So I again checked using F12 and got the following error:

    Screen-Shot-2018_2D00_04_2D00_27-at-12.16.30-PM.png

    Thanks,

    Jon

  • Verified answer
    Shaminderpal Singh Profile Picture
    Shaminderpal Singh 1,565 on at
    RE: fetchXML not storing value on field

    Hi Jon,

    From your screenshot,I can see we are getting an error of cors and thus 401 unauthorized.

    Do the following change in your code

    function CountCasesPast90Days() {

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

               alert(GUIDvalue);

               var fetchXml = "<fetch mapping='logical' distinct='false' aggregate='true'>"

               + "<entity name='incident'>"

               + "<attribute name='title' alias='recordcount' aggregate='count' />"

               + "<filter type='and'>"

               + "<condition attribute='createdon' operator='last-x-days' value='90' />"

               + "</filter>"

               + "<link-entity name='account' from='accountid' to='customerid' link-type='inner' alias='ab'>"

               + "<filter type='and'>"

               + "<condition attribute='accountid' operator='eq' value='" + GUIDvalue + "' />"

               + "</filter>"

               + "</link-entity>"

               + "</entity>"

               + "</fetch>";

               alert(fetchXml);

               var fetch = encodeURI(fetchXml);

               var entityname = "incident";

               var serverURL = Xrm.Page.context.getClientUrl();

               var Query = entityname + "?fetchXml=" + fetch;

               var req = new XMLHttpRequest();

               req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.0/" + Query, false);   //Removed api from url domain

               req.setRequestHeader("Accept", "application/json");

               req.setRequestHeader("Content-Type", "application/json; charset=utf-8");

               req.setRequestHeader("OData-MaxVersion", "4.0");

               req.setRequestHeader("OData-Version", "4.0");

               req.send();

               if (req.readyState == 4) { /* complete */

                   req.onreadystatechange = null;

                   if (200 == req.status) {

                       debugger; // Try to debug in this point to get the count if any error you get

                       var results = JSON.parse(req.response);

                       var totalcount = results.value.length;

                       Xrm.Page.getAttribute("new_casecount").setValue(totalcount);

                   }

                   else {

                       console.log(req.statusText);

                   }

           }

    }

    Hope it helps

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: fetchXML not storing value on field

    Hi Shaminder,

    Here's my updated code:

    function CountCasesPast90Days() {
                var GUIDvalue = Xrm.Page.data.entity.getId();
                alert(GUIDvalue);
                var fetchXml = "<fetch mapping='logical' distinct='false' aggregate='true'>"
                + "<entity name='incident'>"
                + "<attribute name='title' alias='recordcount' aggregate='count' />"
                + "<filter type='and'>"
                + "<condition attribute='createdon' operator='last-x-days' value='90' />"
                + "</filter>"
                + "<link-entity name='account' from='accountid' to='customerid' link-type='inner' alias='ab'>"
                + "<filter type='and'>"
                + "<condition attribute='accountid' operator='eq' value='" + GUIDvalue + "' />"
                + "</filter>"
                + "</link-entity>"
                + "</entity>"
                + "</fetch>";
                alert(fetchXml);
               
    
    
                var fetch = encodeURI(fetchXml);
                var entityname = "incident";
                var serverURL = Xrm.Page.context.getClientUrl();
                var Query = entityname + "?fetchXml=" + fetch;
                var req = new XMLHttpRequest();
                req.open("GET", "tpcus.api.crm.dynamics.com/.../v9.0" + Query, false);
                req.setRequestHeader("Accept", "application/json");
                req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
                req.setRequestHeader("OData-MaxVersion", "4.0");
                req.setRequestHeader("OData-Version", "4.0");
                req.send();
                
                if (req.readyState == 4) { /* complete */
                    req.onreadystatechange = null;
                    if (200 == req.status) {
                        debugger; // Try to debug in this point to get the count if any error you get
                        var results = JSON.parse(req.response);
                        var totalcount = results.value.length; 
                        Xrm.Page.getAttribute("new_casecount").setValue(totalcount);
                    }
                    else {
                        alert(req.statusText);
                    }
            }
    
    
    }


    Here is the screenshot as requested:

    Screen-Shot-2018_2D00_04_2D00_27-at-10.18.31-AM.png

    Thanks,

    Jon

  • Suggested answer
    Shaminderpal Singh Profile Picture
    Shaminderpal Singh 1,565 on at
    RE: fetchXML not storing value on field

    do one thing Press F12 change the frame from top to customscriptframe which I have highlighted in screenshot.

    enter the name of your function and press enter .You will get an error and next to it will be a number,click on it,it will open the line of code where it is breaking.

    4747.Capture1.PNG

    Please paste your updated fn here also so I can see the code.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: fetchXML not storing value on field

    Screen-Shot-2018_2D00_04_2D00_26-at-7.22.49-PM.png

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

Announcing Our 2025 Season 1 Super Users!

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

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,494 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,307 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans