Before you read this blog post, please check out Using GraphQL in ServiceNow in order to understand using introspection and GraphQL Explorer in servicenow.
In this post, we'll explore the GlideRecord_Mutation API in ServiceNow by dissecting a GraphQL mutation, its variables, and the resulting data. We'll also provide an example of how to perform this mutation via a REST API call from a Service Portal widget.
GraphQL mutations are operations that allow you to create, update, or delete data on the server. In ServiceNow, the GlideRecord_Mutation API provides a way to perform these operations on records in the database. Being an out-of-the-box (OOTB) feature, it allows developers to update records without writing any server-side code. This significantly simplifies the development process, enabling faster and more efficient application development within the platform.
Keep in mind that all ServiceNow GraphQL OOTB API adhere to ACL. Therefore, if you don't get the expected result, my suggestion would be to verify your ACLs.
Updating Mutation Syntax
mutation ($sys_id: String!, $short_description: String) {
GlideRecord_Mutation {
update_sn_hr_core_case(sys_id: $sys_id, short_description: $short_description) {
_row_data {
canWrite
uniqueValue
displayValue
__typename
}
short_description {
value
}
}
}
}
Breakdown:
The variables are the dynamic inputs to the mutation:
{
"sys_id": "45ae83320b30220097432da0d5673a76",
"short_description": "New Value Here..."
}
Explanation:
After executing the mutation, you receive the following response:
{
"data": {
"GlideRecord_Mutation": {
"update_sn_hr_core_case": {
"_row_data": {
"canWrite": true,
"uniqueValue": "45ae83320b30220097432da0d5673a76",
"displayValue": "HRC0000282",
"__typename": "GlideRecord_RowDataType"
},
"short_description": {
"value": "New Value Here..."
}
}
}
}
}
Breakdown:
To execute this mutation from a ServiceNow Service Portal widget, you can use AngularJS's $http service to make a REST API call.
Example Code
function($scope, $http) {
// Define the GraphQL mutation as a string
var mutationQuery = `
mutation ($sys_id: String!, $short_description: String) {
GlideRecord_Mutation {
update_sn_hr_core_case(sys_id: $sys_id, short_description: $short_description) {
_row_data {
canWrite
uniqueValue
displayValue
__typename
}
short_description {
value
}
}
}
}
`;
// Define the variables
var variables = {
sys_id: '45ae83320b30220097432da0d5673a76',
short_description: 'New Value Here...'
};
// Configure the request
var request = {
method: 'POST',
url: '/api/now/graphql',
headers: {
'Content-Type': 'application/json'
},
data: {
query: mutationQuery,
variables: variables
}
};
// Execute the HTTP request
$http(request).then(function(response) {
// Success callback
var result = response.data;
console.log('Mutation successful:', result);
// Update the scope or perform additional actions
$scope.updatedRecord = result.data.GlideRecord_Mutation.update_sn_hr_core_case;
}, function(error) {
// Error callback
console.error('Mutation failed:', error);
});
}
Explanation:
The GlideRecord_Mutation API in ServiceNow provides a powerful way to interact with records using GraphQL. By understanding the structure of mutations, variables, and responses, you can efficiently update records and integrate these operations into Service Portal widgets or other custom interfaces.