cancel
Showing results for 
Search instead for 
Did you mean: 

Can I use oData calls within a formatter?

former_member499177
Participant
0 Kudos

Hello experts,

I have implemented a function import which accepts a partner code from entity A and returns the partner description from entity B. Cannot use associations here, since code is not part of the entity A key and SEGW doesn't allow me to create one. Loading data from entity B at the beginning not an option either, contains more than 150.000 entries. Function import works just fine, my problem is that the formatter doesn't seem to wait for the data coming back from the oData call, even though I have attached both a promise and the "batchrequestcompleted" event. I watched all the instructions executed in the correct order inside the debugger (and returning the expected result) but seems like the view doesn't wait. Is there some kind of technique I can use to make theview wait for the result or do I have to attach extra fields in my entity A in order to store the description in the backend, something that for obvious reasons I don't want to do. (Really short) code attached below, any ideas?

Cheers,

Greg

PS: someone will easily notice when observing the code that the value is returned twice, once in the success function and once after the promise. It's simply for testing, after all, none of them works 😄

			textPartner : function (sValue) {
				if (!sValue) {
					return "";
				}
				var that = this.getParent().getParent().getParent().getParent().getParent().getParent().getParent().getParent().getController();
				var oModel = this.getModel();
				var oModelUpdateDeferred = jQuery.Deferred();


				oModel.attachEventOnce("batchRequestCompleted", function(oEvent) {
					if (that._checkIfBatchRequestSucceeded(oEvent)) {
 						oModelUpdateDeferred.resolve();	
					}
				});
				
				oModel.callFunction("/Get_Partner_Name", { urlParameters: { Partner_Code: sValue },
					success: function(oData, sResponse) {
						sValue = oData.ReturnValue;
						return sValue;
					},
					error: function(oError) {
						jQuery.sap.log.error("oData Failure", oError);
					}
				});	
				var readyToGo = function() {
					return sValue;
				};
				jQuery.when(oModelUpdateDeferred).done().then( jQuery.proxy(readyToGo, this) );	
			}

        /* 2nd simplified synchronous version, behaves the same way */

        textPartner : function (sValue) {
				if (!sValue) {
					return "";
				}
				var oModel = this.getModel();


				oModel.callFunction("/Get_Partner_Name", { urlParameters: { Partner_Code: sValue }, async: false,
					success: function(oData, sResponse) {
						sValue = oData.ReturnValue;
						//return sValue;
					},
					error: function(oError) {
						jQuery.sap.log.error("oData Failure", oError);
					}
				});
				return sValue;
			}
        

Accepted Solutions (1)

Accepted Solutions (1)

former_member499177
Participant
0 Kudos

Workaround is following Jun Wu approach (suggestion 1) with SEGW (ignore the key thing) and get the data using expand in the binding. Synchronous logic doesn't seem to work.

Answers (2)

Answers (2)

Joseph_BERTHE
Active Contributor
0 Kudos

Hi,

The association should works like jun.wu5 said. But when you do your association, you have to do it with a different logic.

Please look at this blog

https://blogs.sap.com/2017/12/21/list-report-adding-a-contact-quick-view-to-a-table-without-cds/

at the section

SAP Gateway part : Data Model it will show you how to create an association with that logic.

Kind Regards,

Joseph

former_member499177
Participant
0 Kudos

I already have the association part working, tested and working fine in SEGW, my problem now is with the binding, check my last answer in the thread, thanks for stepping in to help anyway 🙂

junwu
Active Contributor
0 Kudos

1. If I am not worng, association should work, key won't matter

2. make your odata call synchronus

former_member499177
Participant
0 Kudos

1. No it doesn't. I have an entity with 5 fields, 2 key fields, partner code not among them. During the association creation wizard, system allows me to use only the first two fields (keys). Tried to play smart and key in my Partner field but got the message that principal key must be a key field.

2. New to SAPUI and web development in general, had the wrong impression that synchronous execution is not allowed for V2 oData model. However, tried it and doesn't work either(!). Added the async:false keyword and removed all event and promise stuff (see 2nd version of formatter attached to the question). Debugged this thing and has the same behavior (!). Order of execution: 1. call Function, 2. return sValue (still holds the code), 3. success function...

Tried to surround false value in double quotes (shouldn't work but I'm out of options), result still the same. This is as simple as it can be (actually 3 lines of code!). Can it be that I have put my "async: false" directive in the wrong place?

junwu
Active Contributor
0 Kudos

1. you don't have to specify any, you can just delete it.

former_member499177
Participant
0 Kudos

1. Yeps, this approach looks like it's working, deleted the lines, implemented the additional logic in get_entity method and works when tested in GW_CLIENT. The problem now is that I still can't see the expanded entries in the view. I managed to add an expand in the bindRows method like this:

var refKey = "External_Organizations"; this.oExtOrgTable.bindRows({ path: refKey, parameters: { "$expand": "Agreement_Partners,Agreement_Vendors" }, templateShareable: false }

These are the field declarations (just two of them):

this.oExtOrgTable.addColumn(new sap.ui.table.Column({ label: new sap.m.Label({text: "Involvement ID"}), template: new sap.m.Text({text: "{Involveid}"}) }));

this.oExtOrgTable.addColumn(new sap.ui.table.Column({ label: new sap.m.Label({text: "Partner ID"}), template: new sap.m.Text({text: "{Agreement_Partners/NameOrg1}"}) }));

1st column visible, 2nd not. Debugged the model, found the entry and noticed that under it, there's a sub-entry with the expanded entity, starting with "__deferred" but without the values visible, see below:

__deferred:

uri:"<myserver>/sap/opu/odata/SAP/ZCONTRACTS_SRV/External_OrganizationsSet(AgrId='0000000003',AgrExtid='0000006666')/Agreement_Partners"

Any idea what this deferred means and why (unlike SEGW) can't see the entry content?

Thanks, this has been quite helpful already

    former_member499177
    Participant
    0 Kudos

    Ack! Found it, problem was... the $ in front of the "expand" parm, thank you 🙂