Weird problem with populating field using a variable (Code inside)

Nov 7, 2013 at 12:21 PM
Edited Nov 7, 2013 at 12:33 PM
In CRM2011, I was able to dynamically update a field using the code below in this case it is -nameOfField.
var  nameOfField= "new_Status";

    XrmSvcToolkit.updateRecord({
        //Entity we are updating
        entityName: entityType,
        //Entity ID, taken from above
        id: caseID,
        //Attribute, but Schema name not name of entity
        entity: {
            nameOfField: false
        },
        async: false,
        successCallback: function(result) {
           alert("Update successful");
        },
        errorCallback: function(error) {
            alert("ERROR: There was an error when updating the contact record");
        }
Since updating to CRM2013 the code above doesn't work. It's weird because I have one instance of CRM2013 that the code works on, but doesn't work in the others. I can't figure this out. Any advice?
Nov 7, 2013 at 3:05 PM
I don't think you'll be able to do this dynamically as you want with this plugin. I've tried something similar but had to revert to using Jamie's XrmServiceToolkit. I don't think this library is as adaptable as the XrmServiceToolkit. Take a look and give it a try: https://xrmservicetoolkit.codeplex.com/
Nov 7, 2013 at 5:12 PM
Thanks for your feedback Michael. I've looked the the XrmServiceToolkit, seems good but I'll wait for either Daniel or someone else to reply. I really want to stick with this library.

I just want to iterate that all I want is to take the variable and inject it into the update record:
__var  nameOfField= "new_Status";__
to:
 entity: { 
                 nameOfField: false
 },
Coordinator
Nov 7, 2013 at 10:30 PM
Hi Philip,

that should work. Have you tried capturing more details about the error or running Fiddler and checking what's being sent down the wire?

Thanks
George
Nov 8, 2013 at 11:01 AM
Yeah I can confirm it doesn't work. There is no error in this case, it skips to the errorCallback function and produces the error alert.
Nov 8, 2013 at 5:53 PM
Georged, what happens is that I don't get an error in the code, but as Michael said you can't assign as a variable, for some reason it's not registering. What happens is it skips and calls the error function (function(error)) instead. Do you have any suggestions?
Coordinator
Nov 8, 2013 at 5:59 PM
Hi Philip,

what I would do is to run fiddler to capture the trace to see what's being sent to the server. Failing to find out anything, I'd simply step through the code to figure out what's happening. The fact that it does work in 2011 but not in 2013 is very strange as endpoints didn't change at all.

I'm planning to spend some time looking at 2013 compatibility over the weekend (in case you don't get to troubleshooting in the next couple days.)
Nov 9, 2013 at 10:51 AM
Georged, I ran the code in fiddler and VS/IE debugger and I'm beginning to think it may have been a mistake on my part - I just loaded an instance of CRM2011 and found that I got the same error in 2011 after clearing cache. I presumed the update to 2013 broke the code, but I think the file was cached so it was working in 2011.

So as a result, I don't think it's possible without modifying XRM library, which I certainly don't want to do. So this works:
var switchC = "false";

  entity: { 
                 nameOfField: switchC
 },
but this does not:
var nameOfField = "new_Status";

 entity: { 
                 nameOfField: false
 },
I'll wait on your conclusion Georged, see what you come up with. Thanks for your help.
Coordinator
Nov 9, 2013 at 10:55 AM
That's interesting. I'll have a look. Can you tell me what type is new_Status field?
Nov 9, 2013 at 10:58 AM
It's a two option (bit-field), but I have tried with String and Whole numbers and get the same result. A colleague is testing other data types as I type this right now.
Coordinator
Nov 9, 2013 at 2:47 PM
Sorry guys for the late response, I am on my way to move to a different city, it has been a lot of work in the past week, I didn't have much time to get online.

OK, now back to the topic. I don't think this is a problem of the library, rather it is kind of JavaScript behavior.

To get around this issue, you might use a slightly different approach, say the following code:
var  nameOfField= "new_Status";

var request = {
    //Entity we are updating
    entityName: entityType,
    //Entity ID, taken from above
    id: caseID,
    //Attribute, but Schema name not name of entity
    entity: {},
    async: false,
    successCallback: function(result) {
       alert("Update successful");
    },
    errorCallback: function(error) {
        alert("ERROR: There was an error when updating the contact record");
    }
};

request.entity[nameOfField] = false;

XrmSvcToolkit.updateRecord(request);
I didn't run a test, but I suppose it should work, please let me know if otherwise.

Thanks,
Daniel Cai
Nov 9, 2013 at 6:35 PM
I'm just letting you know that the above works perfectly. I've been using this library but it just got a whole lot better - now it's easy to write reusable scripts. One question though; I tried to initialise with the same method above on the following retrieveMultiple code:
XrmSvcToolkit.retrieveMultiple({
        entityName: Contacts,
        async: false,
        odataQuery: "?$filter=new_RND1 eq " + rrnd1 + " and new_RND2 eq " + rrnd2 + "",
        successCallback: function(result) {

            var contact = callBackData.OpportunityId;

        },
        errorCallback: function(error) {
            //some code
        }
How would I go about using a variable for OpportunityId (var contact = callBackData.OpportunityId;) using a field:
var orange = Xrm.Page.getAttribute("new_orangefield").getValue();

var contact = callBackData.orange;