The CreatorCon Call for Content is officially open! Get started here.

Google Gemini Integration - sys_update_set table

Community Alums
Not applicable

Hello all, 

This is more of a personal project... I'm playing with Google Gemini, it works well from a Q&A perspective e.g. "Describe an egg". However I wanted to give it something more challenging and cant get it to work

Below is a UI Action I've whipped together to look at the sys_update_xml table in order to summarise an update set. but whenever I run it the API is called and it just falls over, any ideas would be greatly appreciated.

(function executeAction() {
    action.setRedirectURL(current);

    var payloads = [];
    var childsets = new GlideRecord("sys_update_xml");
    childsets.addQuery("update_set", current.sys_id);
    childsets.orderBy("sys_updated_on");
    childsets.query();

    while (childsets.next()) {
        if (!childsets.payload.nil()) { 
            payloads.push(childsets.payload.toString());
        }
    }

   
    if (payloads.length === 0) {
        gs.addInfoMessage("No XML changes found to process.");
        return;
    }

    try {
        // Construct the API request body
        var requestBody = JSON.stringify({
            contents: [{
                parts: [{
                    text: "Please summarize this XML Data" + payloads.join("\n\n") // Format payloads as a readable block
                }]
            }]
        });

        
        var request = new sn_ws.RESTMessageV2('GoogleGemini', 'POST'); // Use the existing API definition
        request.setRequestBody(requestBody);

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

    
        gs.info("GoogleGemini API Response: " + responseBody);

        if (httpStatus == 200) {
            var responseJson = JSON.parse(responseBody);

        
            if (responseJson && responseJson.responseBody) {
                current.description = responseJson.responseBody;
                current.update();
                gs.addInfoMessage("Technical description generated successfully.");
            } else {
                gs.addErrorMessage("GoogleGemini API did not return a valid description.");
            }
        } else {
            gs.addErrorMessage("GoogleGemini API failed with HTTP status: " + httpStatus);
        }
    } catch (ex) {
        gs.error("Error calling GoogleGemini API: " + ex.message);
        gs.addErrorMessage("An error occurred while calling GoogleGemini API.");
    }
})();


 

1 ACCEPTED SOLUTION

Community Alums
Not applicable

@Ankur Bawiskar I managed to fix it. Here's the script I used.

action.setRedirectURL(current);

var ask = "summarise";
var payloads = []; 


var xmlgr = new GlideRecord("sys_update_xml");
xmlgr.addQuery("update_set", current.sys_id);
xmlgr.query();
while (xmlgr.next()) {
    if (!xmlgr.payload.nil()) { 
        payloads.push(xmlgr.payload.toString());
    }
}

// Combine all payloads into a single string
var payloadText = payloads.join("\n\n");  // Separate payloads for readability

try {
    var body = JSON.stringify({
        "contents": [{
            "parts": [{
                "text": ask + " " + payloadText + " " + current.description.toString()
            }]
        }]
    });

    var r = new sn_ws.RESTMessageV2('GoogleGemini', 'POST');
    r.setRequestBody(body);
    
    var response = r.execute();
    var responseBody = response.getBody();
    var res = JSON.parse(responseBody);
    
    current.description = res.candidates[0].content.parts[0].text;
    current.update();
} catch (ex) {
    gs.error("AI Summary Error: " + ex.message);
}

View solution in original post

7 REPLIES 7

Ankur Bawiskar
Tera Patron
Tera Patron

@Community Alums 

what debugging did you perform?

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
Tera Patron
Tera Patron

@Community Alums 

did the API was consumed? what was the response?

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

Community Alums
Not applicable

@Ankur Bawiskar I managed to fix it. Here's the script I used.

action.setRedirectURL(current);

var ask = "summarise";
var payloads = []; 


var xmlgr = new GlideRecord("sys_update_xml");
xmlgr.addQuery("update_set", current.sys_id);
xmlgr.query();
while (xmlgr.next()) {
    if (!xmlgr.payload.nil()) { 
        payloads.push(xmlgr.payload.toString());
    }
}

// Combine all payloads into a single string
var payloadText = payloads.join("\n\n");  // Separate payloads for readability

try {
    var body = JSON.stringify({
        "contents": [{
            "parts": [{
                "text": ask + " " + payloadText + " " + current.description.toString()
            }]
        }]
    });

    var r = new sn_ws.RESTMessageV2('GoogleGemini', 'POST');
    r.setRequestBody(body);
    
    var response = r.execute();
    var responseBody = response.getBody();
    var res = JSON.parse(responseBody);
    
    current.description = res.candidates[0].content.parts[0].text;
    current.update();
} catch (ex) {
    gs.error("AI Summary Error: " + ex.message);
}

Hi @Community Alums ,

I am getting final response like below without proper formatting like **. Can this be aligned properly.

**Key Features and Capabilities:**

* **IT Service Management (ITSM):** This is ServiceNow's core offering, helping organizations manage incidents, problems, changes, requests, and assets related to IT infrastructure and applications. It streamlines workflows, improves efficiency, and reduces downtime.

* **Customer Service Management (CSM):** Extends the ITSM capabilities to handle customer requests and issues, improving customer satisfaction and streamlining support processes.

 

Thanks,

Dhanunjay