locked
CRM 2011: CRUD Operations RRS feed

  • Question

  • Hi all,

    Below is a code sample that I have create it to retrieve data from Account entity using REST endpoint and jQuery, but I need some help in order to refine it:

     

    function init()
    {
    	// write required ODATA query
     var guid = Xrm.Page.data.entity.getId();
     var name = Xrm.Page.getAttribute("name").getValue();
     var ipaddress = location.host;
     var odataSelect = "http://" + ipaddress + "/intercom/XRMServices/2011/OrganizationData.svc/AccountSet(guid'" + guid + "')";
     alert(odataSelect);
    
    	$.ajax( {
    		type: "GET",
    		contentType: "application/json; charset=utf-8",
    		datatype: "json",
    		url: odataSelect,
    		beforeSend: function ( XMLHttpRequest ) { XMLHttpRequest.setRequestHeader( "Accept", "application/json" ); },
    		success: function ( data, textStatus, XmlHttpRequest )
    		{
    
    
    			// Use this method for a selection that return single entitie
    			RetrieveEntityData( data.d);
    
    		},
    		error: function ( XmlHttpRequest, textStatus, errorThrown ) { alert( 'OData Select Failed: ' + odataSelect ); }
    	} );
    }
    
    function RetrieveEntityData( Entity )
    {
     // get the fields from the Entity object
     var AccountName = Entity.Name;
     var ApplyTestOption = Entity.new_ApplyTestOptionSet;
     var NumberOfEmployees = Entity.NumberOfEmployees;
     var Revenue = Entity.Revenue;
     var DateField = Entity.new_DateField;
    	var ParentAccount = Entity.ParentAccountId;
    	var Status = Entity.StateCode;
    	var StatusReason = Entity.StatusCode;
    
    	alert("Account Name" + AccountName);
    	alert("Apply Test Option Set" + ApplyTestOption);
    	alert("Number Of Employees " + NumberOfEmployees);
    	alert("Revenue " + Revenue);
    	alert("Date Field" + DateField);
    	alert("Parent Account " + ParentAccount);
    	alert("Status " + Status);
    	alert("Status Reason " + StatusReason);
    }
    

     

    1 - Some data types are not retrieved correctly:Option Set,Currency,Date field,Lookup,Status and Status Reason

    2 - Is there any way to retrieve data using anyother field instead of record id, i.e. retrieve multiple or something like this, if yes please help me retrieving values into variables using loops or anyother way

    3 - What are the required modifications to update create and delete ??

     

    Any help regarding the above issues will be appreciated thanks in advance..

    as a side note: I have created this sample cause It could a little bit easier to understand than the SDK sample, if coudl cause any issues in the future, please mention it here... thanks in advance
    Monday, August 15, 2011 11:30 AM

Answers

  • 1. For complex datatupes you need to look for Value property
       like Entity.new_ApplyTestOptionSet.Value.ToString()

    2. yes you can retrieve data using filters for e.g.

      AccountSet?$filter=Address1_City eq 'Redmond' 

      check the other filter condition at  http://technet.microsoft.com/en-us/library/gg309461.aspx


    3 To process multple results change the follwoing line


    // Use this method for a selection that return single entitie
       RetrieveEntityData( data.d);


    with something like
    var accounts = data.d;
       for (var i in accounts) {
       showMessage(accounts[i].Name);
       }

     There are a lot of samples available in sdk to create delete etc.

    SDK\sdk\samplecode\js

    I hope this helps


    Amreek singh Senior CRM Consultant CDC Praxa Sydney,Australia http://mscrmshop.blogspot.com/
    Monday, August 15, 2011 1:27 PM