Getting error ""resp" is not defined." while running code in background script

VIKAS MISHRA
Tera Contributor

I Am trying to run below mentioned code in background script but in line no. 3 its showing me error "resp" is not defined.", please suggest how to resolve this.

 

responseBody = [{"isSuccess":false,"errorMessage":"Agreement: 00000288.0 is not present in Conga","apttus_contract_id":"00000288.0"},{"isSuccess":true,"errorMessage":"","apttus_contract_id":"00006475.0"}];
for(var i in responseBody){
        var flag = resp[i].isSuccess;
        if(!flag){
            resp[i].apttus_contract_id;
            gs.info('testing response For True/False');
        }
    }

 

find_real_file.png

6 REPLIES 6

Michael Jones -
Giga Sage

Try this: 

responseBody = [{"isSuccess":false,"errorMessage":"Agreement: 00000288.0 is not present in Conga","apttus_contract_id":"00000288.0"},{"isSuccess":true,"errorMessage":"","apttus_contract_id":"00006475.0"}];
for(var i in responseBody){
     var resp= JSON.parse(i);
        var flag = resp[i].isSuccess;
        if(!flag){
           
            resp[i].apttus_contract_id;
            gs.info('testing response For True/False');
        }
    }

I hope this helps!

If this was helpful, or correct, please be kind and mark the answer appropriately.

Michael Jones - Proud member of the GlideFast Consulting Team!

I hope this helps!
Michael D. Jones
Proud member of the GlideFast Consulting Team!

Basically you need to turn the JSON payload (each item of your array) into an object to be able to reference it's properties, etc. 

I hope this helps!

If this was helpful, or correct, please be kind and mark the answer appropriately.

Michael Jones - Proud member of the GlideFast Consulting Team!

I hope this helps!
Michael D. Jones
Proud member of the GlideFast Consulting Team!

I added line "var resp= JSON.parse(i);" before line no. 3 and this time i got below error in line no. 4.

 

Evaluator: com.glide.script.RhinoEcmaError: Cannot read property "isSuccess" from undefined
   script : Line(4) column(0)
      1: responseBody = [{"isSuccess":false,"errorMessage":"Agreement: 00000288.0 is not present in Conga","apttus_contract_id":"00000288.0"},{"isSuccess":true,"errorMessage":"","apttus_contract_id":"00006475.0"}];
      2: for(var i in responseBody){
      3: var resp= JSON.parse(i);
==>   4: 		var flag = resp[i].isSuccess;
      5: 		if(!flag){
      6: 			resp[i].apttus_contract_id;
      7: 			gs.info('testing response For True/False');

 

 

 

Interesting - it worked for me, but maybe try this (copy the whole script, please) 

responseBody = [{"isSuccess":false,"errorMessage":"Agreement: 00000288.0 is not present in Conga","apttus_contract_id":"00000288.0"},{"isSuccess":true,"errorMessage":"","apttus_contract_id":"00006475.0"}];
for(var i in responseBody){
     var resp= JSON.parse(i);
        var flag = resp.isSuccess;
        if(!flag){
           
            resp.apttus_contract_id;
            gs.info('testing response For True/False');
        }
    }

find_real_file.png

I hope this helps!

If this was helpful, or correct, please be kind and mark the answer appropriately.

Michael Jones - Proud member of the GlideFast Consulting Team!

I hope this helps!
Michael D. Jones
Proud member of the GlideFast Consulting Team!