cancel
Showing results for 
Search instead for 
Did you mean: 

serviceLayer: returnvalue from object query

AdKerremans
Active Contributor
0 Kudos

Hi,

I use the following code to retrieve a userfield in the service layer.

var queryOption = "$select=TableName,FieldID&$filter=TableName eq '" + fieldData[i].TableName + "' and Name eq '" + fieldData[i].Name +
			"'";
console.log('queryOption: ' + queryOption);

var rs = slContext.UserFieldsMD.query(queryOption);
console.log('rs: ' + JSON.stringify(rs));

The syntax of the query options is good, but I don't know the format of rs.

JSON.stringify(rs) gives {}

How can I get the returned fields of how do I know nothing is returned.

Thanks

Ad

Accepted Solutions (1)

Accepted Solutions (1)

Trinidad
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi Ad,

Have you tried the toArray() method as given in the SL User Manual?

function GET() {
 var queryOption = "$select=CardName, CardCode & $filter=contains(CardCode, 'c1') & $top=5 & $orderby=CardCode";
 var slContext = new ServiceLayerContext();
 var retCaseSensitive = slContext.BusinessPartners.query(queryOption);
 var retCaseInsensitive = slContext.query("BusinessPartners", queryOption, true);
 http.response.setStatus(http.HttpStatus.HTTP_OK);
 http.response.setContent({ "CaseSensitive": retCaseSensitive.toArray(), "CaseInsensitive": retCaseInsensitive.toArray() });
 http.response.send();

I have tried in my environment and made it work with toArray():

    var queryOption = "$select=TableName,FieldID,Name&$filter=TableName eq '@UDO_DOC' and Name eq 'CardCode'"; var slContext = new ServiceLayerContext();
    var dataSrvRes = slContext.UserFieldsMD.query(queryOption);
    http.response.send(http.HttpStatus.HTTP_NOT_FOUND, dataSrvRes.toArray()); 

Hope it helps,
Trinidad.

Answers (2)

Answers (2)

AdKerremans
Active Contributor
0 Kudos

Hi Trinidad,

What I am trying to do is on the server side to check if a userfield exist and according to the answer add or update a userfield

My query on postman works and gives a correct answer (see below).

https://xxxxxx:50000/b1s/v1/UserFieldsMD?$select=TableName,FieldID&$filter=TableName eq 'OHEM' and Name eq 'se_userscan'
{
    "odata.metadata": "https://devhanadb01.serac.nl:50000/b1s/v1/$metadata#UserFieldsMD",
    "value": [
        {
            "TableName": "OHEM",
            "FieldID": 2
        }
    ]
}

Regards

Ad

Trinidad
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi Ad,

I'm not sure to understand what you are trying to do here.

You want to check if a specific field already exists in a table?

Does the query work on Postman? Can you please try it?

There is a sample in the SL User Guide:

Retrieving UDFs 
To retrieve a UDF, send an HTTP request as the example below: 
GET /UserFieldsMD(TableName='OCRD', FieldID=0) For more information, see Retrieving Entities. Querying UDFs
Standard OData query options are also supported for UDFs.
For example, you've forgotten the table name and the UDF ID but still remember the UDF name; you can query the UDF as follows: GET /UserFieldsMD?$filter=Name eq 'u1' The service returns: { "value": [ { "Name": "u1", "TableName": "ACRD", "FieldID": 0, ... ... }, { "Name": "u1", "TableName": "OCRD", "FieldID": 0, ... } ] }

Thanks,
Trinidad.