79) How to create a batch job In D365 fO

  I had a scenario where I need to createBatch job in D 365 fO


Step 1 - create a contract class for the parameter 


/// <summary>

/// The <c>EntAssetMaintenancePlanObjectsContract</c> class is used as a data contract to pass maintenance sequence calculation parameters to and from services.

/// </summary>

[

    DataContractAttribute,

    SysOperationGroupAttribute('WOGroup',literalstr("@EnterpriseAssetManagementAppSuite:EntAsset7"),'2')

]

public final class AvaEntAssetMaintenancePlanObjectsContract implements SysOperationValidatable, SysOperationInitializable

{

    str packedQuery;

    EntAssetMaintenancePlanInterval     interval;

    DayWeekMonthQuarterYear intervalType;

    EntAssetMaintenancePlanAutoCreate   autoCreateWO;

    AvaMaintenanceProgramRecId          maintenanceProgramRecId;

    EntAssetMaintenancePlanRecId        maintenancePlanRecId;

    /// <summary>

    /// Decodes and returns a query encoded as a base 64 string.

    /// </summary>

    /// <returns>A new query decoded from a base 64 encoded string.</returns>

    public Query getQuery()

    {

        return new Query(SysOperationHelper::base64Decode(packedQuery));

    }


    /// <summary>

    /// Initializes a new instance of the <c>EntAssetMaintenancePlanObjectsContract</c> class.

    /// </summary>

    public void initialize()

    {

        this.parmInterval(1);

        this.parmIntervalType(DayWeekMonthQuarterYear::Year);

        this.parmAutoCreateWO(true);

    }


    [

        DataMemberAttribute('autoCreateWO'),

        SysOperationDisplayOrderAttribute('1'),

        SysOperationGroupMemberAttribute('WOGroup'),

        SysOperationLabelAttribute('Auto create if specified in the line'),

        SysOperationControlVisibilityAttribute(false)

    ]

    public EntAssetMaintenancePlanAutoCreate parmAutoCreateWO(EntAssetMaintenancePlanAutoCreate _autoCreateWO = autoCreateWO)

    {

        autoCreateWO = _autoCreateWO;

        return autoCreateWO;

    }


    [DataMemberAttribute('interval'),

        SysOperationDisplayOrderAttribute('2')]

    public EntAssetMaintenancePlanInterval parmInterval(EntAssetMaintenancePlanInterval _interval = interval)

    {

        interval = _interval;

        return interval;

    }


    [DataMemberAttribute('intervalType'),

        SysOperationDisplayOrderAttribute('1')]

    public DayWeekMonthQuarterYear parmIntervalType(DayWeekMonthQuarterYear _intervalType = intervalType)

    {

        intervalType = _intervalType;

        return intervalType;

    }


    [DataMemberAttribute('maintenancePlan'),

        SysOperationDisplayOrderAttribute('3'),

        SysOperationControlVisibilityAttribute(false)

    ]

    public EntAssetMaintenancePlanRecId parmMaintenancePlan(EntAssetMaintenancePlanRecId _maintenancePlanRecId = maintenancePlanRecId)

    {

        maintenancePlanRecId = _maintenancePlanRecId;

        return maintenancePlanRecId;

    }


    [DataMemberAttribute('maintenanceProgram'),

        SysOperationDisplayOrderAttribute('4'),

        SysOperationControlVisibilityAttribute(false)]

    public AvaMaintenanceProgramRecId parmMaintenanceProgram(AvaMaintenanceProgramRecId _maintenanceProgramRecId = maintenanceProgramRecId)

    {

        maintenanceProgramRecId = _maintenanceProgramRecId;

        return maintenanceProgramRecId;

    }


    [DataMemberAttribute,

        AifQueryTypeAttribute('_packedQuery', querystr(AvaEntAssetMaintenancePlanObjectsService))]

    public str parmQuery(str _packedQuery = packedQuery)

    {

        packedQuery = _packedQuery;

        return packedQuery;

    }


    /// <summary>

    /// Sets and encodes a given query as a base 64 encoded string.

    /// </summary>

    /// <param name = "_query">The query to be encoded as a base 64 encoded string.</param>

    public void setQuery(Query _query)

    {

        packedQuery = SysOperationHelper::base64Encode(_query.pack());

    }


    /// <summary>

    /// Validates the values specified for parameters.

    /// </summary>

    /// <returns>

    /// <c>True</c> if specified values are valid; else <c>False</c>.

    /// </returns>

    public boolean validate()

    {

        AvaEntAssetMaintenanceProgram maintenanceProgram;

        boolean ret = true;


        if (!this.maintenancePlanRecId)

        {

            ret = checkFailed(strFmt("@SYS50104", fieldPName(AvaEntAssetMaintenanceProgram, MaintenancePlan)));

        }


        maintenanceProgram = AvaEntAssetMaintenanceProgram::findRecId(this.maintenanceProgramRecId);


        if (!maintenanceProgram.Object)

        {

            ret = checkFailed(strFmt("@SYS50104", fieldPName(AvaEntAssetMaintenanceProgram, Object)));

        }


        return ret;

    }


}


========================================================================


Step 2 - Create a controller class 


/// <summary>

/// The <c>AvaEntAssetMaintenancePlanObjectsController</c> class is the controller class for preventive maintenance service for maintenance program.

/// </summary>

public final class AvaEntAssetMaintenancePlanObjectsController extends SysOperationServiceController

{

    


    public static void main(Args _args)

    {

        AvaEntAssetMaintenancePlanObjectsController maintenancePlanObjectsController;

        FormObjectSet   fos;

    

        maintenancePlanObjectsController = AvaEntAssetMaintenancePlanObjectsController::newFromArgs(_args);

        //maintenancePlanObjectsController.parmShowDialog(true);

        maintenancePlanObjectsController.startOperation();


        fos = FormDataUtil::getFormDataSource(_args.record());


        if (fos)

        {

            fos.research(true);

        }

    }


    /// <summary>

    /// Creates a new instance of the <c>AvaEntAssetMaintenancePlanObjectsController</c> class based on an instance of the <c>Args</c> class.

    /// </summary>

    /// <param name = "_args">An instance of the <c>Args</c> class.</param>

    /// <returns>A new instance of the <c>AvaEntAssetMaintenancePlanObjectsController</c> class.</returns>

    internal static AvaEntAssetMaintenancePlanObjectsController newFromArgs(Args _args)

    {

        AvaEntAssetMaintenancePlanObjectsController     maintenancePlanObjectsController;

        AvaEntAssetMaintenancePlanObjectsContract maintenancePlanObjectsContract;

    

        AvaEntAssetMaintenanceProgram     maintenanceProgram;

        Query     query;

    

        // create a new instance of the controller

        maintenancePlanObjectsController = new AvaEntAssetMaintenancePlanObjectsController();

    

        // initialize from args

        // one of the things this will do is read the "parameters" property from the menu item

        maintenancePlanObjectsController.initializeFromArgs(_args);

    

        // get data contract

        // the string should be the same as the parameter name!

        maintenancePlanObjectsContract = maintenancePlanObjectsController.getDataContractObject('_contract');

    

        // check if the record is of type EntAssetObjectTable

        if(_args && _args.dataset() == tableNum(AvaEntAssetMaintenanceProgram))

        {

            // cast record

            maintenanceProgram = _args.record() as AvaEntAssetMaintenanceProgram;

    

            if (!maintenanceProgram && _args.lookupTable() == tableNum(AvaEntAssetMaintenanceProgram))

            {

                maintenanceProgram = _args.lookupRecord() as AvaEntAssetMaintenanceProgram;

            }

            

            maintenancePlanObjectsContract.parmMaintenanceProgram(maintenanceProgram.RecId);

            maintenancePlanObjectsContract.parmMaintenancePlan(maintenanceProgram.MaintenancePlan);

        }

    

        // return a new instance of this controller

        return maintenancePlanObjectsController;

    }


}

======================================================================


Step 3 - Create a Service class to write a logic 


/// <summary>

/// The <c>AvaEntAssetMaintenancePlanObjectsService</c> class is used to create asset calendar lines based on maintenance sequence lines.

/// </summary>

public final class AvaEntAssetMaintenancePlanObjectsService

{

    EntAssetMaintenancePlanInterval     interval;

    DayWeekMonthQuarterYear intervalType;

    EntAssetMaintenancePlanAutoCreate   autoCreateWO;

    AvaMaintenanceProgramRecId          maintenanceProgramRecId;

    EntAssetMaintenancePlanRecId        maintenancePlanRecId;

    /// <summary>

    /// Calculates preventive maintenance for assets.

    /// </summary>

    /// <param name = "_contract">An instance of the <c>EntAssetMaintenancePlanObjectsContract</c> data contract.</param>

    public void calcObjects(AvaEntAssetMaintenancePlanObjectsContract _contract)

    {

        Query                                               query;

        QueryRun qr;

        SysOperationProgress operationProgress;

    

        Set objectSet = new Set(Types::Int64);

        EntAssetObjectMaintenancePlan objectMaintenancePlan;

        AvaEntAssetMaintenanceProgram                       maintenanceProgram;

        Set functionalLocationSet = new Set(Types::Int64);

    

        #macrolib.AviFiles

    

        setPrefix("@EnterpriseAssetManagementAppSuite:EntAsset402");


        query = _contract.getQuery();        

        interval = _contract.parmInterval();

        intervalType = _contract.parmIntervalType();

        autoCreateWO = _contract.parmAutoCreateWO();

        maintenancePlanRecId = _contract.parmMaintenancePlan();

        maintenanceProgramRecId = _contract.parmMaintenanceProgram();


        if (_contract.parmMaintenancePlan())

        {

            SysQuery::findOrCreateRange(query.dataSourceTable(tableNum(EntAssetMaintenancePlan)), fieldNum(EntAssetMaintenancePlan, RecId)).value(queryValue(_contract.parmMaintenancePlan()));

        }


        if (_contract.parmMaintenanceProgram())

        {

            maintenanceProgram = AvaEntAssetMaintenanceProgram::findRecId(_contract.parmMaintenanceProgram());


            if (maintenanceProgram.Object)

            {

                //insert assets to maintenance plan

                AvaEntAssetMaintenanceProgram::insertObjectMaintenancePlan(maintenanceProgram.Object, maintenancePlanRecId);


                //filer query only for asset selected in maintenance program

                SysQuery::findOrCreateRange(query.dataSourceTable(tableNum(EntAssetObjectTable)), fieldNum(EntAssetObjectTable, RecId)).value(queryValue(maintenanceProgram.Object));

            }

        }


        qr = new QueryRun(query);

    

        operationProgress = SysOperationProgress::newGeneral(#AviUpdate,

            "@EnterpriseAssetManagementAppSuite:EntAsset402",

            QueryRun::getQueryRowCount(qr.query(), maxInt()));

    

        while (qr.next())

        {

            operationProgress.incCount();

    

            if (qr.changed(tableNum(EntAssetObjectMaintenancePlan)))

            {

                objectMaintenancePlan = qr.get(tableNum(EntAssetObjectMaintenancePlan)) as EntAssetObjectMaintenancePlan;

    

                if (EntAssetObjectTable::findRecId(objectMaintenancePlan.Object).lifecycleState().ObjectActive)

                {

                    objectSet.add(objectMaintenancePlan.Object);

    

                    var planObjectCalc = EntAssetMaintenancePlanObjectCalculate::construct();

                    planObjectCalc.parmObjectMaintenancePlan(objectMaintenancePlan);

                    planObjectCalc.parmInterval(interval);

                    planObjectCalc.parmIntervalType(intervalType);

                    planObjectCalc.parmAutoCreateWO(autoCreateWO);

                    planObjectCalc.parmMaintenanceProgram(maintenanceProgramRecId);

                    planObjectCalc.run();

                }

            }   

            

        }

    

        info(strFmt("@EnterpriseAssetManagementAppSuite:EntAsset790", functionalLocationSet.elements(), objectSet.elements()));

    }


}


========================================================================


Step 4 :- Create a Action menu item and set the property 


Step 5 :-on the form just drag the menu item and set the data source 


=========================================================================



Comments

Popular posts from this blog

Customization on Sales invoice Report in D365 F&O

75) COC - Create a coc of the table modified method

46) D365 FO: SHAREPOINT FILE UPLOAD USING X++