XrmServiceToolkit.Soap.Update ... don't update

Oct 21, 2014 at 10:17 AM
Hello,
I'm quite new on the field of XRM and Codeplex, I'm working with CRM 2013 on premise.
I'm looping on the following code which doesn't update a record. The idea is :
  • fetch the entity 'ntk_autonumber' and check there's only 1 record
  • read the record and the field containing an autonumber code
  • increment this number and update the autonumber record
    I get the answer "undefined" from XrmServiceToolkit.Soap.Update and the record is not updated. Where am I wrong ?
    Thank you in advance for your attention.
// check the unicity of the autonumber record(s)
var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>"
  + " <entity name='ntk_autonumber'>"
    + " <attribute name='ntk_autonumberid' />"
    + " <attribute name='ntk_autonumber_jobcode' />"
    + " <filter type='and'>"
        + " <condition attribute='statecode' operator='eq' value='0' />"
    + " </filter>"
  + " </entity>"
+ " </fetch>";
var fetchedRecords = XrmServiceToolkit.Soap.Fetch(fetchXml);
if (fetchedRecords.length != 1) {
    alert("NTK error: autonumber record not unique: " + fetchedRecords.length);
    return;
}

var autonumber = fetchedRecords[0];
var jobCode = autonumber.attributes["ntk_autonumber_jobcode"].value;
jobCode = jobCode + 1;
autonumber.attributes["ntk_autonumber_jobcode"] = jobCode;

// update the autonumber record
var updateResult = XrmServiceToolkit.Soap.Update(autonumber);
alert("updateResult: " + updateResult);
Coordinator
Oct 21, 2014 at 12:42 PM
Hi There.

To use the update method, you will need to initialize the business entity with like
var updateContact = new XrmServiceToolkit.Soap.BusinessEntity("ntk_autonumber", entityId);
You can find examples of usage of methods in the documentation section here.

Hope this helps.

Kind Regards,
Jaimie
Oct 21, 2014 at 12:59 PM
Hi Jaimie,
Thanks for your attention.
I already try that but without more success (same "undefined" result and record not updated)
function autonumberIncrement() {
    // check there's only 1 active autonumber record
    var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>"
      + " <entity name='ntk_autonumber'>"
        + " <attribute name='ntk_autonumberid' />"
        + " <attribute name='ntk_autonumber_jobcode' />"
        + " <filter type='and'>"
            + " <condition attribute='statecode' operator='eq' value='0' />"
        + " </filter>"
      + " </entity>"
    + " </fetch>";
    var fetchedRecords = XrmServiceToolkit.Soap.Fetch(fetchXml);
    if (fetchedRecords.length != 1) {
        alert("NTK error: autonumber record not unique: " + fetchedRecords.length);
        return;
    }
    alert("fetch length: " + fetchedRecords.length);

    var autonumber = new XrmServiceToolkit.Soap.BusinessEntity("ntk_autonumber", fetchedRecords[0].id);
    var jobCode = fetchedRecords[0].attributes["ntk_autonumber_jobcode"].value + 1;
    autonumber.attributes["ntk_autonumber_jobcode"] = jobCode;
    var updateResult = XrmServiceToolkit.Soap.Update(autonumber);

    return jobCode;
}
Coordinator
Oct 26, 2014 at 2:58 PM
Hi There.

A couple of things to check
  1. The returned value of the ntk_autonumber_jobcode is not an integer value, so you may need to convert it and plus 1 to the value
  2. The running user does not have the privilege to perform the logic.
Hope this helps.
Jaimie
Mar 4, 2015 at 3:22 AM
There appears to be a bug in the way XrmServiceToolkit recognises whether you have passed an int or a decimal. Specifically line 1654 which is the following

var oType = (parseInt(encodedValue) === encodedValue) ? "c:int" : "c:decimal";

The encodedValue is a string so parseInt never matches it.

Alternately try this

autonumber.attributes["ntk_autonumber_jobcode"] = ( value: jobCode, type: "int"};
Jan 13, 2016 at 9:25 PM
Thanks @bradsprigg. I ran into the same issue and spent about 2 hours debugging, pretty much down the same path you took, before I saw your solution. It was a big help.

Mitch