Friday, January 17, 2025

Restrict the user to open the particular Form, if the same form already opened by any other user.

 

Restrict the user to open the particular Form, if the same form already opened by any other user.

In my scenario when I open particular SalesOrder i want to restrict to others by throwing an error “The same Salesorder is already opened by any other user”


1. Firstly add one field in SalesTable. 
2. Navigation  AOT -> Tables -> SalesTable.
3.Create new field  userId Datatype is string.

Navigate to Forms\SalesTable\Methods\init, write the following code in it.
public void init()
{
 
         SalesTable    salestablecpy ;
         FormRun       callerForm;

        salestablecpy  = element.args().record();
        callerForm       = element.args().caller();

        If (!salestablecpy.UserId)
        {
            ttsBegin;

                 salestablecpy.UserId = curUserId();
                  salestablecpy.doUpdate();
            ttsCommit;
        
        }
       else if(salestablecpy.UserId != curUserId())
       {
      
            throw error(strFmt("The same Salesorder(%1) is already opened by %2",                                                                 salestablecpy.SalesId,salestablecpy.UserId));

       }
}

. Similarly, navigate to  Forms\SalesTable\Methods\Close,  write the following code in it.




void  close()
{  
        SalesTable    salestablecpy, saleesTableLoc  ;
        formrun      formrun ;

       salestablecpy = element.args().record();

       formrun = element.args().caller();

    if (salestablecpy.UserId)
    {
        ttsBegin;

           select forUpdate saleesTableLoc  where saleesTableLoc.RecId==salestablecpy.RecId;
           saleesTableLoc.UserId = ' ';
           saleesTableLoc.doUpdate();

      ttsCommit;
   
    }

}

Capturing Infolog messages in D365FO using X++.

      Capturing info log messages in D365FO. We can get it in multiple ways.      Way 1.        public static str getError() {     SysInfol...