locked
REST endpoint with javascript RRS feed

  • Question

  • I have tried to follow:

    http://msdn.microsoft.com/en-us/library/gg334427.aspx#BKMK_JScriptDataOperationsJS

    and as usual am getting lost, if I upload the solution all works fine, if I manually enter the files, copy and paste, when I open the htm file in ie and click start nothing happens?  Looking at the html file there is no onclick for the button?

    What I am trying to do is get an overview on how to work with javascipt and HTML5, I would like to populate a combobox with the quotes in the system and when one is selected it then populate a datagrid - does anyone have an easy example to follow or recommend a book?

    regards,

    Matt

    Friday, March 7, 2014 4:56 PM

Answers

  • Hi Matt,

    Also reference this in your HTML page, which allows you to use the Xrm methods

    <script src="./ClientGlobalContext.js.aspx" type="text/javascript"></script>
    Also, if you're embedding the HTML page, you might need to use window.parent.Xrm.... and so on.


    Admin QuikView Solution for CRM 2013


    Monday, March 10, 2014 4:12 PM

All replies

  • Hi Matt,

    Make sure when you're uploading the files individually, they are referencing each other with the correct name. For example, the solution might have files starting with "new_filename" whereas the ones that you're creating might be with "yourorgname_filename".

    Also for what you're trying to achieve here, I would suggest going through this code at "\SDK\SampleCode\JS\RESTEndpoint\JavaScriptRESTRetrieveMultiple" in the CRM SDK. You can use the REST query to fetch all quotes and display them in the dropdown and also fetch details of the specific quote when selected in dropdown. The details can also be fetched similarly using REST in CRM.


    Admin QuikView Solution for CRM 2013

    Monday, March 10, 2014 11:51 AM
  • Have tried this as a webresource file:

    <!DOCTYPE html>
    <html>
    <head>
    <script type="text/javascript">
        function fnbuildODataCall(query) {
            alert("");
    
            var serverUrl = document.location.protocol + "//" + document.location.host + "/" + Xrm.Page.context.getOrgUniqueName();
            alert(serverUrl);
            //var oDataPath = window.location.protocol + "//" + window.location.host + "/xrmservices/2011/organizationdata.svc";
            var oDataPath = Xrm.Page.context.getServerUrl() + "/xrmservices/2011/organizationdata.svc";
            var retrieveReq = new XMLHttpRequest();
            var Odata = oDataPath + query;
            alert(Odata);
            retrieveReq.open("GET", Odata, false);
            retrieveReq.setRequestHeader("Accept", "application/json");
            retrieveReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
            retrieveReq.onreadystatechange = function () { RetrieveReqCallBack(this); };
            retrieveReq.send();
    
        }
    
        function RetrieveReqCallBack(retrieveReq) {
            var metricId = null;
            if (retrieveReq.readyState == 4 /* complete */) {
                var retrieved = JSON.parse(retrieveReq.responseText).d;
                if (retrieved.results.length > 0) {
    
                    /*if the data is returned include your logic here*/
                    /*e.g: var name = retrieved.results[0].Name;*/
                    //alert(retrieved.results[0].name;
                }
            }
        }
    
     </script>
    
    </head>
    <body> 
    <input type="Submit" value="Fetch" onClick="fnbuildODataCall()" style="width:100px;height:24px;">
    <!--<button onclick="fnbuildODataCall('/AccountSet?$select=Name')">Done</button>-->
    
    </body> 
    </html>

    The code fails at Xrm.Page.context.getOrgUniqueName() with "Line: 8 Error: 'Xrm' is undefined".

    Any ideas?

    Matt

    Monday, March 10, 2014 3:41 PM
  • Hi Matt,

    Instead of forming the serverUrl manually, why not use the Xrm.Page.context.getClientUrl() directly? That would give you the complete Url.


    Admin QuikView Solution for CRM 2013

    Monday, March 10, 2014 3:50 PM
  • It fails at the Xrm part saying "Xrm is undefined"

    Matt

    Monday, March 10, 2014 4:00 PM
  • Will have a look and see where it takes me.

    Matt

    Monday, March 10, 2014 4:01 PM
  • Hi Matt,

    Also reference this in your HTML page, which allows you to use the Xrm methods

    <script src="./ClientGlobalContext.js.aspx" type="text/javascript"></script>
    Also, if you're embedding the HTML page, you might need to use window.parent.Xrm.... and so on.


    Admin QuikView Solution for CRM 2013


    Monday, March 10, 2014 4:12 PM
  • Thanks for that, it sorted my problem out superbly!

    My next question would be if the html5 page opened up with all the account names in a combo box, how could I retrieve the related quotes for that account name, using the OData designer I can't seem to crack this?

    regards,

    Matt

    P.S. I would then want to populate a datatable with the quote details.

    Tuesday, March 11, 2014 3:18 PM
  • Hi Matt,

    The following oData query would fetch all Quotes for a selected Account. You would simply need the Account Id (CRM GUID)

    QuoteSet?$select=Name&$filter=AccountId/Id eq guid'YOUR_ACCOUNT_GUID'

    And once you have retrieved the Quotes, use the same technique to fetch the Quote details using the Quite Id (CRM GUID); maybe something like this

    QuoteSet?$select=Description,Name,QuoteNumber&$filter=QuoteId eq guid'YOUR_QUOTE_GUID'You could always add more attributes to fetch by providing them in the select section. I suggest you use the oData Query designer for forming these queries.


    Admin QuikView Solution for CRM 2013

    Tuesday, March 11, 2014 3:46 PM
  • Thanks for this, very helpful.

    So The first combobox displays the accounts, as one is selected a query is fired to return the account names guid, this guid is used to fire another query which returns the quotes available for that account and these populate another combobox.

    The next stage will be to select a quote query the data twice, once to retrieve the guid for the quote and the second one to return the quote details and populate a datagrid.  Looking at the populate a datagrid it seems the majority of examples use third party grids, my question is do you have an example of populating a datagrid, also why are people using third party grid as opposed to the standard HTML5 <datagrid>?

    regards,

    Matt

    Tuesday, March 18, 2014 10:07 AM
  • Hi Matt,

    I would suggest the following flow:

    1. Fetch all the Account Names and their Guids, and populate a combo box (say A) with <Display, Value> pair as <Account Name, Account Guid>.

    2. When someone selects an account from the combo box A, you have the Guid in the value, so use that to fire a query to fetch all the Quotes (Quote Names and their Guids) and populate a second combo box (say B) similarly like A with <Display, Value> pair as <Quote Name, Quote Guid>.

    3. Now, when someone selects a Quote from combo box B, you have the Quote Guid using which you can fire a query to fetch the Quote details.

    So, it's just 3 queries in total for the entire thing: fetch all accounts, fetch all quotes related to that account, fetch quote details.

    Also, regarding the datagrid part, I am not quite sure; but I would suggest you use the one you're comfortable with. People generally tend to use third party ones, since they have less coding to do to set it up, bind the data and show it. Also, third party datagrids come with a lot of out-of-the-box functionality which you would otherwise need to manually code when using HTML5 controls.


    Admin QuikView Solution for CRM 2013

    Wednesday, March 19, 2014 9:39 AM