Wednesday, July 27, 2011

Retrieve single record from an entity using JavaScript in MS CRM 4.0

Here is a JavaScript method which can be used to retrieve a record from an entity using CRM Web Service. This method can be used to retrieve data from any CRM entity. It requires following parameters to be passed:
1.       entityName: Then schema name of an entity to be retrieved.
2.       entityId: record id form an entity.
3.       attributes: Array of attributes to be retrieved.


function RetrieveEntity(entityName, entityId, attributes)
{
    var result = null;
   
    if((entityName != null) && (entityName.length > 0)
    && (entityId != null) && (entityId.length > 0)
    && (attributes != null) && (attributes.length > 0))
    {
         try
         {
            var ColumnsSetAttributes = '';
            // Build columns to be retrieved
            for(var i = 0; i < attributes.length; i++)
            {
                ColumnsSetAttributes += " <q1:Attribute>" + attributes[i] + "</q1:Attribute>";
            }
           
            var XmlHeader = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
            "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
            GenerateAuthenticationHeader() +
            " <soap:Body>";   
            var RetrieveXml = XmlHeader +
            " <Execute xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>" +
            " <Request xsi:type='RetrieveRequest' ReturnDynamicEntities='true'>" +
            " <Target xsi:type='TargetRetrieveDynamic'>" +
            " <EntityName>" + entityName + "</EntityName>" +
            " <EntityId>" + entityId + "</EntityId>" +
            " </Target>" +
            " <ColumnSet xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' xsi:type='q1:ColumnSet'>" +
            " <q1:Attributes>" +
            ColumnsSetAttributes +
            " </q1:Attributes>" +
            " </ColumnSet>" +
            " </Request>" +
            " </Execute>" +
            " </soap:Body>" +
            " </soap:Envelope>" +
            "";

            var RetrieveRequest = new ActiveXObject("Msxml2.XMLHTTP");
            RetrieveRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
            RetrieveRequest.setRequestHeader("SOAPAction"," http://schemas.microsoft.com/crm/2007/WebServices/Execute");
            RetrieveRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
            RetrieveRequest.setRequestHeader("Content-Length", RetrieveXml.length);
            RetrieveRequest.send(RetrieveXml);          
           
            var RetrieveResponseXml = RetrieveRequest.responseXML;
            //Check for errors.
            if((RetrieveResponseXml == null) || (typeof(RetrieveResponseXml) == "undefined"))
            {                  
                alert("Error: No response received.");
            }
            else
            {
                var errorContactIdCount = RetrieveResponseXml.selectNodes('//error').length;
                if(errorContactIdCount != 0)
                {
                    var msg = oXml.selectSingleNode('//description').nodeTypedValue;              
                    alert(msg);
                }
                else
                {
                    //select the node text
                    var QueryBusinessResponseXml = RetrieveResponseXml.selectNodes("//BusinessEntity");
                    result = QueryBusinessResponseXml;
                }
            }
        }
        catch(e)
        {
            alert('Error occurred: ' + e.description);
        }
    }   
    return result;
   
}

Example: Retrieve name from a contact record:

//Example: To retrieve name from a contact
var EntityName = "contact";
var EntityId =  crmForm.ObjectId;//contactid
var AttributeList = new Array();
AttributeList[0] =  "name";

var responseXml = RetrieveEntity(EntityName,EntityId,AttributeList);

if(responseXml != null)
{
    //Get Investor Activity Id from response xml.
    var ContactNameNode = responseXml[0].selectSingleNode("//Property[@Name='name']");
    var CotactName = '';
    if(ContactNameNode != null && ContactNameNode.childNodes[0] != null)
    {
        CotactName = ContactNameNode.childNodes[0].text;
    }
}

No comments:

Post a Comment