Quantcast
Channel: Microsoft Dynamics CRM Forum - Recent Threads
Viewing all articles
Browse latest Browse all 79901

Increment JavaScript not working CRM 2016

$
0
0
Hello,

We had a javascript that auto numbers. It first did not run at all it failed with serverurl() not found. I found out they changed it to clienturl() I changed everywhere in code and it successfully ran. Now the issue is it is not incrementing. It is running successfully but gives every record the same number. I am wondering if the autoNumberSetting.new_CurrentIndex++; is depreciated. below is the code.

Thanks

Chris

// JavaScript source code
///<summary>
/// Returns the URL for the SOAP endpoint using the context information available in the form
/// or HTML Web resource.
///</summary
function _getClientUrl() {
    var clientUrl = "";
    if (typeof GetGlobalContext == "function") {
        var context = GetGlobalContext();
        clientUrl = context.getClientUrl();
    }
    else {
        if (typeof Xrm.Page.context == "object") {
            clientUrl = Xrm.Page.context.getClientUrl();
        }
        else { throw new Error("Unable to access the server URL"); }
    }
    if (clientUrl.match(/\/$/)) {
        clientUrl = clientUrl.substring(0, clientUrl.length - 1);
    }
    return clientUrl;
}
///<summary>
/// Returns the URL for the SOAP endpoint using the context information available in the form
/// or HTML Web resource.
///</summary>
function _orgServicePath() {
    return this._getClientUrl() + "/XRMServices/2011/Organization.svc/web";
}
///<summary>
/// Returns the URL for the REST endpont using the context information available in the form
/// or HTML Web resource. (The OData or Organization Data path.)
///</summary>
function _orgDataPath() {
    return this._getClientUrl() + "/XRMServices/2011/OrganizationData.svc/";
}

///<summary>
/// Entry point.
///</summary>
function GetAutoNumber() {
    var targetGuid = "1a9fe8ac-e922-e211-947a-00155d127c02";  // the guid of the Accessories auto number entity
    var targetName = "new_autonumbersetting";  // the name of the entity     

    // get the current record for targetName entity
    var autoNumberSetting = retrieveRecord(targetGuid, targetName);

    // set retrieved next index number to form field
    Xrm.Page.getAttribute("new_accessorynumber").setValue(autoNumberSetting.new_CurrentIndex.toString());

    // increment
    autoNumberSetting.new_CurrentIndex++;
    var createdOn = autoNumberSetting.CreatedOn.toString();
    var modifiedOn = new Date().toString();
    autoNumberSetting.CreatedOn = { Value: createdOn };
    autoNumberSetting.ModifiedOn = { Value: modifiedOn };

    // update
    updateRecord(targetGuid, autoNumberSetting, targetName);
}

///<summary>
/// Sends a synchronous request to retrieve a record.
///</summary>
function retrieveRecord(id, type) {
    ///<param name="id" type="String">
    /// A String representing the GUID value for the record to retrieve.
    ///</param>
    ///<param name="type" type="String">
    /// The Schema Name of the Entity type record to retrieve.
    /// For an Account record, use "Account"
    ///</param>   

    var systemQueryOptions = "";

    var req = new XMLHttpRequest();
    req.open("GET", encodeURI(this._orgDataPath() + type + "Set(guid'" + id + "')" + systemQueryOptions), false);  // synchronous call
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");

    req.send(null);

    return JSON.parse(req.responseText).d;
}

///<summary>
/// Sends a synchronous request to update a record.
///</summary>
function updateRecord(id, object, type) {
    ///<param name="id" type="String">
    /// A String representing the GUID value for the record to retrieve.
    ///</param>
    ///<param name="object" type="Object">
    /// A JavaScript object with properties corresponding to the Schema Names for
    /// entity attributes that are valid for update operations. 
    ///</param>
    ///<param name="type" type="String">
    /// The Schema Name of the Entity type record to retrieve.
    /// For an Account record, use "Account"
    ///</param>   

    var req = new XMLHttpRequest();
    req.open("POST", encodeURI(this._orgDataPath() + type + "Set(guid'" + id + "')"), false);  // synchronous call
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("X-HTTP-Method", "MERGE");

    var jsonEntity = window.JSON.stringify(object);

    req.send(jsonEntity);

}


 


Viewing all articles
Browse latest Browse all 79901

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>