locked
Using javascript to create and update record in crm 2011 RRS feed

  • Question

  • Hi all,

    I have a requirement where I need to create a record from an entity i.e. from entity A i need to create a record in entity B and if record exists I have to perform update operation in entity B how do I achieve this?

    Thanks in advance

    Thursday, December 13, 2012 6:07 PM

Answers

  • Hi Akiraa,

    Your requirement can be done using javascript and SOAP have a look at the below code to create and update a record.

    // ********************************
    // CREATE NEW ENTITY RECORD
    // ********************************
    // Call Crm Service
    function CallCrmService(soapBody, method) {
        try {
            var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
            xmlHttpRequest.Open("POST", '/mscrmservices/2007/CrmService.asmx', false); //synchronous
            xmlHttpRequest.setRequestHeader("SOAPAction", 'http://schemas.microsoft.com/crm/2007/WebServices/' + method); //Fetch,Execute,Create
            xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
            var xml = "<?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>" + soapBody + "</soap:Body></soap:Envelope>";
            xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
            xmlHttpRequest.send(xml);
    
            var resultXml = xmlHttpRequest.responseXML;
            var errorCount = resultXml.selectNodes('//error').length;
    
            if (errorCount != 0) {
                var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
                alert(msg);
    
                return null;
            }
    
            return resultXml;
        }
        catch (err) {
        }
    
        return null;
    }
    
    // Create Record
    function CreateRecord(entityName, fields) {
        try {
            var resultArray = new Array();
            var attributesList = '';
    
            for (i = 0; i < fields.length; i++) {
                if (fields[i].Value != null)
                    attributesList += "<" + fields[i].SchemaName + ">" + fields[i].Value + "</" + fields[i].SchemaName + ">";
            }
    
            var xml = "<Create xmlns='http://schemas.microsoft.com/crm/2007/WebServices'><entity xsi:type='" + entityName + "'>" + attributesList + "</entity></Create>";
            var resultXml = CallCrmService(xml, 'Create');
    
            if (resultXml) {
                var newid = resultXml.selectSingleNode('//CreateResult').nodeTypedValue;
                return newid;
            }
        }
        catch (err) {
        }
    
        return null;
    }
    
    // Update Record
    function UpdateRecord(entityName, entityId, fields) {
        try {
            if (entityId != null) {
                var resultArray = new Array();
                var attributesList = '<' + entityName + 'id>' + entityId + '</' + entityName + 'id>';
    
                for (i = 0; i < fields.length; i++) {
                    if (fields[i].Value != null)
                        attributesList += "<" + fields[i].SchemaName + ">" + fields[i].Value + "</" + fields[i].SchemaName + ">";
                }
    
                var xml = "<Update xmlns='http://schemas.microsoft.com/crm/2007/WebServices'><entity xsi:type='" + entityName + "'>" + attributesList + "</entity></Update>";
                var resultXml = CallCrmService(xml, 'Update');
    
                return true;
            }
        }
        catch (err) {
        }
    
        return false;
    }

    let me know if you need further assistance on this.

    Regards

    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.


    Sushant Sarkar Microsoft CRM Dynamics Engineer

    • Proposed as answer by Sushant Sarkar Thursday, December 13, 2012 6:13 PM
    • Marked as answer by Akiraa_Dynamics Thursday, December 13, 2012 6:49 PM
    Thursday, December 13, 2012 6:13 PM

All replies

  • Hi Akiraa,

    Your requirement can be done using javascript and SOAP have a look at the below code to create and update a record.

    // ********************************
    // CREATE NEW ENTITY RECORD
    // ********************************
    // Call Crm Service
    function CallCrmService(soapBody, method) {
        try {
            var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
            xmlHttpRequest.Open("POST", '/mscrmservices/2007/CrmService.asmx', false); //synchronous
            xmlHttpRequest.setRequestHeader("SOAPAction", 'http://schemas.microsoft.com/crm/2007/WebServices/' + method); //Fetch,Execute,Create
            xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
            var xml = "<?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>" + soapBody + "</soap:Body></soap:Envelope>";
            xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
            xmlHttpRequest.send(xml);
    
            var resultXml = xmlHttpRequest.responseXML;
            var errorCount = resultXml.selectNodes('//error').length;
    
            if (errorCount != 0) {
                var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
                alert(msg);
    
                return null;
            }
    
            return resultXml;
        }
        catch (err) {
        }
    
        return null;
    }
    
    // Create Record
    function CreateRecord(entityName, fields) {
        try {
            var resultArray = new Array();
            var attributesList = '';
    
            for (i = 0; i < fields.length; i++) {
                if (fields[i].Value != null)
                    attributesList += "<" + fields[i].SchemaName + ">" + fields[i].Value + "</" + fields[i].SchemaName + ">";
            }
    
            var xml = "<Create xmlns='http://schemas.microsoft.com/crm/2007/WebServices'><entity xsi:type='" + entityName + "'>" + attributesList + "</entity></Create>";
            var resultXml = CallCrmService(xml, 'Create');
    
            if (resultXml) {
                var newid = resultXml.selectSingleNode('//CreateResult').nodeTypedValue;
                return newid;
            }
        }
        catch (err) {
        }
    
        return null;
    }
    
    // Update Record
    function UpdateRecord(entityName, entityId, fields) {
        try {
            if (entityId != null) {
                var resultArray = new Array();
                var attributesList = '<' + entityName + 'id>' + entityId + '</' + entityName + 'id>';
    
                for (i = 0; i < fields.length; i++) {
                    if (fields[i].Value != null)
                        attributesList += "<" + fields[i].SchemaName + ">" + fields[i].Value + "</" + fields[i].SchemaName + ">";
                }
    
                var xml = "<Update xmlns='http://schemas.microsoft.com/crm/2007/WebServices'><entity xsi:type='" + entityName + "'>" + attributesList + "</entity></Update>";
                var resultXml = CallCrmService(xml, 'Update');
    
                return true;
            }
        }
        catch (err) {
        }
    
        return false;
    }

    let me know if you need further assistance on this.

    Regards

    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.


    Sushant Sarkar Microsoft CRM Dynamics Engineer

    • Proposed as answer by Sushant Sarkar Thursday, December 13, 2012 6:13 PM
    • Marked as answer by Akiraa_Dynamics Thursday, December 13, 2012 6:49 PM
    Thursday, December 13, 2012 6:13 PM
  • Hi Sushant,

    Thanks for the quick responce will try this out and let you know.

    Thursday, December 13, 2012 6:15 PM
  • to create and update a record you can use both rest and soap endpoints in crm

    check both the services


    ms crm

    Friday, December 14, 2012 8:59 AM
  • var value = "Return";
    var entityName = 'contact';
    var entityId = Xrm.Page.data.entity.getId();
    var fields = [new CRMField('cust_approvalflowcheck', value)];

    you can call below methods as per you requirement.

    CreateRecord(entityName, fields);

    UpdateRecord(entityName, entityId, fields);
    window.location.reload(true);

    // Make Struct
    function MakeStruct(names) {
        var names = names.split(' ');
        var count = names.length;

        function constructor() {
            for (var i = 0; i < count; i++) {
                this[names[i]] = arguments[i];
            }
        }
        return constructor;
    }

    // CRM Field Struct
    var CRMField = MakeStruct("SchemaName Value");

    // Call Crm Service
    function CallCrmService(soapBody, method) {
        try {
            var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
            xmlHttpRequest.Open("POST", '/mscrmservices/2007/CrmService.asmx', false); //synchronous
            xmlHttpRequest.setRequestHeader("SOAPAction", 'http://schemas.microsoft.com/crm/2007/WebServices/' + method); //Fetch,Execute,Create
            xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
            var xml = "<?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>" + soapBody + "</soap:Body></soap:Envelope>";
            xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
            xmlHttpRequest.send(xml);

            var resultXml = xmlHttpRequest.responseXML;
            var errorCount = resultXml.selectNodes('//error').length;

            if (errorCount != 0) {
                var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
                alert(msg);

                return null;
            }

            return resultXml;
        }
        catch (err) {
        }

        return null;
    }

    // Update Record
    function UpdateRecord(entityName, entityId, fields) {
        try {
            if (entityId != null) {
                var resultArray = new Array();
                var attributesList = '<' + entityName + 'id>' + entityId + '</' + entityName + 'id>';

                for (i = 0; i < fields.length; i++) {
                    if (fields[i].Value != null)
                        attributesList += "<" + fields[i].SchemaName + ">" + fields[i].Value + "</" + fields[i].SchemaName + ">";
                }

                var xml = "<Update xmlns='http://schemas.microsoft.com/crm/2007/WebServices'><entity xsi:type='" + entityName + "'>" + attributesList + "</entity></Update>";
                var resultXml = CallCrmService(xml, 'Update');

                return true;
            }
        }
        catch (err) {
        }

        return false;
    }

    // Create Record
    function CreateRecord(entityName, fields) {
        try {
            var resultArray = new Array();
            var attributesList = '';

            for (i = 0; i < fields.length; i++) {
                if (fields[i].Value != null)
                    attributesList += "<" + fields[i].SchemaName + ">" + fields[i].Value + "</" + fields[i].SchemaName + ">";
            }

            var xml = "<Create xmlns='http://schemas.microsoft.com/crm/2007/WebServices'><entity xsi:type='" + entityName + "'>" + attributesList + "</entity></Create>";
            var resultXml = CallCrmService(xml, 'Create');

            if (resultXml) {
                var newid = resultXml.selectSingleNode('//CreateResult').nodeTypedValue;
                return newid;
            }
        }
        catch (err) {
        }

        return null;
    }

    Make sure to "Mark As Answer" and "Vote as Helpful", if you get answer of your question.

    Regards,
    Priyank Jain

    Wednesday, July 17, 2013 11:32 AM