Could not update record through Rest API explorer .

1_DipikaD
Kilo Sage

Hi All,

I am getting 422 service error while using PUT method on Rest API explorer  for incident table.

I have the below  script in Scripted REST Resource table for the API definition. What should I make changes to get 200 success response to update the record.

 

 

if( source == 'phone'){
        if( (gr.category == 'hardware' || gr.category == 'software') && (gr.subcategory == 'Imposter' || gr.subcategory == ' Attachment')) {
        msg = 'Able to update Incident. Error: This SIR is restricted to be updated via API.';
        errDet = 'Can update the  incident which source is phone: ' + number;
        serviceError = RESTUtils.buildErrorObject(422, msg, errDet);
        response.setError(serviceError);
        msg = 'Record updated successfully';
        response.setBody(responseBody);
        return;
        }
    }
 
Please let me know if you need more information.
 
Thank You
17 REPLIES 17

@Ankur Bawiskar  But still it is throwing error as shown in the picture.

 

 

 

@1_DipikaD 

Did you debug which IF part is being run when you consume the endpoint?

what does your sample JSON request look like?

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

@Ankur Bawiskar  

One thing Ankur I want to know that will it update the existing value of a field of  a existing record ?

In my case it is inserting new value for a field of a existing record but it is updating the previous value for the same record

@1_DipikaD 

sorry but you didn't share the complete details i.e. how the API request looks like etc

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

@Ankur Bawiskar 

Yes I debuged that the if part of the following part script is running when we consume the endpoint.

/Can only update Domain Impersonation SIR
            var source = gr.contact_type;
            var scope = gr.u_scope;
            var ci = gr.getDisplayValue('cmdb_ci');
            if( source != 'SPAM Submission' && source != 'Application monitoring' &&ci.indexOf('Elastic')<0&&scope =='Yes'){
                if((gr.subcategory != 'Domain Impersonation' ) && (gr.category != 'Malicious Code' && gr.subcategory != 'Malicious Attachment') && (gr.category != 'Unauthorized Access')) {
                    msg = 'Unable to update Security Incident. Error: This SIR is restricted to be updated via API.';
                    errDet = 'Cannot update the security incident which is not Domain Impersonation: ' + number;
                    serviceError = RESTUtils.buildErrorObject(422, msg, errDet);
                    response.setError(serviceError);
               
                    return;
                }
            }