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 :
Microsoft Dynamics AX (Archived)

How to pass value from button:clicked to static void main class using x++

(0) ShareShare
ReportReport
Posted on by 53

Good Day Mam/Sir,

I have a button. I need to get the name of my button then pass it to static void main in class.

Please see below my codes:

Thanks in advance :)

---------------------------------------------------------Start code of Button: Clicked-------------------------------------------------------------------------------------------

void clicked()
{
PurchFormLetter purchFormLetter;

purchFormLetter.parmBtnID(this.name());
super();
}

---------------------------------------------------------End code of Button: Clicked-------------------------------------------------------------------------------------------

---------------------------------------------------------Start code of parmBtnID-------------------------------------------------------------------------------------------

String30 parmBtnID(String30 _btnID = btnID)
{
btnID = _btnID;

return btnID;
}

---------------------------------------------------------End code of parmBtnID-------------------------------------------------------------------------------------------

---------------------------------------------------------Start code of static client void main-------------------------------------------------------------------------------------------

static client void main(Args args)
{
Common record;
int parmEnum;
Object callerForm;
IdentifierName callerFormName;
MenuItemNameAction callerMenuItem;
boolean isProforma;
FormDataSource recordDataSource;
container dataSourceRecordsPacked;
container cachedArgs;
PurchTable purchTable;
boolean recordExists;
Counter numberOfRecords;
str className, methodName;
SysOperationExecutionMode mode;
container packedPurchFormletter;
FormletterOutputContract outputContract;
PurchFormLetter purchFormLetter;
VendPurchOrderJour vendPurchOrderJourOld,vendPurchOrderJourNew;
String30 myID;

if (!args)
{
throw error("@SYS25407");
}
[className, methodName, mode] = FormLetterServiceController::parseServiceInfo(args);
record = args.record();
parmEnum = args.parmEnum();
callerForm = args.caller();
callerMenuItem = args.menuItemName();

if (callerForm)
{
callerFormName = args.caller().name();
}
else if(args.menuItemName() == identifierStr(EPVendInvoice) || args.menuItemName() == identifierStr(EPVendInvoiceVendAdvance))
{
callerFormName = args.name();
}

isProforma = FormLetterServiceController::isMenuItemProforma(args.menuItemName());

if (record && record.dataSource())
{
recordDataSource = record.dataSource();
dataSourceRecordsPacked = FormLetter::getFormRecord(recordDataSource);
}

cachedArgs = [#CachedArgsList];

packedPurchFormletter = PurchFormLetter::mainOnServer(cachedArgs, record, callerForm, dataSourceRecordsPacked);

if (packedPurchFormletter != conNull())
{
purchFormLetter = PurchFormLetter::construct(parmEnum, className, methodName, mode, callerMenuItem);
purchFormLetter.unpack(packedPurchFormletter);
purchFormLetter.init();

purchFormLetter.prePromptModifyData();

if (!purchFormLetter.shouldSkipPrompt() && purchFormLetter.prompt())
{
purchFormLetter.run();
outputContract = purchFormLetter.getOutputContract();
numberOfRecords = outputContract.parmNumberOfOrdersPosted();
}
}

if (numberOfRecords
// <GEERU>
|| (purchFormLetter && purchFormLetter.doRefreshCallerDataSource())
// </GEERU>
)
{
if (callerForm && formHasMethod(callerForm,identifierStr(interCompanyRefreshCache)))
{
callerForm.interCompanyRefreshCache();
}

if (recordDataSource)
{
if (record is PurchTable)
{
purchTable = record as PurchTable;
recordExists = PurchTable::exist(purchTable.PurchId);
}
else
if (record is PurchParmTable)
{
recordExists = false;
}
// <GEERU>
else
if (SysCountryRegionCode::isLegalEntityInCountryRegion([#isoRU]) &&
record is VendPackingSlipJour)
{
recordExists = VendPackingSlipJour::findRecId(record.RecId).RecId;
}
else if (record is EGAISWaybillJour_RU)
{
recordExists = EGAISWaybillJour_RU::findRecId(record.RecId).RecId;
}
// </GEERU>
// <GEEPL>
else if (record is PlSADTable)
{
recordExists = PlSADTable::findRecId(record.RecId).RecId;
}
// </GEEPL>
FormLetter::reFreshCallerDataSource(recordDataSource,
recordExists,
(numberOfRecords > 1));


myID=purchFormLetter.parmBtnID();
info(strfmt("my BTN name is: ",myID));

/*
//-------------------------------------Start of Old VendPurchOrderJour------------------------------------------------------------
select firstOnly vendPurchOrderJourOld order by vendPurchOrderJourOld.RecId asc
where vendPurchOrderJourOld.PurchId == purchTable.PurchId ;
//-------------------------------------End of Old VendPurchOrderJour------------------------------------------------------------

if(vendPurchOrderJourOld.PurchId)
{

ttsBegin;
select forupdate vendPurchOrderJourNew order by vendPurchOrderJourNew.RecId desc
where vendPurchOrderJourNew.PurchId == purchTable.PurchId ;
vendPurchOrderJourNew.PurchaseOrderIdOld = vendPurchOrderJourNew.PurchaseOrderId;
vendPurchOrderJourNew.PurchaseOrderId=vendPurchOrderJourOld.PurchaseOrderId;
vendPurchOrderJourNew.update();
ttsCommit;

}

*/
}
}
}

---------------------------------------------------------End code of static client void main-------------------------------------------------------------------------------------------

*This post is locked for comments

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

    The standard code above shows you how to get the menu item name (args.menuItemName()). Isn't it sufficient?

    What's the point of knowing the name of a particular button? You would take dependency on an implementation detail that should be irrelevant to the posting logic, therefore the whole idea sounds suspicious to me.

  • Gelo Tabon Profile Picture
    53 on at

    Thank you for your replied sir martin.

    I have 2 buttons in my form (PurchTableListPage). The first button is to Confirm the PO then it will generate a new Purchase order confirmations.

    and the 2nd button is to confirm the PO but it will not generate a new Purchase Order, it will just copy the existing purchase order confirmations.

    my logic is to get the name of the button that clicked by the user, it will pass the values to static void main of the class (PurchFormLetter)

    . Once the static void main detected that the 2nd button triggered, it will perform the code of the below:

    myID=purchFormLetter.parmBtnID();

    if(myID=="2ndButtonName")

     {

       select firstOnly vendPurchOrderJourOld order by vendPurchOrderJourOld.RecId asc

       where vendPurchOrderJourOld.PurchId == purchTable.PurchId ;

       if(vendPurchOrderJourOld.PurchId)

       {

         ttsBegin;

            select forupdate vendPurchOrderJourNew order by vendPurchOrderJourNew.RecId desc

            where vendPurchOrderJourNew.PurchId == purchTable.PurchId ;

            vendPurchOrderJourNew.PurchaseOrderIdOld = vendPurchOrderJourNew.PurchaseOrderId;

            vendPurchOrderJourNew.PurchaseOrderId=vendPurchOrderJourOld.PurchaseOrderId;

            vendPurchOrderJourNew.update();

         ttsCommit;

       }

     }

    Thanks,,

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

    I can't say I understand your logic. When you say "it will not generate a new Purchase Order", I guess you mean a new confirmation, because this process shouldn't create a new order. And if you don't create a confirmation, is it true that you confirmed the order? I wouldn't say so. Also, creating a copy of an old confirmation doesn't make sense to me. It's for a previous state of the order, which may be very different from the current one.

    Anyway, if you insist of copying an old confirmation, create a class for this logic, then a menu item pointing to this class and drage the menu item to the form, which will create a new menu item button calling your class.

    Don't mix it with actual confirmation posting and don't base your business logic on form control names.

  • Gelo Tabon Profile Picture
    53 on at

    sorry, sir Martin for the wrong typo.

    I mean "It will not generate a new Purchase Order Confirmation". not "Generate a new Purchase Order"

  • Suggested answer
    Pedro Tornich Profile Picture
    955 on at

    Hello Gelo,

    It's a bad design to rely on form control names for your logic, if you have two buttons that will perform similar logic you should create two separate menu items and use the Enum property of each new menu item to identify which process must be executed by the class.

    If it's a complex process, you should consider having a base class and two child classes. Then on the main() method of your base class you can use the enum value from the _args parameter to construct the right child object.

  • Suggested answer
    Khushhal Garg Profile Picture
    1,514 on at

    There are already 2 separate button. Just call separate methods for their own logic to trigger from clicked method of these buttons. Passing button name doesn't sound very good design.

  • Gelo Tabon Profile Picture
    53 on at

    Thank you, Pedro

    how I can do that if the enum has existed value?

    Please see the attached image below:

    5873.png

    Thanks sir,

  • Gelo Tabon Profile Picture
    53 on at

    Thank you for your response Khushhal,

    Can you please give me examples or link that related that you have mentioned?

    Thank you

  • Suggested answer
    Khushhal Garg Profile Picture
    1,514 on at

    You can do something like this. Create a class say Class1 and have 2 methods method1 and method2. These can be static methods. Now, on clicked method of first button can call method1 and second button can call method2. method1 can perform confirmation and generate new PO confirmation. method2 can copy existing PO confirmation. Does it make sense?

    button1 > clicked method

    Class1::method1()

    button2 > clicked method

    Class2::method2()

    If button1 and button2 are menu items and there is same class behind both. Then based on menu item name, different actions can be performed within class.

  • Verified answer
    Pedro Tornich Profile Picture
    955 on at

    Hi Gelo,

    Now I understand your problem. You need to change some behavior on the PurchFormLetter class, but you cannot use the menu item properties because they were already used by the framework.

    So I will keep things simple for you.

    Probably Microsoft engineers had the same problem when they were developing D365 renewed FormLetter framework, because the caller menu item name is a global variable on every FormLetter class and it's extracted from the args parameter on the main() method.

    If you open up the PurchFomLetter code and search for "callerMenuItem" you will find several references to it. So I think you should use the same trick and base your conditions on the caller menu item (not the button).

    Here is a sample code:

    if(purchFormLetter.parmCallerMenuItem() == menuItemActionStr(PurchFormLetter_PurchaseOrder_Action))
    {
        // Standard process
    }
    else
    {
        // Customized process
    }

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 > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans