Tuesday, 13 March 2018

How to export Data into the CSV File with Cross Company

In this post we will go through the exporting the data into the CSV file using X++.

For this you can use a class or Job,

public static void main(Args _args)
{
   // take the commaio class used for CSV formate
    Commaio                     file;
    container                   line,companies; // to store the data need to exported and company information
    InventTable                 inventTable;
    ItemGroupId                 itemGroupId;
    Name                        itemGroupName;
    ItemName                    itemName;
    InventModelGroupItem        invModGrp;
    str                         invmodeGroupId,filename,strdatetime;
    date                        currentDate = today();

    str                         product_class,product_class_desc,master_product,master_product_desc,product_name;
    str                         product_name_desc,product_desc,product_id,search_name,hazard,pricing_strategy,packaging_group;
    str                         weight,net_weight,purchase_stopped,sales_stopped,inventory_stopped,rckpit,item_model_number;

    int                         row; // for how many records exported
    #File
    ;
    strdatetime = date2Str(currentDate,321,DateDay::Digits2,DateSeparator::Hyphen,DateMonth::Digits2,DateSeparator::Hyphen,DateYear::Digits4); //  for current date
    filename = "G:\\PRODUCT_CSV_"+ strdatetime +".csv"; // file name
    file = new Commaio(filename , #io_write); // file permissions
    //file.outFieldDelimiter(';'); //delimeter
    if( !file || file.status() != IO_Status::Ok) // checking the file
    {
        throw error("File Cannot be opened");
    }
    companies = ["230","320"];
    while select crossCompany:companies * from inventTable
    {
        invModGrp = InventModelGroupItem::findByItemIdLegalEntity(inventTable.ItemId,                          inventTable.dataAreaId,false);
        invmodeGroupId = invModGrp.ModelGroupId;
            if((invmodeGroupId == "FIN") || (invmodeGroupId == "RAW"))
            {
                product_class = (inventTable.itemGroupId()?inventTable.itemGroup().ItemGroupId:'');
                product_class_desc = (inventTable.itemGroupId()?inventTable.itemGroup().Name:'')?                          (inventTable.itemGroupId()?inventTable.itemGroup().Name:''):'NA';
                master_product = 'MP-'+inventTable.RES_MasterProduct;
                master_product_desc = inventTable.RES_MasterProduct;
                itemName = inventTable.itemName();
                line = [product_class,product_class_desc,master_product,master_product_desc];
                file.writeExp(line);
                row++;
            }
    }
    info(strFmt("Completed, total number of rows exported:  %1",row));
}

No comments:

Post a Comment