hljs.configure({cssSelector: "code"}); hljs.highlightAll();

Monday, June 19, 2023

Form level event handlers in D365FO

 

1. OnActivated.
//The primary purpose of the _OnActivated event handler is to handle specific actions or modifications
// that need to occur when a form or control becomes active. This can include tasks such as refreshing data,
 [FormEventHandler(formStr(PurchEditLines), FormEventType::Activated)] 
 public static void PurchEditLines_OnActivated(xFormRun sender, FormEventArgs e)
 { 
	 FormRun 		formrun = sender; //  form  formrun. 
	 FormDataSource formdatasource = formrun.dataSource();      // formdatasource   
	 PurchParmTable purchParmTable = formdatasource.cursor(); // active record.    
 }

2. OnClosing

// onclosing: the onclosing eventhanlders is triggered while closing the form.
[FormEventHandler(formStr(PurchEditLines), FormEventType::Closing)]
public static void PurchEditLines_OnClosing(xFormRun sender, FormEventArgs e)
{        
	FormRun         formrun = sender; //  form  formrun.
	FormDataSource  formdatasource = formrun.dataSource(); // formdatasource 
	PurchParmTable  purchParmTable = formdatasource.cursor();		
	formdatasource.reread(); 
	formdatasource.refresh();
	formdatasource.research();
}

3.OnInitialized.
//OnInitialized: The OnInitialized event handler is triggered after the form has been fully initialized 
//and is ready to be interacted with by the user. At this stage, all form controls have been rendered,
// and data sources and data bindings have been established. The OnInitialized event provides an opportunity to perform additional actions, 
//such as populating default values, setting focus on specific controls, or applying any post-initialization logic.

[FormEventHandler(formStr(PurchEditLines), FormEventType::Initialized)]
public static void PurchEditLines_OnInitialized(xFormRun sender, FormEventArgs e)
{        
	 FormRun         formrun = sender; //  form  formrun.
	 formrun         senderformrun = formrun.caller_RU();
	 PurchTable      purchtable = formrun.args().record(); // calling form active record using args.
}

4. OnInitializing

//OnInitializing: The OnInitializing event handler is triggered before the form is fully initialized
// and displayed to the user. It is used to perform any necessary setup or modifications on the form
// and its elements before they are rendered on the screen. This event allows you to customize the initial state of the form and its controls.


 [FormEventHandler(formStr(PurchEditLines), FormEventType::Initializing)]
 public static void PurchEditLines_OnInitializing(xFormRun sender, FormEventArgs e)
 {        
	FormRun                 formrun = sender;
	FormButtonControl       control = formrun.design().controlName('control name'); 
	control.visible(false); 
 }

5.OnPostRun

/// If you want to jump from FormControl, to another Form you can 
[FormEventHandler(formStr(PurchEditLines), FormEventType::PostRun)] 
public static void PurchEditLines_OnPostRun(xFormRun sender, FormEventArgs e)
{   
FormStringControl   control = sender.design().controlName(formControlStr(purcheditlines, ok));
control.registerOverrideMethod(methodStr(FormStringControl, jumpRef), methodStr(your jumrefclass, jumpRef), sender);
}


    -----------------------------------------------------------------------------------------------------------------------------------
6. OnActivated
// this is a datasource active method.

 [FormDataSourceEventHandler(formDataSourceStr(PurchEditLines, PurchParmUpdate), FormDataSourceEventType::Activated)]
 public static void PurchParmUpdate_OnActivated(FormDataSource sender, FormDataSourceEventArgs e)
 { 
	FormDataSource  formdatasource = sender; // current form datasource. 
	FormRun         formrun = formdatasource.formRun(); // current fromrun 
	FormControl     controlname = formrun.design().controlName('name of the control'); // to get the formcontrl.
	PurchParmTable purchparmtable = formdatasource.cursor(); // active record.
	if (purchparmtable.RecId)
	{    
	controlname.visible(true); 
	} 
 }

7.OnCreated
// The primary purpose of the OnCreated event handler is to handle specific actions or modifications that need
// to occur when a new record or object is created. This can include tasks such as initializing default values, 
//performing calculations, setting up relationships or dependencies, or any other behavior that should be triggered 
//when a new instance is created.

[FormDataSourceEventHandler(formDataSourceStr(PurchEditLines, PurchParmTable), FormDataSourceEventType::Created)]
public static void PurchParmTable_OnCreated(FormDataSource sender, FormDataSourceEventArgs e)
{ 
	FormDataSource  formdatasource = sender; // current form datasource.
	FormRun         formrun = formdatasource.formRun(); // current fromrun 
	PurchParmTable purchparmtable = formdatasource.cursor(); // active record.
	purchparmtable.DeliveryName = "default value";  // initilizing some default values to current record  
}

8.OnCreating

1.Allows customization of the record's initial state before it is saved.
2. Useful for performing validations, setting default values, or modifying the record before it is created.

[FormDataSourceEventHandler(formDataSourceStr(PurchEditLines, PurchParmTable),   FormDataSourceEventType::Creating)]
public static void PurchParmTable_OnCreating(FormDataSource sender, FormDataSourceEventArgs e)
{
   FormDataSource  formdatasource = sender; // current form datasource.
   PurchParmTable purchparmtable = formdatasource.cursor(); // active record.
   purchparmtable.validations();
}

9.OnDeleted
//The primary purpose of the OnDeleted event handler is to handle specific actions or modifications
// that need to occur when a record or object is deleted. This can include tasks such as updating related records,
// performing clean-up operations, logging deletion details, or any other behavior that should be triggered when a deletion occurs.

[FormDataSourceEventHandler(formDataSourceStr(PurchEditLines, PurchParmTable), FormDataSourceEventType::Deleted)]
public static void PurchParmTable_OnDeleted(FormDataSource sender, FormDataSourceEventArgs e)
{    
	FormDataSource  formdatasource = sender; // current form datasource. 
	PurchParmTable purchparmtable = formdatasource.cursor(); // active record  
	if (purchparmtable.PurchId) 
	{        
	 warning("since purchi id is available you cant delete the record");  
	}  
}


10.OnDisplayOptionInitialize
//The primary purpose of the OnDisplayOptionInitialize event handler is to handle specific actions
// or modifications related to the display options of form controls. This can include tasks such as enabling 
//or disabling controls, setting their visibility, modifying their labels, or any other behavior
// that should be applied during the initialization of the form.

[FormDataSourceEventHandler(formDataSourceStr(PurchEditLines, PurchParmTable), FormDataSourceEventType::DisplayOptionInitialize)]
public static void PurchParmTable_OnDisplayOptionInitialize(FormDataSource sender, FormDataSourceEventArgs e)
{   
	FormDataSource  formdatasource = sender; // current form datasource. 
	FormRun         formrun = formdatasource.formRun(); // current fromrun 
	PurchParmTable purchparmtable = formdatasource.cursor(); // active record 
	FormControl     controlname = formrun.design().controlName('name of the control'); // to get the formcontrl. 
	if (purchparmtable.PurchId) 
	{ 
	   controlname.allowEdit(true); 
	}  
}
	
11.OnInitialized
 // The OnInitilize event handler at the datasource level is particularly useful
 //when you need to customize or set up the initial state of the datasource and its related elements, 
 //such as fields or relations. It allows you to perform actions that should occur during the initialization phase of the datasource,
 //ensuring that the datasource is prepared and ready for data operations.
 
[FormDataSourceEventHandler(formDataSourceStr(PurchEditLines, PurchParmTable), FormDataSourceEventType::Initialized)]
public static void PurchParmTable_OnInitialized(FormDataSource sender, FormDataSourceEventArgs e)
{ 
	 FormDataSource  formdatasource = sender; // current form datasource. 
	 FormRun         formrun = formdatasource.formRun(); // current fromrun 
	 PurchParmTable purchparmtable = formdatasource.cursor(); // active record.
	 purchparmtable.PurchName =""; 
}

12.OnInitValue
//The primary purpose of the OnInitValue event handler at the datasource level is to handle
// specific actions or modifications that need to occur when a field's value is being initialized. 
//This can include tasks such as setting default values, performing calculations based on other field values,
// or any other behavior that should be triggered during the value initialization process.

[FormDataSourceEventHandler(formDataSourceStr(PurchEditLines, PurchParmTable), FormDataSourceEventType::InitValue)]
public static void PurchParmTable_OnInitValue(FormDataSource sender, FormDataSourceEventArgs e)
{  
  FormDataSource  formdatasource = sender; // current form datasource.
  FormRun         formrun = formdatasource.formRun(); // current fromrun
  PurchParmTable purchparmtable = formdatasource.cursor(); // active record.
  purchparmtable.PurchName ="";
}
  
13.OnLeavingRecord
//The primary purpose of the OnLeavingRecord event handler at the datasource level is to handle specific actions
// or modifications that need to occur when the focus is leaving a record. This can include tasks such as validating
// the record's data, performing calculations or updates based on the record's values, or any other behavior that should be 
//triggered before moving to another record.

[FormDataSourceEventHandler(formDataSourceStr(PurchEditLines, PurchParmTable), FormDataSourceEventType::LeavingRecord)]
public static void PurchParmTable_OnLeavingRecord(FormDataSource sender, FormDataSourceEventArgs e)
{ 
	FormDataSource  formdatasource = sender; // current form datasource. 
	FormRun         formrun = formdatasource.formRun(); // current fromrun
	PurchParmTable purchparmtable = formdatasource.cursor(); // active record.   
	if (purchparmtable.PurchName =="") 
	{  
		warning("purch name should be enetered");
	}  
}
	
14.OnLeftRecord
// The OnLeftRecord event handler at the datasource level is used to perform actions or operations
// after the focus has moved away from a record within a datasource. It is triggered after the focus has left
// the current record, allowing you to perform any necessary calculations, updates, or other actions related to the record

[FormDataSourceEventHandler(formDataSourceStr(PurchEditLines, PurchParmTable), FormDataSourceEventType::LeftRecord)]
 public static void PurchParmTable_OnLeftRecord(FormDataSource sender, FormDataSourceEventArgs e)
{        
	 FormDataSource  formdatasource = sender; // current form datasource.
	 FormRun         formrun = formdatasource.formRun(); // current fromrun 
	 PurchParmTable  purchparmtable = formdatasource.cursor(); // active record. 
	 select forupdate TableName      
	 where    TableName.fieldname == purchparmtable.fieldname;
	 
	 ttsbegin;
	 TableName.fieldname == purchparmtable.fieldname;
	 TableName.update();
	 ttscommit;
}
	
15.MarkChanged
//The primary purpose of the OnMarkChanged event handler at the datasource level is to handle specific
// actions or modifications that need to occur when a field's value has been changed. This can include
// tasks such as performing calculations, updating related fields or records, or any other behavior that
// should be triggered when a field is marked as changed.

[FormDataSourceEventHandler(formDataSourceStr(PurchEditLines, PurchParmTable), FormDataSourceEventType::MarkChanged)]
public static void PurchParmTable_OnMarkChanged(FormDataSource sender, FormDataSourceEventArgs e)
{  
	FormDataSource  formdatasource = sender; // current form datasource. 
	FormRun         formrun = formdatasource.formRun(); // current fromrun 
	PurchParmTable  purchparmtable = formdatasource.cursor(); // active record.
	
	formdatasource.object(fieldNum(PurchParmTable,PurchName)); 
	 
} 

16.OnPostLinkActive
//The OnPostLinkActive event handler is called when a link is activated on a form data source.
// It is used to perform some action after the link has been activated. The event handler is defined in the FormDataSource class
// and can be overridden in a subclass. The event handler has the following

[FormDataSourceEventHandler(formDataSourceStr(PurchEditLines, PurchParmTable), FormDataSourceEventType::PostLinkActive)]
public static void PurchParmTable_OnPostLinkActive(FormDataSource sender, FormDataSourceEventArgs e)
{      
	FormDataSource  formdatasource = sender; // current form datasource.
	FormRun         formrun = formdatasource.formRun(); // current fromrun
	PurchParmTable  purchparmtable = formdatasource.cursor(); // active record. 
}
	
17.OnQueryExecuted

//The primary purpose of the OnQueryExecuted event handler at the datasource level is to handle specific actions or modifications
// that need to occur after a query has been executed. This can include tasks such as manipulating the result set,
// performing calculations or aggregations on the retrieved data, or any other behavior that should be triggered after the query execution.

[FormDataSourceEventHandler(formDataSourceStr(PurchEditLines, PurchParmTable), FormDataSourceEventType::QueryExecuted)] 
public static void PurchParmTable_OnQueryExecuted(FormDataSource sender, FormDataSourceEventArgs e)
{       
	sender.query().dataSourceName(sender.name()).addRange(fieldnum(purchparmtable,     purchname)).value("");  
}

18.OnQueryExecuting

//The primary purpose of the OnQueryExecuting event handler at the datasource level is to handle specific actions
// or modifications that need to occur before a query is executed. This can include tasks such as modifying the query,
// applying filters, or any other behavior that should be triggered before the query is sent to the data source.

[FormDataSourceEventHandler(formDataSourceStr(PurchEditLines, PurchParmTable), FormDataSourceEventType::QueryExecuted)] 
public static void PurchParmTable_OnQueryExecuting(FormDataSource sender, FormDataSourceEventArgs e)
{       
	sender.query().dataSourceName(sender.name()).addRange(fieldnum(purchparmtable,     purchname)).value("");  
}

19.OnRefreshed
//The primary purpose of the OnRefreshed event handler at the datasource level is to handle specific actions or modifications 
//that need to occur after the datasource has been refreshed and all data-related events have been processed.
// This can include tasks such as updating dependent controls or calculations based on the refreshed data,
// or any other behavior that should be triggered after the data has been refreshed and all data-related events have been handled.

[FormDataSourceEventHandler(formDataSourceStr(PurchEditLines, PurchParmTable), FormDataSourceEventType::Refreshed)]
public static void PurchParmTable_OnRefreshed(FormDataSource sender, FormDataSourceEventArgs e) 
{   
	FormDataSource  formdatasource = sender; // current form datasource.
	FormRun         formrun = formdatasource.formRun(); // current fromrun.
	PurchParmTable  purchparmtable = formdatasource.cursor(); // active record. 
	controlname mycontrol = formrun.design().controlName('');
	if (purchparmtable)
	{
		mycontrol.enable(true);
	}
}

20.OnReread.

//The primary purpose of the OnReread event handler at the datasource level is to handle specific actions
// or modifications that need to occur after the datasource has been reloaded with the same set of data.
// This can include tasks such as refreshing dependent controls, recalculating values, or any other behavior
// that should be triggered after the data has been reloaded.

[FormDataSourceEventHandler(formDataSourceStr(PurchEditLines, PurchParmTable), FormDataSourceEventType::Reread)]
public static void PurchParmTable_OnReread(FormDataSource sender, FormDataSourceEventArgs e)
{  

}
	 
21.OnSelectionChanged.

//The primary purpose of the OnSelectionChanged event handler at the datasource level is to handle specific actions
// or modifications that need to occur when the selection of records in the datasource is changed.
//This can include tasks such as updating dependent controls, recalculating values, or any other behavior
// that should be triggered when the user changes the selected records.

[FormDataSourceEventHandler(formDataSourceStr(PurchEditLines, PurchParmTable), FormDataSourceEventType::SelectionChanged)]
public static void PurchParmTable_OnSelectionChanged(FormDataSource sender, FormDataSourceEventArgs e)
{   

}    

22.OnValidatedDelete

 [FormDataSourceEventHandler(formDataSourceStr(PurchEditLines, PurchParmTable), FormDataSourceEventType::ValidatedDelete)]
 public static void PurchParmTable_OnValidatedDelete(FormDataSource sender, FormDataSourceEventArgs e)
 {  
 }
 
23. OnValidatedWrite

 [FormDataSourceEventHandler(formDataSourceStr(PurchEditLines, PurchParmTable), FormDataSourceEventType::ValidatedWrite)]
 public static void PurchParmTable_OnValidatedWrite(FormDataSource sender, FormDataSourceEventArgs e)  
 {  
 }
 
 24.OnWriting.
 
 //The primary purpose of the OnWriting event handler at the datasource level is to handle specific actions
// or modifications that need to occur before a write operation is performed on the datasource. This can include tasks
// such as validating the data, applying business rules, modifying field values, or any other behavior that should be 
//triggered before the write operation takes place.

[FormDataSourceEventHandler(formDataSourceStr(PurchEditLines, PurchParmTable), FormDataSourceEventType::Writing)] 
public static void PurchParmTable_OnWriting(FormDataSource sender, FormDataSourceEventArgs e)  
{  
    FormDataSource CaseDetailBase_ds = sender.formRun().datasource(formDataSourceStr(purcheditlines,purchParmTable));
    CaseDetailBase_ds.refresh();
    CaseDetailBase_ds.research(true);
}    

25.OnWritten.
//The primary purpose of the OnWritten event handler at the datasource level is to handle specific actions
 //or modifications that need to occur after a write operation has been performed on the datasource.
 //This can include tasks such as refreshing dependent controls, updating related data, triggering notifications,
 //or any other behavior that should be triggered after the write operation has completed.
 
[FormDataSourceEventHandler(formDataSourceStr(PurchEditLines, PurchParmTable), FormDataSourceEventType::Written)]
public static void PurchParmTable_OnWritten(FormDataSource sender, FormDataSourceEventArgs e) 
{  
	formrun  formrun = sender.formRun();
	PurchParmTable  purchparmtable = sender.cursor(); 
	//Write your code
}
--------------------------------------------------------------------------------------------------------------------------------------
26.OnModified
[FormDataFieldEventHandler(formDataFieldStr(PurchEditLines, PurchParmTable, BankLCImportLine), FormDataFieldEventType::Modified)]
public static void BankLCImportLine_OnModified(FormDataObject sender, FormDataFieldEventArgs e)
{    
	FormDataSource          purchparmtable_ds   = sender.datasource(); // datasource. 
	PurchParmTable  purchparmtable = purchparmtable_ds.cursor(); // active record.  
	if (purchparmtable.PurchId)  
	{   
		purchparmtable.PurchName = "";   
	}
}

Keep learning!!

Tuesday, June 6, 2023

Opening Journal lines From a custom form in D365FO using X++

 Opening Journal lines From a custom form in D365FO using X++

//Clicked method of custom form control
Public void clicked()
{
   FormDataSource          FormDataSource; 
   Dax_LedgerJournalTable  daxLedgerJournalTable; 
   LedgerJournalTable      LedgerJournalTablenew; 
   
   super(); 

   FormRun formrunrecord,formRun; 
   formrunrecord = this.formRun() ; 
   FormDataSource = formrunrecord.dataSource("Dax_LedgerJournalTable"); 
   daxLedgerJournalTable = FormDataSource.cursor() ; 
   LedgerJournalTablenew = LedgerJournalTable::find(daxLedgerJournalTable.JournalNum); 
   Args args; 
   
  // object formRun; 
   JournalFormTable journalForm; 
   
   //Open form 
   args = new Args(); 
   args.name(formStr(LedgerJournalTable)); 
   LedgerJournalTable = LedgerJournalTable::find(daxLedgerJournalTable.JournalNum); 
   args.record(LedgerJournalTable);  
   formRun = classfactory.formRunClass(args); 
   formRun.init(); 
   formRun.run(); 

   args.caller(formRun); 
   journalForm = JournalForm::fromArgs(args); 
   if (journalForm && journalForm.journalTableData().journalTable().JournalId ==LedgerJournalTable.JournalNum) 
   {		
	 journalForm.openLinesForm(); 
   }
   formRun.close(); 
 }

We can achieve this using in below way as well. To check out Click here

Keep learning!!