Cannot find function getStatusCode in object [object RESTMessageV2].

gauravbhandari
Giga Contributor

Hi All,

I have situation where i need to make two outbound call. The second call is based on the first call result. Now when trigger second call i got below error:

Cannot find function getStatusCode in object [object RESTMessageV2].

I couldn't find anything related to this. Does anyone has any idea about it.

@Ankur Bawiskar @Pradeep Sharma any idea?

Thanks in Advance.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@gauravbhandari 

Few points:

1) you should be using the getStatusCode() method on the response you received.

2) It seems you are applying it on object of RESTMessageV2

3) Also are you doing this in scoped app then it would throw this error

I just tried this and it throwed me error when I used that on object of RESTMessageV2

https://developer.servicenow.com/dev.do#!/reference/api/orlando/server/sn_ws-namespace/c_RESTMessage...

Sample script below:


    try {
        var request = new sn_ws.RESTMessageV2();
        request.setHttpMethod('post');
        request.setEndpoint('<web service endpoint URL>');
        request.setRequestBodyFromAttachment('<attachment sys_id>');
        
        var response = request.execute();

        // this would work fine as you are using response
        var httpResponseStatus = response.getStatusCode();
    
        // this will throw error as you are using it on request object
        var httpResponseStatus = request.getStatusCode();
        gs.info("http response status_code: " + httpResponseStatus);        
    }
    catch (ex) {
        var message = ex.getMessage();
        gs.info(message);
    }

Output: When I tried to use RESTMessageV2 in scoped app from background and used request object to get the status code; I also received the same error

So you should use that on response variable.

find_real_file.png

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@gauravbhandari 

Few points:

1) you should be using the getStatusCode() method on the response you received.

2) It seems you are applying it on object of RESTMessageV2

3) Also are you doing this in scoped app then it would throw this error

I just tried this and it throwed me error when I used that on object of RESTMessageV2

https://developer.servicenow.com/dev.do#!/reference/api/orlando/server/sn_ws-namespace/c_RESTMessage...

Sample script below:


    try {
        var request = new sn_ws.RESTMessageV2();
        request.setHttpMethod('post');
        request.setEndpoint('<web service endpoint URL>');
        request.setRequestBodyFromAttachment('<attachment sys_id>');
        
        var response = request.execute();

        // this would work fine as you are using response
        var httpResponseStatus = response.getStatusCode();
    
        // this will throw error as you are using it on request object
        var httpResponseStatus = request.getStatusCode();
        gs.info("http response status_code: " + httpResponseStatus);        
    }
    catch (ex) {
        var message = ex.getMessage();
        gs.info(message);
    }

Output: When I tried to use RESTMessageV2 in scoped app from background and used request object to get the status code; I also received the same error

So you should use that on response variable.

find_real_file.png

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Ankur,

Thank you for the quick response. I overlooked that part. It works now.

Regards

Gaurav