Search This Blog

Tuesday, July 24, 2012

Import Ax2012 Routes from CSV

We are importing route  from CSV.
We are dealing with the following tables for importing routes.

 RouteTable; RouteVersion,  Route,   RouteOpr .

For any item there must be only one RouteVersion record per item.So before importing we have to find whether routeversion exists or not.

There is 1:1 relation between RouteTable and RouteVersion records during the import.

If the RouteVersion record exists, then the RouteTable record must exist as well and we should not be created.

Class declaration


class krishh_ImportRoute 
{
    //Define your csv file column name with positions
    #define.RouteTable_Name_ColumnNum(1)
    #define.RouteVersion_ItemId_ColumnNum(2)
    #define.Route_OprId_ColumnNum(3)
    #define.Route_OprNum_ColumnNum(4)
    #define.Route_OprNumNext_ColumnNum(5)
    #define.RouteOpr_ProcessTime_ColumnNum(6)
    #define.RouteOpr_RouteGroupId_ColumnNum(7)
    #define.RouteOpr_RouteType_ColumnNum(8)
    #define.RouteOpr_SetupTime_ColumnNum(9)
    #define.RouteOpr_ToHours_ColumnNum(10)
    #define.RouteOpr_WrkCtrIdCost_ColumnNum(11)
    #define.WrkCtrActivityResourceRequirement_WrkCtrId_ColumnNum(12)

    RouteTable      routeTable;
    RouteVersion    routeVersion;
    Route           route;
    RouteOpr        routeOpr;
    ItemId          itemIdRouteVersion;
    NumberSeq       numberSeq;
}
// execute the route imports here container is the values that we read from CSV file.

public void run(container _item)
{
    ttsBegin;
    this.initRouteVersion(_item);
    this.createRouteTable(_item);
    this.createRoute(_item);
    this.createRouteOpr(_item);
    this.createWrkCtrActivity(_item);
    ttsCommit;
}
protected void new()
{
    ;
    numberSeq       =   RouteTable::numberSeq(false,true);
}

protected void initRouteVersion(container _conLine)
{
    itemIdRouteVersion  =   conPeek(_conLine,
                                              #RouteVersion_ItemId_ColumnNum);

    // There must be only one RouteVersion record per item
    select firstOnly routeVersion
        where routeVersion.ItemId == itemIdRouteVersion;
}
protected void createRouteTable(container _conLine)
{
    ;
    if(routeVersion.RecId)
    {
        routeTable  =   RouteTable::find(routeVersion.RouteId);
        // There is always 1:1 relation betwean RouteTable and RouteVersion records during the import.
        // If the RouteVersion record exists, then the RouteTable record must exist as well and should not be created.
        return;
    }
    // If it is a new route, then update the last one.
    if(routeTable.RecId     &&
       !routeVersion.RecId)
    {
        this.updateLastRoute();
    }
    routeTable.clear();
    routeTable.initValue();
    routeTable.CheckRoute   =   NoYes::Yes;
    // Approved
    routeTable.Approved     =   NoYes::Yes;
    // Approver
    routeTable.Approver     =   "krishh"
    // Name
    routeTable.Name         =   conPeek(_conLine,
                                                  #RouteTable_Name_ColumnNum);
    // RouteId
    routeTable.RouteId      =   numberSeq.num();
    routeTable.insert();
}
protected void createRouteVersion(container _conLine)
{
    InventDim           inventDim;

    if(routeVersion.RecId)
    {
        return;
    }
    routeVersion.clear();
    routeVersion.initValue();
    // Active
    routeVersion.Active         =   NoYes::Yes;
    // Approved
    routeVersion.Approved       =   NoYes::Yes;
    // Approver
    routeVersion.Approver       =   "Krishh";
    // FromDate
    routeVersion.FromDate       =   dateNull();
    // ItemId
    routeVersion.ItemId         =   itemIdRouteVersion;
    // RouteId
    routeVersion.RouteId        =   routeTable.RouteId;
    // InventDimId
    inventDim.clear();
    inventDim.InventSiteId      =   InventTable::find(routeVersion.ItemId).inventInventSiteId();
    routeVersion.InventDimId    =   InventDim::findOrCreate(inventDim).inventDimId;
    routeVersion.insert();
}


protected void createRoute(container _conLine)
{
   Route routeFieldList;
   ;
   route.clear();
   route.initValue();
   route.RouteId = routeTable.RouteId;
   route.OprId =Conpeek(_conLine,#Route_OprId_ColumnNum);
   route.OprNum = Conpeek(_conLine,#Route_OprNum_ColumnNum);
   route.OprNumNext = Conpeek(_conLine,#Route_OprNumNext_ColumnNum);

   select RecId from routeFieldList where routeFieldList.RouteId == route.RouteId &&                routeFieldList.OprNum == route.OprNum && routeFieldList.OprPriority == route.OprPriority;

        if(routeFieldList.RecId)         {

              // Do not create if it already exists.
                     return;          }
   route.insert();
}


protected void createRouteOpr(container _conLine)
{
    ;
    routeOpr.clear();

    routeOpr.initValue();

    routeOpr.WrkCtrIdCost   =  Conpeek(_conLine,
                                                  #RouteOpr_WrkCtrIdCost_ColumnNum);

    routeOpr.initFromWrkCtrId(routeOpr.WrkCtrIdCost);
    routeOpr.ProcessTime    =   str2num( Conpeek (_conLine,
                                                  #RouteOpr_ProcessTime_ColumnNum));
    routeOpr.RouteGroupId   =    Conpeek (_conLine,
                                                  #RouteOpr_RouteGroupId_ColumnNum);
    routeOpr.RouteType      =    Conpeek (_conLine,
                                                  #RouteOpr_RouteType_ColumnNum) == "Standard" ?
    RouteOprType::Standard : RouteOprType::Vendor;
    routeOpr.SetupTime      =   str2num( Conpeek (_conLine
                                                                          , #RouteOpr_SetupTime_ColumnNum));
    routeOpr.ToHours        =   Conpeek (_conLine,
                                                  #RouteOpr_ToHours_ColumnNum);

    routeOpr.OprId          =   route.OprId;

    routeOpr.ItemCode       =   TableGroupAll::Table;
    routeOpr.ItemRelation   =   routeVersion.ItemId;
    routeOpr.RouteCode      =   RouteAll::Route;
    routeOpr.RouteRelation  =   route.RouteId;
    routeOpr.SiteId         =   routeVersion.inventSiteId();
    if(RouteOpr::exist(routeOpr.OprId,
                       routeOpr.ItemCode,
                       routeOpr.ItemRelation,
                       routeOpr.ConfigId,
                       routeOpr.RouteCode,
                       routeOpr.RouteRelation,
                       routeOpr.SiteId))
    {
        return;
    }
    routeOpr.insert();
}


protected void createWrkCtrActivity(container _conLine)
{
    WrkCtrActivityRequirement           wrkCtrActivityRequirement;
    WrkCtrActivityResourceRequirement   wrkCtrActivityResourceRequirement;
    ;
    wrkCtrActivityRequirement.clear();

    wrkCtrActivityRequirement.ActivityRequirementSet    =   routeOpr.activityRequirementSet().RecId;

    wrkCtrActivityRequirement.RelationshipType          =   WrkCtrActivityRequirementType::Resource;
    wrkCtrActivityRequirement.UsedForJobScheduling      =   NoYes::Yes;

    wrkCtrActivityRequirement.insert();


    wrkCtrActivityResourceRequirement.clear();

    wrkCtrActivityResourceRequirement.setActivityRequirement(wrkCtrActivityRequirement.RecId);

    wrkCtrActivityResourceRequirement.ResourceDataAreaId    =   curExt2dataareaid(tableNum(WrkCtrTable));
    wrkCtrActivityResourceRequirement.WrkCtrId              =  Conpeek(_conLine,
                                                                                  #WrkCtrActivityResourceRequirement_WrkCtrId_ColumnNum);

    wrkCtrActivityResourceRequirement.insert();
}


protected void updateLastRoute()
{
    if(routeTable.RecId)
    {
        routeTable.updateRoute(false);
    }
}










2 comments:

  1. Hi Krishna, I'm new in AX and i need migrate routes,but i see that you duplicate the method "createRouteVersion" instead of "createRoute". You can fix the class and reply to me.
    I must add a method to load the csv to container?

    Thanks a lot
    Oscar Oliveros

    ReplyDelete
  2. Hi again Krishna, the method "getLineValue" is missing too.

    Thanks
    Oscar Oliveros

    ReplyDelete

Thanks for visiting my blog,
I will reply for your comment within 48 hours.

Thanks,
krishna.