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 :
Finance | Project Operations, Human Resources, ...
Suggested Answer

How to get formrun instance of caller form in pre main method event of the class.

(0) ShareShare
ReportReport
Posted on by

Hello folks,

Please suggest how to get caller formrun instance in the pre event of Main method of a class

 [PreHandlerFor(classStr(SalesFormLetter), staticMethodStr(SalesFormLetter, main))]
    public static void SalesFormLetter_Pre_main(XppPrePostArgs args)
    {
        args parmArgs = args.getArg('args');
       
        //get caller form instance of this class and 
        
        if (some condition match)
        {
            callerform.close();
        }
        
    }

Thanks

Mav

I have the same question (0)
  • Suggested answer
    Martin Dráb Profile Picture
    237,866 Most Valuable Professional on at

    If the class was called with a form as a caller(), you can get it by calling parmArgs.caller().

    But you shouldn't use PreHandlerFor and get the parameter by name - it's all easier and safer with CoC:

    [ExtensionOf(classStr(SalesFormLetter))]
    final class MySalesFormLetter_Extension
    {
    	public static void main(Args _args)
    	{
    		next main(_args);
    		
    		FormRun callerForm = _args.caller() as FormRun;
    		if (callerForm)
    		{
    			callerForm.close();
    		}
    	}
    }

  • Mav Profile Picture
    on at

    Thanks Martin, COC is NO CAN DO as ISVs have already done COC's for that feature.

    I think COC should be blocked  until evolved to a level where 1 can decide the sequence in which the COC for same methods will run.

    Currently if you have multiple ISV (which mostly happens) then you will see all sort of issues because these ISV's unaware of each other code bases would develop COC for the most common methods in their solution , it is inevitable. And now when you use these ISV's  in your codebase , you have  multiple COC on same method  and  there is no way you can control which will run when +  you cannot develop your COC too at because even amongst the layer ISV vs Cus/Usr COC runs randomly.

    Apparently COC brings bring back the horrors of Ax2012 over layering.

  • Martin Dráb Profile Picture
    237,866 Most Valuable Professional on at

    According to your questions, you're not very experienced developer. When the rest of world can use CoC and you aren't able, didn't you consider that you may be doing something wrong?

    You'll have "all sorts of issues" if you (or the ISVs) don't write code in the right way. If your code depends on some ISV solution running before it, or it breaks ISVs' extensions, it's not designed correctly. For example, if it depends on an ISV solution, it should extend the ISV solution and not just hope that it'll be installed and executed before your extension.

    We also have a plenty of ISVs solutions and nobody ever considered leaving CoC for a much unsafer way for doing the same.

    Also, you're suggesting that PreHandlerFor is a solution, but it has exactly the same problem. If you have more event handlers on the same method, you don't know in which order they'll be executed.

  • Mav Profile Picture
    on at

    Did I upset you  ? If so then it was purely unintentional , I respect you a lot , you are the citadel of Ax knowledge.

    I was just laying out the facts, there are numerous cases where the issue with COC as shared above are  reported and then the customer is forced to develop a solution using event handlers, at times that too don't work because the methods are not public and this is how customer is directed to long tedious process of adding extensibility request or adding idea.

    You can verify my facts unlike your view above ,  by just reading why the idea was registered on idea website or  why the extensisibilty requests was created and the fact that so many ideas + extensibity request have been processed also verify my claim that at times things are locked and 1 has to wait for them to open up.

    I wish I had control on ISV's and ask them that when building COC on most commonly used methods, as a courtesy please make your solution extendible (There again we would pray that we do not land in situation where we have to make extension of an extension , because that too also does not work).

    Did you ever wonder, ISV's could come back saying that why cant the base code detect that extension is being made of commonly used class and automatically make it adhere to extensibility approach ,thereby minimizing or zeroing out the change of human error of not building the class adhering to extensibility .

    I think in 2022 , I would agree with ISV's on that if something has to happen when they develop extension then base code should take care of it.

    Thanks

    Mav

  • Martin Dráb Profile Picture
    237,866 Most Valuable Professional on at

    I agree that many things are not designed in an extensible way (that's no surprise) and extensible requests are often necessary. There is no disagreement - but that wasn't the topic of our discussion.

    I reacted to your statement that CoC can't be used and should be blocked. And the implication that using PreHandlerFor is good and that there is not such a problem with the execution sequence.

    Anyway, back to the original question. Is it now clear to you have to get a reference to the caller form?

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 > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 659 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 533 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 289 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans