Forum Discussion

mpw83's avatar
mpw83
Contributor
6 years ago

Get ID dynamically and do a property transfer ?

I have a REST request which returns IDs. It's cannot be guaranteed about the number of records. Sometimes the number of ID's is 2 and sometimes more than 2 (e.g. 4). I need to get these ID's and do a property transfer into Test Case Properties.
Response JSON is similar to 

{
   "totalSize" : 1,
   "done" : true,
   "records" : [
      {
         "attributes" : {
            "type" : "Account__c",
            "url" : "/services/data/v39.0/sobjects/Account__c/a4X0p0000001LWLEA2"
         },
         "Id" : "a4X0p0000001LWLEA2",
         "peer__Schedule__r" : {
            "totalSize" : 4,
            "done" : true,
            "records" : [
               {
                  "Id" : "a680p0000000QHxAAM"
               },
               {
                  "Id" : "a680p0000000QHyAAM"
               },
               {
                  "Id" : "a680p0000000QHzAAM"
               },
               {
                 "Id" : "a680p0000000QI0AAM"
               }
            ]
         }
      }
   ]
}

How I can get these ID's dynamically? Help much appreciated. Thanks   

  • Hi mpw83 ,

     

    In that case you can use groovy script and use jsonSlurper, use the below groovy code.

    import groovy.json.JsonSlurper
    def ResponseMessage = testRunner.testCase.getTestStepByName("Name of your Test Step").getPropertyValue("response");
    def jsonSlurper = new JsonSlurper().parseText(ResponseMessage)
    	
    mainRecord =  jsonSlurper.records
    for(i=0;i<mainRecord.size();i++){
    	recordsID = mainRecord[i].peer__Schedule__r.records;
    	for(j=0;j<recordsID.size();j++){
    		log.info recordsID.Id[j]
    	}
    }

    Hope it will help you i n resolving your problem.

     

     

3 Replies

  • Hi mpw83 ,

     

    In that case you can use groovy script and use jsonSlurper, use the below groovy code.

    import groovy.json.JsonSlurper
    def ResponseMessage = testRunner.testCase.getTestStepByName("Name of your Test Step").getPropertyValue("response");
    def jsonSlurper = new JsonSlurper().parseText(ResponseMessage)
    	
    mainRecord =  jsonSlurper.records
    for(i=0;i<mainRecord.size();i++){
    	recordsID = mainRecord[i].peer__Schedule__r.records;
    	for(j=0;j<recordsID.size();j++){
    		log.info recordsID.Id[j]
    	}
    }

    Hope it will help you i n resolving your problem.