Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Error popping twice while Save&Close and Save&New

Posted on by 280

I had this custom entity where the user can only fill one of three look up fields, if more than 1 are populated a error pops up. I used a custom Jscript to achieve the functionality. Triggering it on OnSave event. It is working alright when clicked save but the error is thrown twice when tried to Save&Close and Save&New. 

function fnOnSave(context) {
   
    
    var account = Xrm.Page.getAttribute("parentaccountid") != null ? Xrm.Page.getAttribute("parentaccountid").getValue() : null;
    var contact = Xrm.Page.getAttribute("parentcontactid") != null ? Xrm.Page.getAttribute("parentcontactid").getValue() : null;
    var user = Xrm.Page.getAttribute("new_user") != null ? Xrm.Page.getAttribute("new_user").getValue() : null;
    var saveEvent = context.getEventArgs();

    if ((account != null && contact != null) || (account != null && user != null) || (contact != null && user != null)) {
        alert("You can only populate one of the lead source look-up fields - Account, Contact, or User.");
        saveEvent.preventDefault();
return false; } }


*This post is locked for comments

  • Verified answer
    tw0sh3ds Profile Picture
    tw0sh3ds 5,600 on at
    RE: Error popping twice while Save&Close and Save&New

    Do you have auto-save enabled? If yes then probably that's the reason - you are trying to save the record, your code runs and prevents the save, but both Save&Close and Save&New are also trying to close the form and the auto-save is therefore triggered, causing the second call). I'm nout sure if that's not a bug in CRM behaviour - you should try how it behaves if you change your legacy form rendering option. Anyway simply check if your save is autosave

    var eventArgs = context.getEventArgs();

     if (eventArgs.getSaveMode() == 70) {

         //this is auto save

     }

    and don't pop-up message if it's autosave, simply prevent default.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans