Missing Soap method: initializeFrom

Jul 3, 2015 at 10:40 AM
Below the code I use for a initializeFrom request to for example create a quote based on a opportunity.
Maybe it is useful to add it to the toolkit.
    var initializeFrom = function (entityName, id, targetEntityName, callback) {
        ///<summary>
        /// Sends synchronous/asynchronous request to setState of a record.
        ///</summary>
        ///<param name="entityName" type="String">
        /// A JavaScript String corresponding to the Schema name of
        /// entity that is used for initializeFrom operations.
        /// </param>
        ///<param name="id" type="String">
        /// A JavaScript String corresponding to the GUID of
        /// entity that is used for initializeFrom operations.
        /// </param>
        ///<param name="targetEntityName" type="String">
        /// A JavaScript String corresponding to the value of
        /// target entity that is used for initializeFrom operations.
        /// </param>
        ///<param name="callback" type="Function">
        /// A Function used for asynchronous request. If not defined, it sends a synchronous request.
        /// </param>

        var request = [
            "<request i:type='b:InitializeFromRequest' xmlns:a='http://schemas.microsoft.com/xrm/2011/Contracts' xmlns:b='http://schemas.microsoft.com/crm/2011/Contracts'>",
                "<a:Parameters xmlns:c='http://schemas.datacontract.org/2004/07/System.Collections.Generic'>",
                    "<a:KeyValuePairOfstringanyType>",
                        "<c:key>EntityMoniker</c:key>",
                        "<c:value i:type='a:EntityReference'>",
                          "<a:Id>", id, "</a:Id>",
                          "<a:LogicalName>", entityName, "</a:LogicalName>",
                          "<a:Name i:nil='true' />",
                        "</c:value>",
                      "</a:KeyValuePairOfstringanyType>",
                      "<a:KeyValuePairOfstringanyType>",
                        "<c:key>TargetEntityName</c:key>",
                        "<c:value i:type='a:string' xmlns:a='http://www.w3.org/2001/XMLSchema'>", targetEntityName, "</c:value>",
                      "</a:KeyValuePairOfstringanyType>",
                      "<a:KeyValuePairOfstringanyType>",
                        "<c:key>TargetFieldType</c:key>",
                        "<c:value i:type='b:TargetFieldType'>All</c:value>",
                      "</a:KeyValuePairOfstringanyType>",
                "</a:Parameters>",
                "<a:RequestId i:nil='true' />",
                "<a:RequestName>InitializeFrom</a:RequestName>",
            "</request>"
        ].join("");

        var async = !!callback;

        return doRequest(request, "Execute", async, function (resultXml) {
            var response;
            if (jQuery(resultXml).find('c\\:value').length != 0) {
                response = jQuery(resultXml).find('c\\:value').eq(0)[0];
            } else {
                response = jQuery(resultXml).find('value').eq(0)[0]; 
            }

            var entity = new businessEntity();
            entity.deserialize(response);

            if (!async)
                return entity;
            else
                callback(entity);
            // ReSharper disable NotAllPathsReturnValue
        });
        // ReSharper restore NotAllPathsReturnValue
    };