Skip to Content
avatar image
Former Member

Not able to pass service URL in the OModel object. Not getting the table data in the SAPUI5 app

Hello Experts,

Grettings! I am facing problem while creating the SAPUI5 application.

I created an oData service, it is working fine getting the metadata as well as the json format. But when I am trying to pass that service URL in the oModel object, I am not able to retrieve the table data in the UI app yet there is data in the table. I am not able to figure out why it is not fetching the table data.

The service i passed in the oModel is as follows :

https://i0xb0bcbe732.us1.hana.ondemand.com/Test1/odataservice.xsodata/

Please guide me through this query.

It would be really helpful if I can get a solution to this problem.

Thanks in advance.

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

3 Answers

  • Aug 17, 2017 at 03:08 AM

    how you bind the table to the model? how you try to get the data?

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Aug 18, 2017 at 11:04 AM

    When you call the odata service form UI, did you debug in which function the control is going to success or in error?

    When you tried to execute the url from sap gateway are you getting some results?

    Few suggestions:

    1. Install a postman app for chrome and give the url link and try to post or a get request. if it gives some results which means the back end part is working fine OR

    2. Go to gateway and execute the service and check if you are getting the results

    3. If above 1 or 2 is returning some results please check the binding part.

    Or please paste the code how you are posting it ?

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      Hey Rahul,

      Thanks for the response. I passed the url in the postman app but getting error. I have attached the screenshot of the postman as well as attaching my code. See if you can help me get through it

      .snap1.jpg

      Controller code
      
      
      onInit: function() {
      
      
      		var oModel =  sap.ui.model.odata.ODataModel("https://i0xb0bcbe732.us1.hana.ondemand.com/Test1/odataservice.xsodata/")
      		
      		sap.ui.getCore().setModel(oModel, "products");
      		console.log(oModel);
      	},
      
      
      mode:0,
      	resetForm: function(){
      		$('#name').val('');
      		$('#genre').val('');
      		$('#id').val('');
      	},
      	
      	create: function() {
      		this.mode = 'create';
      		this.resetForm();
      		
      		$('#formid').slideDown(300, function(){
      			
      			var id = sap.ui.getCore().byId('tableid')._getRowCount();
      			$('#id').val(id);
      			
      		})
      	},
      	
      	edit: function(){
      		this.mode = 'edit';
      		
      		var oTable = sap.ui.getCore().byId('tableid');
      		
      		var selected = oTable.getSelectedIndex();
      		
      		if(selected ==-1) {
      			alert("select a row");
      		}else{
      			
      			$('#formid').slideDown(function() {
      				
      				var data = oTable.getModel('products').oData['Products('+ selected +')'];
      				
      				var id = data.ID;
      				var genre= data.Genre;
      				var name = data.Name;
      				
      				$("#name").val(name);
      				$("#genre").val(genre);
      				$("#id").val(id);
      				
      			})
      			
      		}
      	},
      	
      	removeId: 0,
      	
      	remove: function(){
      		
      		this.mode = 'delete';
      		
      		var oTable = sap.ui.getCore().byId('tableid');
      		
      		var selected = oTable.getSelectedIndex();
      		
      		if(selected ==-1){
      			
      			alert("Select a row");
      		}else{
      				var data = oTable.getModel('products').oData['Products(' + selected + ')'];
      				this.removeId = data.ID;
      			 	this.save();
      		}
      	},
      	
      	save: function(){
      
      
      		var requestObj = {
      				
      				requestUri : '',
      				method: '',
      				headers : {
      					
      					"X-Requested-With" : "XMLHttpRequest",
      					"Content-Type": "application/json;odata=minimalmetadata",
      					"DataServiceVersion" : "3.0",
      					"MaxDataServiceVersion" : "3.0",
      					"Accept": "application/json;odata=minimalmetadata"
      				}
      		} ;
      		
      		
      		var newData = {
      				
      				"odata.type": "ODataDemo.Product", //from metadata info
      				"ID":$('#id').val(),
      				"Name":$('#name').val(),
      				"Genre":$('#genre').val(),
      		}
      		
      		if(this.mode == 'create'){
      			var url = "https://i0xb0bcbe732.us1.hana.ondemand.com/Test1/odataservice.xsodata/Test";
      			var method= "POST";
      			
      			requestObj.requestUri = url;
      			requestObj.method = method;
      			requestObj.data = newData;
      		}
      		
      		else if(this.mode == 'edit') {
      			
      			var id = $('#id').val();
      			var url = "https://i0xb0bcbe732.us1.hana.ondemand.com/Test1/odataservice.xsodata/Test("+id+")";
      			var method = "PUT";
      			
      			requestObj.requestUri = url;
      			requestObj.method = method;
      			requestObj.data = newData;
      		}
      		
      		else if(this.mode == 'delete'){
      			var id= this.removeId;
      			var url = "https://i0xb0bcbe732.us1.hana.ondemand.com/Test1/odataservice.xsodata/Test(" + id + ")";
      			var method= "DELETE";
      			
      			requestObj.requestUri = url;
      			requestObj.method = method;
      		}
      		
      		OData.request(requestObj, function(){
      			
      			sap.ui.getCore().getModel('products').refresh();
      			$('#formid').slideUp();
      		});
      	}
      });
      
      //This is view where i created the table and the bind
      
      
      var oTable = new sap.ui.table.Table("tableid", {
      visibleRowCount: 5, editable: false });
      oTable.addColumn(new sap.ui.table.Column({ label: new sap.ui.commons.Label({text: "ID"}),
      visible:true,
      template: new sap.ui.commons.TextView({text: "{products>ID}"}) }));
      oTable.addColumn(new sap.ui.table.Column({ label: new sap.ui.commons.Label({text: "Name"}),
      visible:true,
      template: new sap.ui.commons.TextView({text: "{products>NAME}"}) }));
      oTable.addColumn(new sap.ui.table.Column({ label: new sap.ui.commons.Label({text: "Genre"}),
      visible:true, template: new sap.ui.commons.TextView({text: "{products>GENRE}"}) }));
      oTable.bindRows("products>/TestApp")
      
      
      Thanks in advance!
      snap1.jpg (77.5 kB)
  • avatar image
    Former Member
    Aug 21, 2017 at 01:24 PM

    pls try this

    onAfterRendering: function(){ var that = this; this.oModel = new sap.ui.model.odata.ODataModel("url" );

    this.oModel.read("/EntitySet", null, null, false, function(oData, oResponse)

    { var jModel = new sap.ui.model.json.JSONModel(oData);

    that.getView().setModel(jModel, "sapremote"); });

    Add comment
    10|10000 characters needed characters exceeded