Iframe with script so i get the current page incident details like subject , description upon click

Aditya007aa
Tera Contributor

Hi Everyone,

I am able to create an iFrame inside the Incident page.

Now I want to auto populate those current Incident records like Incident Number & Incident Subject when I click on a process button so it auto populates those details into my A360 process.

 

Or 


I have also created a Widget with scripts added there and I am able to add that widget in "Home" page only, I want to add that widget in the "Incident" page that will solve my problem very easily.

If anyone can help me with either of the issue, it would be so helpful.

15 REPLIES 15

@Aditya007aa 

then try to see in somehow if you can access the URL

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

Hi @Ankur Bawiskar 

I have attached the screenshot where if you would see I am able to get the incident number, subject, description etc. using the script I have written attached image.

The iframe is also loading correctly.

Now when I press the button highlighted in red in the attached image , then the incident data should be auto -populated in my form ( image attached).

Script i have written :-

function onLoad() {
    console.log("Client Script Loaded!");
 
    // Function to fetch the Incident Number dynamically
    function getLatestData() {
        return {
            4938: {
//TextBox0: "INC67123"
                TextBox0: g_form.getValue("number"), // Retrieves Incident Number
TextBox1: g_form.getValue("state"), // Retrieves Incident state
TextBox2: g_form.getValue("short_description"), // Retrieves Incident short description
TextBox3: g_form.getValue("description"), // Retrieves Incident description
TextBox4: g_form.getValue("category"), // Retrieves Incident category
TextBox5: g_form.getValue("impact"), // Retrieves Incident impact
            }
        };
    }
    console.log("Incident Data: ", JSON.stringify(getLatestData()));
 
    // Workaround: Create a Custom Event to send messages
    function sendMessageToIframe() {
        var iframeElement = document.querySelector('iframe[name="aari-embedded-app"]');
        if (iframeElement) {
            console.log("Iframe found, sending message...");
            var mappedHostData = getLatestData();
            iframeElement.dispatchEvent(
                new CustomEvent("sendAariData", {
                    detail: { data: mappedHostData[4938] }
                })
            );
        } else {
            console.error("Iframe not found!");
        }
    }
 
    // Delay to allow the iframe to load before sending data
    setTimeout(sendMessageToIframe, 1000);
}

 

You can check & resolve it( listener seems most likely the issue)

Hi @Ankur Bawiskar 
The code below is the listerner issue, the red button that i click highlighted above image , the ID of that process is mentioned a "4938". which means when i click on that process it should auto fill my form , form is attached in previous message image

--------------------

 return {
            4938: {

@Aditya007aa 

I don't think using onLoad client script you can access the elements within iframe.

whatever needs to be done should be within the widget

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

Hi @Ankur Bawiskar 
How can that be done, if you could guide me on that , with all the info i shared above and as you mentioned everything should be on widget side.
My instance of service now is classic UI