Hello! does anybody know if there is a way to add...
# suitescript
m
Hello! does anybody know if there is a way to add a message on a window opener page as Netsuite does? Consider, for example, projects and project tasks: when you edit a project task a popup is opened and after saving a message is reported on the project page without reloading the page (see the attached image). Thank you in advance, Manuel
m
Thank you. My problem is to execute the N/ui/message on the window opener
n
Something like this work in the beforeload UE script:
Copy code
var myMsg = message.create({
	        		title 	: 'Return Failed',
	        		message : 'Review Related Records for RA & Item Receipts. You will need to manually enter for this Return.',
	        		type 	: message.Type.WARNING
	        	})
	        	 //display message
	        	context.form.addPageInitMessage({
	    				message		: myMsg,
	            		duration	: 50000
	    				});
m
I know
but i am not in a server script
because I do net have to reload the page
n
Could you explain what you mean by you wanting it to happen on a window opener? I suspect you just need to add the functionality to a client script since you can definitely create messages using N/ui/message in a client script. As for how you might implement that I'm not clear on your requirement right now.
m
I think I am nearly there. window.addEventListener("beforeunload", (e) => { window.opener.require(['N/ui/message'], (message) => { let myMsg = message.create({ title: 'My Title', message: 'My Message', type: message.Type.ERROR }); myMsg.show(); }); return true;});
n
Ah ok, cool.
m
When the project task window closes, the project page updates
👍🏻 1
Ok. I post here the solution. I don't know if it is the best solution: it works and, by now, it is enough. I already had a user event that made some checks on project tasks and saved an error message in the session. I added a beforeLoad method on the same User Event that adds a client script to the form.
Copy code
const beforeLoad = (scriptContext) => {
            
            let current_session = runtime.getCurrentSession();
            if(scriptContext.newRecord.id){
                overallocation_message = current_session.get({name: "overallocation_message_"+scriptContext.newRecord.id});
                if(overallocation_message){
                    scriptContext.form.addPageInitMessage({
                        title: "Over Allocations", 
                        message: overallocation_message, 
                        type: message.Type.ERROR
                    });
                    current_session.set({name: "overallocation_message_"+scriptContext.newRecord.id, value: null});
                }
                
            }
            scriptContext.form.clientScriptModulePath = "/SuiteScripts/ResourceCalendar/aly_resource_plan_show_error_cs.js";
        }
I added a Client Script that uses the window.opener and the unload event.
Copy code
define(['N/currentRecord'],
/**
 * @param{runtime} runtime
 * @param{message} message
 */
function(currentRecord) {
    
    window.addEventListener("unload", (e) => {
        var task = currentRecord.get();
        alert("TASK "+task.id);
        window.opener.require(['N/ui/message', 'N/runtime'], (message, runtime) => {            
            let current_session = runtime.getCurrentSession();
            var overallocation_message = current_session.get({name: "overallocation_message_"+task.id});
            if(overallocation_message){
                var error_message=message.create({
                    title: "Over Allocations", 
                    message: overallocation_message, 
                    type: message.Type.ERROR
                });
                error_message.show();
                current_session.set({name: "overallocation_message_"+task.id, value: ""});
            }  
        });
        return true;
    });
    
});
In this way when the Project Task popup is closed, the project page is updated without reloading.