Getting [object Object] undeefined error in Log statement

peri sravani1
Tera Expert

Hi All, Getting below error while printing log statements.

perisravani1_0-1699951451978.png

Below is the code I've written:

let response = r.execute();
        let responseBody = response.getBody();
        let httpStatus = response.getStatusCode();
        let parsedData = JSON.parse(responseBody);

        let opportunities = parsedData.opportunitiesData;
        gs.info("test line 40 "+opportunities);
        totalOpportunities = [...opportunities];
    }

        gs.info("test line 44 "+totalOpportunities);
2 ACCEPTED SOLUTIONS

Prince Arora
Tera Sage
Tera Sage

@peri sravani1 

 

As per the screenshot, this is not an error, you have parsed the JSON thats why you get the data as JSON object

 

Please do it as follows:

 
var opportunities = parsedData.opportunitiesData;
opportunities = JSON.stringify(opportunities);
gs.info(opportunities);
 
If you want to access some object from the JSON tree, you need to parse it and access the data
If you want to check the data in it, you can use JSON.stringify() and it will work for you
 

If my answer solved your issue, please mark my answer as  Correct & 👍Helpful based on the Impact.

View solution in original post

Danish Bhairag2
Tera Sage
Tera Sage

Hi @peri sravani1 ,

 

The Output is in JSON format in order to view it you can stringify it as mentioned by Prince above.

You can write this line instead 

 

gs.info("test line 40 "+JSON.stringify(opportunities));

 

Thanks,

Danish

 

 

View solution in original post

9 REPLIES 9

Hi @peri sravani1 ,

 

I presume u have written a scripted Rest API for this. You can just do a GlideRecord query to insert the data & while doing a GlideRecord query u can verify if the data exists if no then initialize if yes then update/ignore based on your requirement.

 

Thanks,

Danish

 

I'm updating the records but they are not getting updated.
oppo.update();
This will work in action part script thing right Danish!?

Hi @peri sravani1 ,

 

Possible to share ur script.will be bit easier to find the issue if any.

 

Thanks,

Danish

 

(function execute(inputs, outputs) {
// ... code ...

try {

var r = new sn_ws.RESTMessageV2('x_xxxx_xxxx_xxxxx.SAM.gov Data Import', 'Get Opportunities Public API');
r.setStringParameterNoEscape('samgov_get_postedfrom', gs.getProperty('x_xxxx_xxxx_xxxxx.samgov_get_postedfrom'));
r.setStringParameterNoEscape('samgov_get_postedto', gs.getProperty('x_xxxx_xxxx_xxxxx.samgov_get_postedto'));
r.setStringParameterNoEscape('offset', 0);

gs.info("test line no.11");

var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();

var parsedResponse = JSON.parse(responseBody);
gs.info("test line no.18");
var totalSize = parsedResponse.totalRecords;
gs.info("test line no.20");

gs.info("######test line 22 "+totalSize);

var baseOffset = 100;
var totalOpportunities = [];

for(var j=0;j < Math.ceil(totalSize/baseOffset); j++ ){

let r = new sn_ws.RESTMessageV2('x_xxxx_xxxx_xxxxx.SAM.gov Data Import', 'Get Opportunities Public API');
r.setStringParameterNoEscape('samgov_get_postedfrom', gs.getProperty('x_xxxx_xxxx_xxxxx.samgov_get_postedfrom'));
r.setStringParameterNoEscape('samgov_get_postedto', gs.getProperty('x_xxxx_xxxx_xxxxx.samgov_get_postedto'));
r.setStringParameterNoEscape('offset', (baseOffset*j));

let response = r.execute();
let responseBody = response.getBody();
let httpStatus = response.getStatusCode();
let parsedData = JSON.parse(responseBody);

let opportunities = parsedData.opportunitiesData;
var a = JSON.stringify(opportunities.length);
gs.info("test line 40 "+a);
totalOpportunities = [...totalOpportunities,...opportunities];
}

gs.info("#######test line 44 "+totalOpportunities.length);

var specialNotices = 0;
var updateCount = 0;
var insertCount = 0;

// start bulk insert
for(var i=0;i<totalOpportunities.length;i++) {

var oppo = new GlideRecord("x_xxxx_xxxx_xxxxx_opportunity");
oppo.setValue("noticeid", totalOpportunities[i].noticeId);
oppo.setValue("title", totalOpportunities[i].title);
oppo.setValue("solicitation_number", totalOpportunities[i].solicitationNumber);
oppo.setValue("posted_date", totalOpportunities[i].postedDate);
oppo.setValue("type", totalOpportunities[i].type);
oppo.setValue("base_type", totalOpportunities[i].baseType);
oppo.setValue("archive_type", totalOpportunities[i].archiveType);
oppo.setValue("archive_date", totalOpportunities[i].archiveDate);
oppo.setValue("response_deadline", totalOpportunities[i].responseDeadLine);
oppo.setValue("u_naics_code", totalOpportunities[i].naicsCode);
oppo.setValue("classification_code", totalOpportunities[i].classificationCode);
oppo.setValue("active", totalOpportunities[i].active);
oppo.setValue("organization_type", totalOpportunities[i].organizationType);

//oppo.setValue("u_naics_code", parsedData.opportunitiesData[i].placeOfPerformance);
//set any other values
if(totalOpportunities[i].solicitationNumber==''){
specialNotices = specialNotices + 1;
oppo.insert();
}
else{
var oppoList = new GlideRecord('x_xxxx_xxxx_xxxxx_opportunity');
oppoList.addQuery('solicitation_number', totalOpportunities[i].solicitationNumber);
oppoList.query();
if(!oppoList.hasNext()) {
oppo.insert();
insertCount = insertCount + 1;
}
else{
if(totalOpportunities[i].solicitationNumber == '36C25924Q0044'){
gs.info('Hii123'+totalOpportunities[i].title);
oppo.setValue("title", totalOpportunities[i].title);
oppo.update();
var b = oppo.getValue("title");
gs.info('Hii123'+b);
}
oppo.update();
updateCount = updateCount + 1;
}
}

}

gs.info("####insertss"+insertCount+"$$$$$updatess"+updateCount+"%%%%%specialNoticess"+specialNotices);

}
catch(ex) {
var message = ex.message;
}
})(inputs, outputs);

This is the code I've used Danish...

Hi @Danish Bhairag2 
This is the code I'm using.