-1

I am using Karate for my test scenarios for the requirement to update the contract plan by passing the plan Id's and I am retrieving the planId from the database and passing it to my request json.

But the problem I am facing it comes with the column name whereas I only need the value.

Below is the example of how I am fetching my Id by connecting to the database

`@Scenario1 Scenario:

use jdbc to validate

  • def config = { username: 'mmultapp', password: 'Mmu1t@pp', url: 'jdbc:db2://edb2dev3.momentum.co.za:60022/MMULTTST', driverClassName: 'com.ibm.db2.jcc.DB2Driver' }
  • def DbUtils = Java.type('wellness_core_utils.DbUtils')
  • def db = new DbUtils(config)
  • def productId = db.readRows('select PRODUCT_ID from MULTUSR1.PLANS order by PLAN_ID desc fetch first 1 rows only')
  • print productId`

And I am getting the results in this way

{ "PRODUCT_ID": 68 }

I only need to read the value of 68 so I that I can pass it to my json request

Thanks in advance

Koos
  • 39
  • 1
  • 8
  • downvoting because no matter how many times I try to make this clear - this is not a Karate related question: https://stackoverflow.com/a/52078427/143475 – Peter Thomas Aug 19 '19 at 13:46

1 Answers1

2

The query returns a key-value pair which you write in the query, Suppose you write

select PRODUCT_ID AS pId from MULTUSR1.PLANS order by PLAN_ID desc fetch first 1 rows only;

So It returns a PRODUCT_ID with the name pId. Later on, you can use that result as per your requirements

Let's try this in your instance.

def productId = db.readRows('select PRODUCT_ID from MULTUSR1.PLANS order by PLAN_ID desc fetch first 1 rows only').PRODUCT_ID;
const result =  productId.PRODUCT_ID;

Notice: Please check the first type of your query result Like It's in [] or {} In above instance I consider it in {}

const result = [{ PRODUCT_ID: 87 }];
console.log(result[0].PRODUCT_ID);
Neel Rathod
  • 2,013
  • 12
  • 28