cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Mahesh_Mohan_
New Member

Using AJAX Request with JSON

I am trying to retrieve a record's data from Dynamics CRM in portals using AJAX request and JSON. Although i have the issue where the resultset of the GET action for AJAX is not getting stored in the variable(json). below is the code which fetches the request. This calls another web template where the JSON setup is done and the fetch query is implemented.

 

 

$(document).ready(function(){

$('#UpdateButton').after('<button id="searchResources" type="button" class="btn btn-primary button" style="margin-left:10px;"><span></span> Search Resources</button>');

$("#searchResources").click(function(){
try {
getAvailableResources();
} catch (e) {
console.log("Search Resources :: " + e.message);
}
});
});

function getAvailableResources(){
debugger;
var webTemplateURL = "";
var workorderid = "";
var workOrderCollection = null;
try {
workorderid = $("#msdyn_name").val();
alert(workorderid);
webTemplateURL = "https://sampleportalurs.powerappsportals.com/table-of-contents/page/getworkorderjson/?workorderid=" + workorderid + "";
workOrderCollection = getResponse(webTemplateURL);
if (workOrderCollection.results.length > 0) {
workOrderCollection.results.forEach(function (workorder) {
alert(workorder.name);
});
}
} catch (e) {
console.log("getAvailableResources :: " + e.message);
}
}
//Function to execute ajax request

function getResponse(webTemplateURL) {
var response = null;
try {
$.ajax({type: "GET", url: webTemplateURL, dataType: 'json', async: false, success: function(json){
response = json;
alert("test"+json);
alert("test"+response);
}});
} catch (e) {
console.log("getResponse :: " + e.message);
}
return response;
}

function isValid(attributes) {
try {
if (attributes != null&& attributes != undefined && attributes != "") {
return true;
}
else {
return false;
}
} catch (e) {
console.log("isValid :: " + e.message);
}
}

 

 

The Web Template Code:

 

 

{% fetchxml workorder %}

<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
<entity name="msdyn_workorder">
<attribute name="createdon" />
<attribute name="msdyn_serviceaccount" />
<attribute name="msdyn_workorderid" />
<attribute name="msdyn_functionallocation" />
<attribute name="msdyn_timetopromised" />
<attribute name="msdyn_timefrompromised" />
<attribute name="msdyn_primaryincidentestimatedduration" />
<order attribute="msdyn_name" descending="false" />
<filter type="and">
<condition attribute="msdyn_name" operator="eq" value='{{request.params["workorderid"]}}' />
</filter>
</entity>
</fetch>

{% endfetchxml %}

{% if workorder.results.entities.size > 0 %}

{

"results":

[
{% for wo in workorder.results.entities %}

{

"name": "{{wo.msdyn_name}}",

"serviceaccount": "{{wo.msdyn_serviceaccount}}"

"workorderid": "{{wo.msdyn_workorderid}}"

"timetopromised": "{{wo.msdyn_timetopromised}}"

"timefrompromised": "{{wo.msdyn_timefrompromised}}"

"estimatedduration": "{{wo.msdyn_primaryincidentestimatedduration}}"
}

{% unless forloop.last %},{% endunless %}

{% endfor %}

]

}

{% else %}

{% if page.adx_entityform %}
{% entityform id: page.adx_entityform.id %}
{% endif %}

{% endif %}

 

 

while i execute this https://sampleportalurs.powerappsportals.com/table-of-contents/page/getworkorderjson/?workorderid=00... result is as the attached image. I believe it is  fetching the record data fromthe JSON. Although on click of the button which inturn uses the ajax request, the results are not getting retrieved. Kindly help me with this issue.issuewith ajax.PNG

1 REPLY 1
chleverenz
Advocate III
Advocate III

Hi Mahesh_Mohan_,

You should uncheck the "show website header and footer" on the pagetemplate for the page carrying your service. This will then just render your jsontemplate and not all the other stuff.

It's a little bit a misleading name because it not only suppresses header and footer, the liquid only is rendered 🙂

 

Additionally i would highly recommend to dedicate one liquid fetch template for the service. So do NOT respond with something like 

{% else %}

{% if page.adx_entityform %}
{% entityform id: page.adx_entityform.id %}
{% endif %}

only because you did not find any workorders. Just return an empty json (or null, which i do not recommend 🙂 )

 

Hope this helps if you haven't found out yet,

  Christian

 

PS bonuspoints if you set the content type of the liquid to application/json 🙂

Helpful resources

Announcements
PA User Group

Welcome to the User Group Public Preview

Check out new user group experience and if you are a leader please create your group

MBAS Attendee Badge

Claim Your Badge & Digital Swag!

Check out how to claim yours today!

secondImage

Are Your Ready?

Test your skills now with the Cloud Skill Challenge.

Top Solution Authors
Users online (58,327)