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

Notifications

Announcements

No record found.

Community site session details

Community site session details

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

Infinite Loop on OnLoad with openForm

(2) ShareShare
ReportReport
Posted on by 145

Ahoy, 

When I attach the below code on the OnLoad event of the form, it creates an infinite loop. The form that the code navigates to does not have the below code on it. Any ideas how to not make this loop? window.open also gets ignored. 

function switchContactForm(executionContext) {

	var formContext = executionContext.getFormContext();
	
	var formOptions = {};

	formOptions["entityName"] = "contact";

	formOptions["entityId"] = formContext.data.entity.getId();	
	
	formOptions["formid"] = "";

	Xrm.Navigation.openForm(formOptions);
};

I have the same question (0)
  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    To break infinite loop you should analyze current form that is opened. You can try something like:

    function switchContactForm(executionContext) {
    	var formContext = executionContext.getFormContext();
    
        var formId = "";
        
        var currentFormId = formContext.ui.formSelector.getCurrentItem().getId();
        
        if (formId === currentFormId) {
            return;
        }
    	
    	var formOptions = {};
    
    	formOptions["entityName"] = "contact";
    
    	formOptions["entityId"] = formContext.data.entity.getId();	
    	
    	formOptions["formid"] = formId;
    
    	Xrm.Navigation.openForm(formOptions);
    }

  • rikrokNA Profile Picture
    145 on at

    Yeah! I tried doing this, but it just keeps looping. It knows its not the right form, and as I mentioned, the other form doesn't have this javascript on it's OnLoad condition.

    Both modifications return an infinite loop. the second modification shows "I'm navigating" in the infinite loop. 

    if (formContext.ui.formSelector.getCurrentItem().getId() == myFormId) {
    	
    	console.log("I'm returning");
    		
    	return;			
    }	
    	
    Xrm.Navigation.openForm(formOptions);

    if (formContext.ui.formSelector.getCurrentItem().getId() != myFormId) {
    		
    	console.log("I'm navigating");
    	
    	Xrm.Navigation.openForm(formOptions);
    		
    }	
    	
    return;

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Can you please provide the Id that you use in the code? I believe the reason is in formatting.

  • rikrokNA Profile Picture
    145 on at

    Ok. "19796693-2701-4568-8h9n-x80ef4e38183";

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    I'm afraid your guid is wrong. Here is the online checker - http://guid.us/Test/GUID...

    Your string is not a guid and I believe that's what wrong...

    My recommendation - add alert(formContext.ui.formSelector.getCurrentItem().getId()); into your code and you will get proper id of your form and you will be able to use it after. BTW ctrl+c work fine to copy guid from alert dialog.

    Good luck.

  • rikrokNA Profile Picture
    145 on at

    Nope, no dice. The GUID is a correct one. Evidenced by pasting it in the URL to a web address, copying it (using Control + C) and pasting it (Control + V), and it matches the console.log as well. Additionally, it would not have worked in the if statement conditions you suggested earlier.

    It seems to just keep opening itself over and over again because of this OnLoad condition. It actually isn't navigating to the other form, which I did try in an URL to check to see if that GUID is in fact the right targeted form. Are there any permissions or other things that could telling it "No thanks"?

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    I will not argue. I'm afraid I can't help you. Good luck.

  • rikrokNA Profile Picture
    145 on at

    Alright, thanks for your help.

  • Verified answer
    meelamri Profile Picture
    13,216 User Group Leader on at

    Hello partner,

    Infinity loop is caused by the fact you are passing the wrong parameter to your formOptions object. Please refer to the code.

    function switchContactForm(executionContext) {
    
    	var formContext = executionContext.getFormContext();	
    	var formOptions = {};
    	formOptions["entityName"] = "contact";
    	formOptions["entityId"] = formContext.data.entity.getId();		
        
        //use formId not formid haha =)
        //formOptions["formid"] = "";
        
        formOptions["formId"] = "";
    
    	Xrm.Navigation.openForm(formOptions);
    }

    Mehdi El Amri

    Please tick as verified if the answer is useful. 

  • NoellaB Profile Picture
    165 User Group Leader on at

    Hello,

    I have tested Mehdi El Amri code and it's working.

    His answer need to be verified.

    Thank you !

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 70 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 33 Most Valuable Professional

#3
Daniyal Khaleel Profile Picture

Daniyal Khaleel 32 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans