Monday, December 24, 2018

Update multiple selected records on form in AX 2012 through Class and Passing parameter from Form to Class

Passing parameter through Args in AX is very easy important functionality. To demonstrate this i used one form and added one button to call class.
Below are the artefacts:

Table:pocVehicle
Class:POCVehicleMasterClass
ManuItem:POCVehicleMasterClass

Method on Form button click:
void clicked()
{

MenuFunction mf;
  //POCVehicle pocVehicle;

Args args=new Args();
;
args.record(pocVehicle);
mf= new MenuFunction(identifierStr(POCVehicleMasterClass), MenuItemType::Action);
mf.run(args);


Main Methods on Class POCVehicleMasterClass :


Public static void main(Args _args)

{
POCVehicle pocVehicle;

POCVehicleMasterClass myClass;

FormDataSource formdatasource;

Common common;
;

myClass= new POCVehicleMasterClass();

if(_args.record().TableId==tableNum(POCVehicle))

{
//Buffer selected records
common=_args.record();
//formdatasource=_args.record().dataSource();
//Records data source
formdatasource=common.dataSource();
//Passing parameter to class updateRecord method
myClass.UpdateRecord(formdatasource,_args);
}

}


Second method on the class:

Public void UpdateRecord(FormDataSource datasource, Args args)
{

POCVehicle pocvehicle;
FormDataSource formDataSource;
int recordCount, recorUpdated;
;

formDataSource= datasource;
//Count selected records
RecordCount= formDataSource.recordsMarked().lastIndex();
recorUpdated=0;

//Update all selected records
for(pocvehicle=formDataSource.getFirst(true)? formDataSource.getFirst(true): formDataSource.cursor() ; pocvehicle ;pocvehicle=formDataSource.getNext())

{
ttsBegin;
pocvehicle.Insurence= NoYes::No;
pocvehicle.update();
ttsCommit;
recorUpdated++;

}
//Refresh caller form datasource with same pointer

formDataSource.research(true);

info(strFmt('%1 records selected and %2 records updated ', RecordCount,recorUpdated));
  
}

No comments:

Post a Comment