Tuesday, May 7, 2019

Batch job: Exporting data into excel


Contract class:
[
DataContractAttribute,
SysOperationContractProcessingAttribute(classstr(mfiInventWHSSyncUiBuilder))
]
class mfiInventWHSSyncContract implements SysOperationValidatable
{
    str         packedQuery;
    NoYesId     syncWarehouse;
    List        wareHouse;
}

public Query getQuery()
{
    return new Query(SysOperationHelper::base64Decode(packedQuery));
}

[
DataMemberAttribute,
AifQueryTypeAttribute('_packedQuery', queryStr(mfiInventOnHand))
]
Public str parmQuery(str  _packedQuery = packedQuery )
{
    packedQuery =  _packedQuery;

    return packedQuery;
}

[
DataMemberAttribute,
SysOperationLabelAttribute("@MFI6182")
]
public NoYesId parmSyncWarehouse(NoYesid  _syncWarehouse = syncWarehouse)
{
    syncWarehouse = _syncWarehouse;
    return syncWarehouse;
}

[
DataMemberAttribute,
SysOperationLabelAttribute("@SYS6437"),
AifCollectionTypeAttribute("@MFI6178", Types::String)
]
public List parmWareHouse(List  _wareHouse = wareHouse)
{
    wareHouse = _wareHouse;
    return wareHouse;
}

public void setQuery(Query _query)
{
    packedQuery = SysOperationHelper::base64Encode(_query.pack());
}



/// <summary>
///    Validates the SSRS report parameters.
/// </summary>
/// <returns>
///    true if successful; otherwise, false.
/// </returns>
public boolean validate()
{
    boolean ret = true;
    List                        wareHouseList;
    ListIterator                wareHouseListIterator;
    InventLocation              inventLocation;

    if (syncWarehouse == NoYes::No && wareHouse !=null)
    {
       ret = checkFailed("@MFI6179");
    }

    wareHouseListIterator  = new ListIterator(this.parmWareHouse());

    wareHouseList = this.parmWareHouse();

    while(wareHouseListIterator.more())
    {
        select InventLocationId,mfiSyncToD365 from inventLocation
            where inventLocation.InventLocationId == wareHouseListIterator.value();
        if (inventLocation.mfiSyncToD365 == NoYes::No)
        {
         ret = checkFailed(strFmt("@MFI6181",inventLocation.InventLocationId));
        }
        wareHouseListIterator.next();
    }
    return ret;
}

Service class:
class mfiInventWHSSyncService extends SysOperationServiceBase
{

}

[SysEntryPointAttribute]
Public void processOperation(mfiInventWHSSyncContract   _contract)
{
    MFIInventCountProcessData   inventCount;
    InventLocation              inventLocation;
    NoYesId                     synWareHouse;
    List                        wareHouseList;
    ListIterator                wareHouseListIterator;
    AxdSendContext              axdSendContext      = AxdSendContext::construct();
    AifEntityKey                aifEntityKey        = AifEntityKey::construct();
    AifConstraintList           aifConstraintList   = new AifConstraintList();
    AifConstraint               aifConstraint       = new AifConstraint();
    AifEndpointList             endpointList;
    AifActionId                 actionId;
    Query                       query,aifQuery;
    QueryBuildDataSource        qbdsInventWHS,qbdsInventCount;
    QueryRun                    queryRun;

    wareHouseListIterator  = new ListIterator(_contract.parmWareHouse());

    synWareHouse   = _contract.parmSyncWarehouse();
    wareHouseList  = _contract.parmWareHouse();

    query       = new Query(_contract.getQuery());

    qbdsInventWHS = query.dataSourceTable(tableNum(MFIInventCountProcessData));

    if (wareHouseList != null && synWareHouse ==NoYes::No)
    {
        while(wareHouseListIterator.more())
        {
           qbdsInventWHS.addRange(fieldNum(MFIInventCountProcessData,InventLocationId)).value(wareHouseListIterator.value());
           wareHouseListIterator.next();
        }
    }
    if (synWareHouse == NoYes::Yes)
    {
        while select RecId,InventLocationId from inventLocation
            where inventLocation.mfiSyncToD365 == NoYes::Yes
        {
            qbdsInventWHS.addRange(fieldNum(MFIInventCountProcessData,InventLocationId)).value(queryValue(inventLocation.InventLocationId));
        }
    }

    queryRun       = new QueryRun(query);

    while(queryRun.next())
    {
        inventCount      = queryRun.get(tableNum(MFIInventCountProcessData));

        inventLocation   = InventLocation::find(inventCount.InventLocationId);

        if (inventLocation.mfiSyncToD365 == NoYes::Yes)
        {
             aifQuery                   = new Query(queryStr(mfiInventOnHand));

             qbdsInventCount =query.dataSourceTable(TableNum(MFIInventCountProcessData));
             qbdsInventCount.addRange(FieldNum(MFIInventCountProcessData,RecId)).value(queryvalue(inventCount.RecId));

             actionId = AifSendService::getDefaultSendAction(classnum(mfiInventOnHandService), AifSendActionType::SendByQuery);
             aifConstraint.parmType(AifConstraintType::NoConstraint);
             aifConstraintList.addConstraint(aifConstraint) ;
             endpointList = AifSendService::getEligibleEndpoints(actionId, aifConstraintList);

             AifSendService::SubmitFromQuery(actionId, endpointList, query, AifSendMode::Async);

        }


    }
}


UI Builder class:
class mfiInventWHSSyncUiBuilder extends SysOperationAutomaticUIBuilder
{
    DialogField     dialogWareHouse;
    DialogField     wareHouseCheckBox;
}

public void build()
{
    mfiInventWHSSyncContract   contract;
    DialogGroup                dlgGrp;

    Dialog      dlg    = this.dialog();

    contract           = this.dataContractObject() as mfiInventWHSSyncContract;

    dlgGrp = dlg.addGroup('');
    dlgGrp.columns(1);
    dlgGrp.addFieldname(identifierStr(wareHouseCheckBox));
    wareHouseCheckBox   = this.addDialogField(methodStr(mfiInventWHSSyncContract,parmSyncWarehouse),contract);

    dlgGrp = dlg.addGroup('Inventory Dimensions');
    dlgGrp.columns(1);
    dlgGrp.addFieldname(identifierStr(dialogWareHouse));
    dialogWareHouse     = this.addDialogField(methodStr(mfiInventWHSSyncContract,parmWareHouse),contract);

}

public boolean modifyChecBox(FormCheckBoxControl _checkBoxControl)
{
    dialogWareHouse.enabled(!_checkBoxControl.checked());
    return true;
}

public void postBuild()
{
    mfiInventWHSSyncContract   contract;

    super();

    dialogWareHouse.enabled(any2enum(!wareHouseCheckBox.value()));

    contract = this.dataContractObject() as mfiInventWHSSyncContract;

    wareHouseCheckBox = this.bindInfo().getDialogField(contract,methodStr(mfiInventWHSSyncContract,parmSyncWarehouse));
    dialogWareHouse   = this.bindInfo().getDialogField(contract,methodStr(mfiInventWHSSyncContract,parmWareHouse));

    dialogWareHouse.registerOverrideMethod(methodStr(FormStringControl, lookup),methodStr(mfiInventWHSSyncUiBuilder,wareHouseLookup),this);
    wareHouseCheckBox.registerOverrideMethod(methodstr(FormCheckBoxControl, modified), methodstr(mfiInventWHSSyncUiBuilder,modifyChecBox), this);

}


private void wareHouseLookup(FormStringControl _control)
{
    Query       query;
    container   conWareHouse;

    query = new Query(queryStr(InventLocationSRS));

    SysLookupMultiSelectGrid::lookup(query,_control,_control,conWareHouse);
}

Controller class:
[SysOperationJournaledParametersAttribute(true)]
class mfiInventWHSSyncController extends SysOperationServiceController
{
}

public ClassDescription caption()
{
    return "@MFI6176";
}

public void new()
{
    super(classStr(mfiInventWHSSyncService),methodStr(mfiInventWHSSyncService,processOperation),SysOperationExecutionMode::ReliableAsynchronous);

    this.parmDialogCaption("@MFI6177");
}

// Here goes a description of the class
static ClassDescription description()
{
    return "D365 Inventory Sync Job";
}

public static void main(Args _args)
{
    mfiInventWHSSyncController     controller;

    controller = new mfiInventWHSSyncController();
    controller.startOperation();
}

No comments:

Post a Comment