Show related records on CHANGE or SUBMIT

Nic Omaha
Tera Guru

I have a form where a user can submit a ticket on a record producer for a custom table inheriting from task. The issue I am trying to solve is that they should not be submitting a ticket for an order IF an order is already open. I have written a on Submit client script that will stop and prompt them the Existing Order number but what I would really like to do is show existing orders on CHANGE of the order number field. It would be great if they could click into or be redirected to them. This could also happen on submit if CHANGE is not possible. Here is the script Ive been trying with no luck. 

function onSubmit() {
   //Type appropriate comment here, and begin script below
   var orderID = g_form.getValue("u_order_number");
    
    
    var orderResult = new GlideRecord("u_flooring_measures");
    orderResult.addQuery("active", true);
    orderResult.addQuery("u_order_number", orderID);
    orderResult.query();

    if (orderResult.next()) {
        if (confirm("There is already a open Measure Request for this order, Would you like to view it?")) {
            var resultID = g_form.getUniqueValue();
            var editURL = "/u_flooring_measures.do?sys_id=" + resultID;
            window.location.href = editURL;
        }
    } else {
        alert("Your Measure has not been submitted.");
    }
}
2 REPLIES 2

Saurabh Gupta
Kilo Patron
Kilo Patron

Hi,
This is not recommended to use server side APIs inside client side scripts.
Also for redirection try below script

function function onSubmit() {
    // If you want redirection based on certain conditions then you can have your if-else statements here.
    alert("You will be redirected");
    top.window.location = "https://www.google.com/ ";  // Change URL Based on your requirements.

    //Type appropriate comment here, and begin script below
}

Thanks and Regards,

Saurabh Gupta

Brad Bowman
Kilo Patron
Kilo Patron

 Aside from the verboten GlideRecord in a client script, it should still work in this case, or you can follow the rules and use a GlideAjax call to a Script Include to handle the GlideRecord

https://www.servicenow.com/community/developer-articles/glideajax-example-cheat-sheet-updated/ta-p/2... 

 

In either case do you really want to query for the same order number - is that what determines that an order is already open?  If that's correct, it seems like resultID should = orderResult.sys_id.toString(); or orderResult.getValue('sys_id'); to redirect to the record returned by the GR.  getUniqueValue is going to give you the sys_id of the current/new record, so that doesn't sound like what you want to redirect to.