locked
CRM 2013 Soap /Rest Cliecnt JScript Example RRS feed

  • Question

  • Does any one have example for Counting the Contact associted for an Account sing the JScript through SOAP calls or REST End points??
    Wednesday, June 18, 2014 2:33 PM

Answers

  • Try with this code:

    function getTotalContacts(accountId) {
    	var serverUrl;
    	if (Xrm.Page.context.getClientUrl !== undefined) {
    		serverUrl = Xrm.Page.context.getClientUrl();
    	} else {
    		serverUrl = Xrm.Page.context.getServerUrl();
    	}
    	
    	var ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc"; 
    	var contactsRequest = new XMLHttpRequest();
    	
    	contactsRequest.open("GET", ODataPath + "/ContactSet?$select=ContactId&$filter=ParentCustomerId/Id eq guid'" + accountId + "'", false); 
    	contactsRequest.setRequestHeader("Accept", "application/json"); 
    	contactsRequest.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    	contactsRequest.send();
    	if (contactsRequest.status === 200) {
    		var retrievedContacts = JSON.parse(contactsRequest.responseText).d;
    		return retrievedContacts.results.length;
    	} else {
    		return "error";
    	}
    }

    and to use it

    var total = getTotalContacts("52F4F268-19C6-E323-8706-000C2983455B");


    My blog: www.crmanswers.net - Rockstar 365 Profile

    • Marked as answer by Nicksoft2011 Wednesday, June 18, 2014 3:28 PM
    Wednesday, June 18, 2014 2:57 PM
  • That is it.i tried OData URL, but never knew that you can Inject the JSON request like this and get the count easily out of JSON.parse method. very efficient. thanks for your code and quick guidance here.

    Is there any good site to learn OData quesry options and Injecting this kind of JSON request like this??

    • Marked as answer by Nicksoft2011 Friday, June 20, 2014 10:01 PM
    Wednesday, June 18, 2014 3:31 PM
  • I suggest to check OData Query Designer, you can find it inside Dynamics XRM Tools: http://dynamicsxrmtools.codeplex.com/

    Is the one I used to build the correct url for my code.

    My code is synchronous, for small queries like this one I prefer to use synchronous requests instead of asynchronous ones (the truth is that I'm a bit lazy to manage the callbacks :) but I think that synchronous requests are simpler to read and to understand)


    My blog: www.crmanswers.net - Rockstar 365 Profile

    • Marked as answer by Nicksoft2011 Friday, June 20, 2014 10:01 PM
    Wednesday, June 18, 2014 6:24 PM
  • Guido, thanks, this tool is good. Can tou please provide detail, why you are saying this is Syncronous?? is this because it is OData with JSON calls??
    • Marked as answer by Nicksoft2011 Monday, June 23, 2014 12:55 PM
    Wednesday, June 18, 2014 7:40 PM
  • it is synchronous because I set to false the last parameter of the open method:

    var contactsRequest = new XMLHttpRequest();
    contactsRequest.open("GET", ODataPath + "/...", false); 
    	

    so after the send() javascript stop to execute until he receive a response. With asynchronous the javascript will continue to execute (for this is necessary to define a success callback function, function that will be called when the response arrives)

    you can check this site: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Synchronous_and_Asynchronous_Requests


    My blog: www.crmanswers.net - Rockstar 365 Profile

    • Marked as answer by Nicksoft2011 Friday, June 20, 2014 10:01 PM
    Wednesday, June 18, 2014 7:56 PM
  • Guido thanks for your assistance here.

    I am using this tool that you referred and working on Activity count but couldn't able to figure out the URL for OData for counting activity associated with Account.

    For the activity , the relationship is little different because acitivityParty and activitypointer tables are involved behind the scene. can you please take a look and see if there is an easy way to do it??

    thanks,

    Nick

    • Marked as answer by Nicksoft2011 Monday, June 23, 2014 12:55 PM
    Thursday, June 19, 2014 4:59 PM
  • Guido, can you please advice on writing oData Query for Activity associated with Account?? it is little different since activity is special entity.
    • Marked as answer by Nicksoft2011 Monday, June 23, 2014 12:55 PM
    Friday, June 20, 2014 10:02 PM
  • sorry, I didn't see that you asked another question.
    To count all the activities associated to an account you need to consider only ActivityPointer, the OData path will be similar as:

    activitiesRequest.open("GET", ODataPath + "/ActivityPointerSet?$select=Subject&$filter=RegardingObjectId/Id eq guid" + accountId + "'", false); 


    My blog: www.crmanswers.net - Rockstar 365 Profile

    • Marked as answer by Nicksoft2011 Monday, June 23, 2014 12:55 PM
    Friday, June 20, 2014 11:52 PM

All replies

  • Try with this code:

    function getTotalContacts(accountId) {
    	var serverUrl;
    	if (Xrm.Page.context.getClientUrl !== undefined) {
    		serverUrl = Xrm.Page.context.getClientUrl();
    	} else {
    		serverUrl = Xrm.Page.context.getServerUrl();
    	}
    	
    	var ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc"; 
    	var contactsRequest = new XMLHttpRequest();
    	
    	contactsRequest.open("GET", ODataPath + "/ContactSet?$select=ContactId&$filter=ParentCustomerId/Id eq guid'" + accountId + "'", false); 
    	contactsRequest.setRequestHeader("Accept", "application/json"); 
    	contactsRequest.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    	contactsRequest.send();
    	if (contactsRequest.status === 200) {
    		var retrievedContacts = JSON.parse(contactsRequest.responseText).d;
    		return retrievedContacts.results.length;
    	} else {
    		return "error";
    	}
    }

    and to use it

    var total = getTotalContacts("52F4F268-19C6-E323-8706-000C2983455B");


    My blog: www.crmanswers.net - Rockstar 365 Profile

    • Marked as answer by Nicksoft2011 Wednesday, June 18, 2014 3:28 PM
    Wednesday, June 18, 2014 2:57 PM
  • That is it.i tried OData URL, but never knew that you can Inject the JSON request like this and get the count easily out of JSON.parse method. very efficient. thanks for your code and quick guidance here.

    Is there any good site to learn OData quesry options and Injecting this kind of JSON request like this??

    • Marked as answer by Nicksoft2011 Friday, June 20, 2014 10:01 PM
    Wednesday, June 18, 2014 3:31 PM
  • I suggest to check OData Query Designer, you can find it inside Dynamics XRM Tools: http://dynamicsxrmtools.codeplex.com/

    Is the one I used to build the correct url for my code.

    My code is synchronous, for small queries like this one I prefer to use synchronous requests instead of asynchronous ones (the truth is that I'm a bit lazy to manage the callbacks :) but I think that synchronous requests are simpler to read and to understand)


    My blog: www.crmanswers.net - Rockstar 365 Profile

    • Marked as answer by Nicksoft2011 Friday, June 20, 2014 10:01 PM
    Wednesday, June 18, 2014 6:24 PM
  • Guido, thanks, this tool is good. Can tou please provide detail, why you are saying this is Syncronous?? is this because it is OData with JSON calls??
    • Marked as answer by Nicksoft2011 Monday, June 23, 2014 12:55 PM
    Wednesday, June 18, 2014 7:40 PM
  • it is synchronous because I set to false the last parameter of the open method:

    var contactsRequest = new XMLHttpRequest();
    contactsRequest.open("GET", ODataPath + "/...", false); 
    	

    so after the send() javascript stop to execute until he receive a response. With asynchronous the javascript will continue to execute (for this is necessary to define a success callback function, function that will be called when the response arrives)

    you can check this site: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Synchronous_and_Asynchronous_Requests


    My blog: www.crmanswers.net - Rockstar 365 Profile

    • Marked as answer by Nicksoft2011 Friday, June 20, 2014 10:01 PM
    Wednesday, June 18, 2014 7:56 PM
  • Guido thanks for your assistance here.

    I am using this tool that you referred and working on Activity count but couldn't able to figure out the URL for OData for counting activity associated with Account.

    For the activity , the relationship is little different because acitivityParty and activitypointer tables are involved behind the scene. can you please take a look and see if there is an easy way to do it??

    thanks,

    Nick

    • Marked as answer by Nicksoft2011 Monday, June 23, 2014 12:55 PM
    Thursday, June 19, 2014 4:59 PM
  • Guido, can you please advice on writing oData Query for Activity associated with Account?? it is little different since activity is special entity.
    • Marked as answer by Nicksoft2011 Monday, June 23, 2014 12:55 PM
    Friday, June 20, 2014 10:02 PM
  • sorry, I didn't see that you asked another question.
    To count all the activities associated to an account you need to consider only ActivityPointer, the OData path will be similar as:

    activitiesRequest.open("GET", ODataPath + "/ActivityPointerSet?$select=Subject&$filter=RegardingObjectId/Id eq guid" + accountId + "'", false); 


    My blog: www.crmanswers.net - Rockstar 365 Profile

    • Marked as answer by Nicksoft2011 Monday, June 23, 2014 12:55 PM
    Friday, June 20, 2014 11:52 PM