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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

Closing browser page after saving CRM record

(1) ShareShare
ReportReport
Posted on by 25

Hello,

I am new to this community. 

This question might seem strange, but I wanted to know if it is possible to close the page automatically when the record is created?

For example - I am creating a sales order line, adding a product, price, qty. etc. and the Sales order line is being created in a new window. After I hit "SAVE" button, I want the page to be closed automatically, without having to press the "X" button on the browser window.

By the way, the CRM is on-prem version 8.2.

Looking forward to your replies!

I have the same question (0)
  • Suggested answer
    Pawar Pravin Profile Picture
    5,237 on at

    You can try below code to redirect on previous page on save of record:

    if (window.history.length > 1) {

               window.history.go(-1);

           }

  • Verified answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi,

    Xrm.Page.ui.close() or formContext.ui.close() (For version 9.0) should work for you. Follow below reference -

    docs.microsoft.com/.../close

  • Suggested answer
    thonda Profile Picture
    Microsoft Employee on at

    Yes. Make the above recommended formContext.ui.close() api method call in your onsave success event handler docs.microsoft.com/.../save

  • rv1911 Profile Picture
    25 on at

    This is the function I've created and made it to be onSave of the form + passing the exec. context.

    However this does not work, the window in which the record is being created remains open.

    Maybe the logic in the form is not correct?

    function onSaveCloseSOL(executionContext) {
    try {
    console.log("Getting the context");
    //get the context
    var formContext = executionContext.getFormContext();
    //save form and close
    formContext.data.save().then(function successCallback() {
    console.log("Form successfully saved.");
    formContext.ui.close();
    }
    );
    } catch (e) {
    XRM.Utility.alertDialog(e.message);
    }

    }

  • Suggested answer
    thonda Profile Picture
    Microsoft Employee on at

    Please confirm to which event this  onSaveCloseSOL function is attached, Is it onsave or onload of form?

    This should be attached to OnLoad of form. Pass errorcallback function param as well to the then() method call.

    You can try catch instead call Xrm.Navigation.openAlertDialog(alertStrings,alertOptions) in errorcallback.

  • Suggested answer
    ajyendra Profile Picture
    1,738 on at

    Hi,

    Call this function on OnSave Event and 

    Add error function like below. So If you get any error Please let us know

    function onSaveCloseSOL(executionContext) {
    try {
    console.log("Getting the context");
    //get the context
    var formContext = executionContext.getFormContext();
    //save form and close
    formContext.data.save().then(function successCallback() {
    console.log("Form successfully saved.");
    formContext.ui.close();
    },function error(e){

    alert(e);

    //Xrm.Navigation.openAlertDialog(alertStrings,alertOptions)

    }
    );
    } catch (e) {
    XRM.Utility.alertDialog(e.message);
    }

    }

  • thonda Profile Picture
    Microsoft Employee on at

    Is it not already late by the time OnSave event is fired to attach success failures callbacks to OnSave event itself?

    Why should not they be attached(formContext.data.save().then(successCallback,failurecallback) on the form OnLoad event, such that they ready on fire of OnSave Event.

  • Suggested answer
    ajyendra Profile Picture
    1,738 on at

    Hi,

    Whatever entity form you need to close on Save Just add the web resources which contain this below function addMessageToOnSave and call that function in Form Load 

    function displayOrgName(execContext){
    var formContext = execContext.getFormContext();
    formContext.ui.close();
    }
    function addMessageToOnSave(executionContext) {
    var formContext = executionContext.getFormContext();
    formContext.data.entity.addOnSave(displayOrgName);
    }

    No need to save that entity as When we click on Save it automatically call onsave event where it will execute code formContext.data.entity.save() by default.

    Hope It Helps

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
ManoVerse Profile Picture

ManoVerse 180 Super User 2026 Season 1

#2
11manish Profile Picture

11manish 123

#3
CU11031447-0 Profile Picture

CU11031447-0 100

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans