web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

How to use a function from Microsoft's Main_system_library.js in my own .js resource?

(0) ShareShare
ReportReport
Posted on by

Hello,

I am trying to perform a test on presence of unsaved text in a text field before the user "Save & Close" their case. "Save & Close" is performed via button, with OOB function:

Library: $webresource:Main_system_library.js
Function Name: XrmCore.Commands.Save.saveAndCloseForm
Crm Parameter =PrimaryControl

I have decided to use XrmToolBox Ribbon Workbench to assign my own function to the "Save & Close" button, form my own .js library, which will:

  • Test if there is an unsaved text
    • If there is -> Gives alert and does nothing
    • If there is not -> Performs the same action as XrmCore.Commands.Save.saveAndCloseForm

Of course, $webresource:Main_system_library.js is not freely accessible since it is OOB Microsoft script. Now here is what I attempted, and where the problem is (if test works as intended):

if (textInPortalComment) {
    console.log("There is text in the Portal Comment field in Action Log.");
    Xrm.Utility.alertDialog("You have unsaved text in the Portal Comment field!");
} else {
    console.log("There is NO text in the Portal Comment field in Action Log.");
    
    /*
    HERE IS THE PROBLEM - DO NOT KNOW HOW TO EXECUTE THE FUNCTION THAT XrmCore.Commands.Save.saveAndCloseForm DOES:
    
    saveAndCloseForm(); - DOES NOTHING (expected)
    formContext.data.entity.save("saveandclose"); - DOES NOTHING
    Mscrm.SaveAndClosePrimary(); - DOES NOTHING (expected)
    Xrm.Page.data.save("saveandclose"); - GIVES ERROR: 0x83215603
    */
    Xrm.Page.data.entity.save("saveandclose"); // - GIVES ERROR: 0x83215603
};

Of course, what would be ideal, if I could in my own Custom JavaScript Action directly call the function XrmCore.Commands.Save.saveAndCloseForm form $webresource:Main_system_library.js. Or if I could at least access the $webresource:Main_system_library.js so I could copy XrmCore.Commands.Save.saveAndCloseForm into my own code, or some similar solution.

Any insight on how to call, or at least copy a function from Main_system_library.js (specifically function XrmCore.Commands.Save.saveAndCloseForm) in my own .js script?

I have the same question (0)
  • LY-16081107-0 Profile Picture
    3 on at
    How to use a function from Microsoft's Main_system_library.js in my own .js resource?
    Hi, I have a similar kind of request where I want to refresh the page using the "XrmCore.Commands.Refresh.refreshCommand" function from Main_system_library.js, is it possible to write my function and call it from a power automate process, and if yes can you please guide me on how to do that so ?

    I have found the API call to refresh the page data.refresh (Client API reference) in model-driven apps - Power Apps | Microsoft Learn
  • Suggested answer
    LeoAlt Profile Picture
    16,331 Moderator on at
    RE: How to use a function from Microsoft's Main_system_library.js in my own .js resource?

    Hi partner,

    We really don't recommend that you modify or call the JS that comes with the system.

    If you want to use the oob functions, you could write another new function.

    For example, you want to create a "save&close" function and check if the email feild has been entered when save the form, you could use the following code.


        function  chechField(executionContext){
            var formcontext=executionContext.getFormContext();
            var field=formcontext.getControl("new_email");
            var value=field.getValue();
            if(value==undefined||value==""){
                Xrm.Utility.alertDialog("You should enter the email before saving the form!");
                executionContext.getEventArgs().preventDefault();
            }
            else{
                formContext.data.entity.save("saveandclose");
            }
        }

    And if I did not fill in the email and I click on save&close, this record will not be saved and the alert message will be shown.

    pastedimage1575601879200v2.png

    https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/reference/formcontext-data-entity/save

    pastedimage1575601506320v1.png

    Dynamics 365 provided a very powerful and friendly js platform for developers, we could use the Xrm platform to complete the new features we need or copy the OOB features.

    Hope it helps.

    Best Regards,

    Leo

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Sahan Hasitha Profile Picture

Sahan Hasitha 225

#2
DAnny3211 Profile Picture

DAnny3211 134

#3
Abhilash Warrier Profile Picture

Abhilash Warrier 129 Super User 2025 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans