cancel
Showing results for 
Search instead for 
Did you mean: 

Q: Getting a value from a very simple web-service

mario_maisto
Participant
0 Kudos

Hey guys,


i am having a slight problem with I hope a very easy question for you. I am trying to read a single value (Formid) from a web-service that has only the read (GetEntity) function to a value in javascript.

The service has  a dummy input and an output that is a Globally Unique Identifier (Formid). The service works properly :

But I cant get the value to my javascript variable. Am I trying to get it wrong? The code is as follows :


onInit: function() {

        var sServiceUrl = "http://my.uri.domain:port/sap/opu/odata/sap/GENERATE_GUID_SRV_01/";

        var oModel = new sap.ui.model.odata.ODataModel(sServiceUrl, true);

        var oJsonModel = new sap.ui.model.json.JSONModel();

        oModel.read("/guidSet('X')?", null, null, true, function(oData, response) {

            oJsonModel.setData(oData);

          

        });

      

        sap.ui.getCore().setModel(oJsonModel);

        var formId = sap.ui.getCore().getModel().getProperty("{/Formid}");

        alert(formId); //SOMEWHY IS NULL

    },

I hope You can help me,

Mario.

Accepted Solutions (1)

Accepted Solutions (1)

former_member182372
Active Contributor
0 Kudos
  1. oModel.read("/guidSet('X')?", {
  2. success : function(oData, response) { 
  3. var oJsonModel = new sap.ui.model.json.JSONModel(); 
  4. oJsonModel.setData(oData.results); 
  5.              
  6. sap.ui.getCore().setModel(oJsonModel); 
  7. var formId = sap.ui.getCore().getModel().getProperty("/Formid"); 
  8. alert(formId); //SOMEWHY IS NULL 
  9.         }

  10. }); 
mario_maisto
Participant
0 Kudos

Hey Maksim,
thank You for the quick response !
I tried your suggested code, but it did not work. The alert now displays 'undefined'.
Any suggestions?

former_member182372
Active Contributor
0 Kudos

No ? mark at tthe end

  1. oModel.read("/guidSet('X')", {
former_member182372
Active Contributor
0 Kudos

actually what is the purpose of that 'X'??

can't you just do /guidSet ?




  1. oModel.read("/guidSet", {
  2. success : function(oData, response) {
  3. var oJsonModel = new sap.ui.model.json.JSONModel();
  4. oJsonModel.setData( { Formid : oData.results[0].Formid } );
  5.             
  6. sap.ui.getCore().setModel(oJsonModel);
  7. var formId = oData.resultssap.ui.getCore().getModel().getProperty("/Formid");
  8. alert(formId); //SOMEWHY IS NULL
  9.         }
  10. });
mario_maisto
Participant
0 Kudos

Changed it to :


oModel.read("/guidSet('X')", {

            success: function(oData, response) {

                var oJsonModel = new sap.ui.model.json.JSONModel();

                oJsonModel.setData(oData.results);

                sap.ui.getCore().setModel(oJsonModel);

                var formId = sap.ui.getCore().getModel().getProperty("/Formid");

                alert(formId);

            }

        });


It still says undefined.

mario_maisto
Participant
0 Kudos
As I don't have an entitySet implemented (only Get Entity - read), the simple /guidSet does not work. The value X is a dummy input (I wanted the service to send  only the Formid, but the read needed an input also).

It is quite confusing to me, because when I go to my.uri.domain:port/sap/opu/odata/sap/GENERATE_GUID_SRV_01/guidSet('X') the Formid value is right there, .. just how to get it.
former_member182372
Active Contributor
0 Kudos

Try

  1. oModel.read("/guidSet(Dummy='X')", { 
Private_Member_15166
Active Contributor
0 Kudos

Hi Mario,

Do like this and see if it's working.

                    var oModel = "http://my.uri.domain:port/sap/opu/odata/sap/GENERATE_GUID_SRV_01/"

              var sRead = "/guidSet(DUMMYVARIABLENAME='X')"  ;

              oModel.read( sRead, null, null, true, function(oData, oResponse){

              var formId = oData.formId;

              },function(){

            alert("Read failed");});


or you may also use proxy in your URL.


var oModel = "proxy/http/my.uri.domain:port/sap/opu/odata/sap/GENERATE_GUID_SRV_01/"


See here if you are getting your formId.


Also check if you are getting your data in console.

console.log(oModel);


Regards

Dhananjay

mario_maisto
Participant
0 Kudos

Hey Maksim,

Got it now !!

I changed the oData.results (as I don't have sets) to just oData and got the value!

oModel.read("/guidSet('X')", {

            success: function(oData, response) {

                var oJsonModel = new sap.ui.model.json.JSONModel();

                oJsonModel.setData(oData);

                sap.ui.getCore().setModel(oJsonModel);

                var formId = sap.ui.getCore().getModel().getProperty("/Formid");

                alert(formId);

            }

        });

Thanks a lot for your help!

Answers (0)