Thursday 20 July 2023

CRUD Operation in X++

 Insert and Select Statement



static void insert(Args _args)
{

    AttEmployeeTable     attEmployeeTable;  ;       //declaring a table buffer

    

    attEmployeeTable.EmployeeId = "Emp100";

    attEmployeeTable.Name = "Rahul";

    attEmployeeTable.Phone = "9897888821";

    

    attEmployeeTable.insert(); // inbuilt

    info("Record has been inserted");

}

 

static void bulkInserts(Args _args)
{

    ATTEmployeeTable    attEmployeeTable;    

    for ( int i = 1 ; i <=100; i++)

    {

        attEmployeeTable.AcccountNumber = "Emp" + int2str(i);

        attEmployeeTable.Name = "Rahul";

        attEmployeeTable.Phone = "9897888821";

    

        attEmployeeTable.insert(); // inbuilt      

    }

    info("Record has been inserted");

}



static void LearnSelectStatements(Args _args)

{

    AttEmployeeTable  attEmployeeTable;    

    Select * from attEmployeeTable;    

    info(attEmployeeTable.EmployeeId);

    

    // Select latest record

    select * from attEmployeeTable order by RecId desc;    

    info(attEmployeeTable.EmployeeId);

    

    // select a particular record    

    select * from attEmployeeTable where attEmployeeTable.EmployeeId == 'SIS99';

    info(attEmployeeTable.EmployeeId);

    

    // MULTIPLE RECORDS FETCHING - while select      

    while select * from attEmployeeTable where attEmployeeTable.Gender == Gender::Male

    {

        info(attEmployeeTable.EmployeeId);

    }

  

    // fieldslist  --  select the fields which u want to display or use further    

    select Name from attEmployeeTable where attEmployeeTable.EmployeeId == 'Emp99';

    info(attEmployeeTable.EmployeeId);    

    //infolog.clear();      

}





Update and Delete Statement


static void UpdateQuery(Args _args)
{

    CustTable custTable;    

    ttsBegin;

    select forupdate custTable where custTable.AcccountNumber == 'ATT100';

    if (custTable)

    {

        custTable.Name = "New name";

        custTable.update();

    }

    ttsCommit; //ttsabort

}



static void UpdateQueryMultiple(Args _args)
{

    CustTable custTable;    

    ttsBegin;

    while select forupdate custTable //where custTable.AcccountNumber == 'ATT100';

    {

        custTable.Name = "New name";

        custTable.update();

    }

    ttsCommit; //ttsabort

 

    // roundtrips to sql is no of records + 1

    // performance improvement - update_recordset

    

    update_recordset custTable setting Name = "New Name again";

    // you can even add where clause

}



static void deleteQuery(Args _args)

{

    SISCustTable custTable;    

    ttsBegin;

    while select forupdate custTable //where custTable.AcccountNumber == 'ATT100'

    {

        custTable.delete();

    }

    ttsCommit; //ttsabort

    

    // roundtrips to sql is no of records + 1

    // performance improvement - delete_from

    

    delete_from custTable;

        // you can even add where clause

}



 


No comments:

Post a Comment

Data Entities in D365 FnO

  Data management - Data entities ·        Data entity is a conceptual abstraction and encapsulation of one of more underlying tables. ...