How to capture update query responce

Oct 7, 2013 at 5:51 AM
Hi,
I have question regarding update query, if I fire crmrestkit update how can I get response back whether it is failed or succeeded?
Because for me next entity update should only happen if first is succeeded.

Thanks.
Rashmi Mane
Coordinator
Oct 7, 2013 at 6:28 AM
Edited Oct 7, 2013 at 6:39 AM
Hi,

all you have to do is using the "then" method of the promise object:
///
/// Is invoked for the success-event of the CrmRestKit update operation
///
function onRestKitSuccess( data, status, xhr ) {
    
    // ...

}

///
/// This function is invoked for the error event
///
function onRestKitError( xhr, status, errorThrown ) {

    // ...
}

// performs an update an registers an success- and error handler
CrmRestKit.Update('Account', '<%id-value%>', { Name: 'Foobar'} )
    .then(onRestKitSuccess)
    .fail(onRestKitError);

// same result, differnt syntax
// performs an update an registers an success- and error handler
CrmRestKit.Update('Account', '<%id-value%>', {Name: 'Foobar'})
    .then(onRestKitSuccess, onRestKitError);
Take a look at the documentation of jQuery-deferrred implementation. .

In case you are not so familiar with the concept of promise take a look at this articel.

Regards,

Daniel
Oct 7, 2013 at 6:47 AM
Thanks for quick response, will try.
Oct 7, 2013 at 11:49 AM
I tried this but I cannot capture failure, if the update fails it still goes in then. Any idea thanks.
Coordinator
Oct 8, 2013 at 7:55 AM
Edited Oct 8, 2013 at 7:57 AM
Hi Rashmi,

could you please post your code and I need some more details on the error:
  • Is the error a "CRM" errror (e.g.: an plugin through an error, or a wrong value for a option-set)?
  • Could you please post the error message
Do I undersand this right that the then-function is invoked even through an error is generated.... what are the value of the arguments (data, status, xhr)?


Daniel
Oct 9, 2013 at 3:57 AM
Yes you are right that the then-function is invoked even through an error is generated. This is html web resource no plug in.
Coordinator
Oct 9, 2013 at 6:19 AM
I understood that this is JavaScript not a Plugin, my sample was describing a situation where you perform an update via the CrmRestKit and a plugin is registered for the update-event and this plugin throughs an error.

Could you please post here the error message and the JavaScript source-code? Without this details it is not possible to solve your problem.

Daniel
Oct 9, 2013 at 6:37 AM
Edited Oct 9, 2013 at 6:37 AM
Yes it is java script, but the problem is if in any case if i pass null value or wrong entity name then it is throwing error for bad request. I would like to capture that as if something is wrong on first entity update then i want to stop the next entity update.

Is their any way to capture failures on bad query request and stop updating next entity.
Coordinator
Oct 9, 2013 at 7:11 AM
Edited Oct 9, 2013 at 7:39 AM
Rejected (e.g. "Bad Request") should trigger the fail-function, as fare as I can tell. I just created a new unit-test and everything works as expected:
QUnit.test( 'Bad-Request will trigger the fail method -sync-', function () {
   // arrange - the web-service will through an error because the id is not valid
   var notExistingId = '0815';
            
   // action - try to update an not existing attribute (webserivce should through an error)
   CrmRestKit.Update( 'Account', notExistingId, { NotExistingAttribute: 'Test' }, false )
       .then( function ( data ) {
          // assert 
          QUnit.ok( false, 'The then-handler should not be called' );
       } )
       .fail( function () {
          // assert
          QUnit.ok( true, 'Fail-hanlder is invoked as expected ' );
       } );
} );

QUnit.asyncTest( 'Bad-Request will trigger the fail method -async-', function () {

   // arrange - the web-service will through an error because the id is not valid
   var notExistingId = '0815';

   // action - try to update an not existing attribute (webserivce should through an error)
   CrmRestKit.Update( 'Account', notExistingId, { NotExistingAttribute: 'Test' }, true )
      .then( function ( data ) {
         // assert 
         QUnit.ok( false, 'The then-handler should not be called' );
         // continue with the other tests
         QUnit.start();
     } )
     .fail( function () {
        // assert
        QUnit.ok( true, 'Fail-hanlder is invoked as expected ' );
        // continue with the other tests
        QUnit.start();
     } );
} );
To isolate the problem could you please check which version of the CrmRestKit you are using? And which jQuery version.


And please post the JavaScript source-code.


Daniel
Oct 9, 2013 at 8:54 AM
Hi Daniel,

Here are version details i think that also can makes difference

CRMrestkit : CRM 5.0 CrmRestKit Version 0.0.1
JQuery: jquery.1.4.2.js

Thanks.

Rashmi
Coordinator
Oct 9, 2013 at 9:05 AM
Edited Oct 9, 2013 at 9:08 AM
Hello Rashmi,

ok this will be the reason, current version of the CrmRestKit is 2.6.1 and jQuery is right now on version 2.x

The CrmRestKit version 0.0.1 (+3 years old) was never intended to be used in production, in that version the library was still under development!

Why do you use this old version, when did you download that version? Get the current version for CrmRestKit and jQuery and try it again!

Please take into account that the version 2.x of jQuery does not support IE 6,7,8! So jQuery version 1.10.2 might be better.

Daniel
Marked as answer by thuld on 10/28/2013 at 4:26 AM
Coordinator
Oct 11, 2013 at 12:17 PM
Hi Rashmi,

any news on this topic? Is it now running with the latest versions?

Daniel