rallycode-rally

How to create new TimeEntryValue in Rally


I'm fairly new to the Rally API and JS, and Stackoverflow for that matter. I have been using Stackoverflow to answer all of my questions so far, but I can't seem to find anything about adding new TimeEntryValues.

I am building an app that allows to add new TimeEntryValues. I can add or load TimeEntryItems but for TimeEntryValues, I ever only seem to post the Hours field when looking at the trace in the browser.

Here is a simplified code that exhibits the same problem.

    launch: function(){      
    //For this example, pre-define Time Entry Reference, Date, and Hour value
    var myTimeEntryItem = "/timeentryitem/1234";
    var myDateValue = "2016-05-20T00:00:00.000Z";
    var myHours = 2.5;

    //Check if Time Entry Value (TEV) already exists
    var TEVstore = Ext.create('Rally.data.WsapiDataStore', {
        model: 'TimeEntryValue',
        fetch: ['ObjectID','TimeEntryItem','Hours','DateVal'],
        filters: [{
            property: 'TimeEntryItem',
            operator: '=',
            value: myTimeEntryItem
        },
        {
            property: 'DateVal',
            operator: '=',
            value: myDateValue
        }],

        autoLoad: true,
        listeners: {
            load: function(TEVstore, tevrecords, success) {
                //No record found - TEV does not exist
                if (tevrecords.length === 0) {
                    console.log("Creating new TEV record");

                    Rally.data.ModelFactory.getModel({
                        type: 'TimeEntryValue',
                        success: function(tevModel) {
                            var newTEV = Ext.create(tevModel, {
                                DateVal: myDateValue,
                                Hours: myHours,
                                TimeEntryItem: myTimeEntryItem
                            });

                            newTEV.save({
                                callback: function(result, operation) {
                                    if(operation.wasSuccessful()) {
                                        console.log("Succesful Save");
                                        //Do something here
                                    }
                                }
                            });
                        }
                    });
                } else {
                    console.log("TEV Record exists.");
                    //Do something useful here
                }
            }
        },
        scope: this
    });                            
}

Any hints what I am doing wrong are greatly appreciated. Thanks


Solution

  • This is actually a longstanding defect in App SDK caused by a mismatch in the WSAPI attribute metadata and the client side models used for persisting data to the server.

    Basically what's happening is the DateVal and TimeEntryItem fields are marked required and readonly, which doesn't make sense. Really, they need to be writable on create and then readonly after that.

    So all you need to do in your app is before you try to save your new TimeEntryValue just mark the DateVal and TimeEntryItem fields as persistable and you should be good to go.

    //workaround
    tevModel.getField('DateVal').persist = true;
    tevModel.getField('TimeEntryItem').persist = true;
    
    //proceed as usual
    var newTEV = Ext.create(tevModel, {
        DateVal: myDateValue,
        Hours: myHours,
        TimeEntryItem: myTimeEntryItem
    });
    // ...