Skip to main content

Notifications

Finance | Project Operations, Human Resources, ...
Answered

To remove record using button

(0) ShareShare
ReportReport
Posted on by 1,479
I have created the button on my form which gets enable disable on certain condition here i want to delete the selected record when the button is clicked the enable/disable condition on button is working fine but how can i remove the selected record can any one guide me on this , my code is below and the button i am using is not command button 
[ExtensionOf(formDataSourceStr(TrvExpenses,TrvExpTrans))]final class DTTrvExpenses_TrvExpTrans_Extension{    [Control(/Button/)]    class DeleteExpenceLines    {        /// <summary>        /// Remove the selected expenses from the expense report        /// </summary>        public void clicked()        {            // Invoke method to process expense line deletion.            // This method should be invoked for deletion from both details view and grid view.            // There is no special processing for deletion from grid view.            this.MyMethodToDeleteLines();            super();        }    }}
 
  • Verified answer
    Mohit Rampal Profile Picture
    Mohit Rampal 12,552 Super User 2024 Season 1 on at
    To remove record using button
    You don't need to extend the private method. Either By pass the standard validation or as Girish mentioned, try updating the status to None, so that it passes the validation and then Delete that record.
  • Verified answer
    GirishS Profile Picture
    GirishS 27,821 Super User 2024 Season 1 on at
    To remove record using button
    Instead of bypassing the validation which is not good practice - Just update the status to Draft in the clicked method and then delete the line.
     
    Thanks,
    Girish S.
  • Dineshkarlekar Profile Picture
    Dineshkarlekar 1,479 on at
    To remove record using button
    hi 
    Mohit Rampal,
     
    Thanks for reply ,
    if i cannot extend the protected method the is there any way to put my validation in COC and get the line deleted.
     
    Thanks,
     
    Regards,
     
    Dinesh
  • Verified answer
    Mohit Rampal Profile Picture
    Mohit Rampal 12,552 Super User 2024 Season 1 on at
    To remove record using button
    It is basically same validation I have told before and provided code to by pass validation. 'canExpenseLineBeDeletedWithApprovalStatus' is private method so you can not extend it to add one more condition.
     
  • Verified answer
    GirishS Profile Picture
    GirishS 27,821 Super User 2024 Season 1 on at
    To remove record using button
    There is no need to pass the buffer from clicked event handler to COC of doDelete method.
    Once you call doDelete method at clicked method it will call the standard doDelete method and your extension code should call.
     
    Thanks,
    Girish S.
  • Dineshkarlekar Profile Picture
    Dineshkarlekar 1,479 on at
    To remove record using button
    hi 
     
    Girish s
    if i passed dodelete() method there i have to create the extension of it on tabel level because do delete method is having some validataion i need to add my validation there so how can i pass the current record from click event handler to this can u please guide me on it.
    [ExtensionOf(tableStr(TrvExpTrans))]
    final class DTTrvExpTrans_Extension
    {
        public void doDelete()
        {
            next doDelete();
            if(!this.canExpenseLineBeDeleted())
            {
                return;
            }
    
        }
    
        /// <summary>
        /// List of approval Status for which expense line can be deleted
        /// </summary>
        /// <returns>
        ///     true if we can delete , false if not allowed
        /// </returns>
       Public boolean canExpenseLineBeDeleted()
        {
            TrvExpTable                     trvExpTable;
    
            if( trvExpTable.ApprovalStatus == TrvAppStatus::Pending)
            {
                return true;
            }
    
            return false;
        }
    }
     
     
  • Verified answer
    GirishS Profile Picture
    GirishS 27,821 Super User 2024 Season 1 on at
    To remove record using button
    In that case just call the doDelete method inside the clicked method instead of delete method.
     
    [FormControlEventHandler(formControlStr(TrvExpenses, DeleteExpenceLines), FormControlEventType::Clicked)]
        public static void DeleteExpenceLines_OnClicked(FormControl sender, FormControlEventArgs e)
        {
            FormButtonControl callerButton  = sender as FormButtonControl;
            FormRun                  form   = callerButton.formRun();
            FormDataSource   trvExpTrans_ds = form.dataSource(formDataSourceStr(TrvExpenses,TrvExpTrans)) as FormDataSource; // datasource name where the records to be deleted.
    
            //Multiselect helper class
    
            TrvExpTrans          _trvExpTrans ;//records to delete from
    
            MultiSelectionHelper  helper =  MultiSelectionHelper::construct();
            helper.parmDatasource( trvExpTrans_ds);
            _trvExpTrans  = helper.getFirst();
       
            while ( _trvExpTrans.RecId != 0)
            {
                if(_trvExpTrans.ApprovalStatus == TrvAppStatus::Pending)
                {
                    _trvExpTrans.doDelete();
    
                }
                _trvExpTrans = helper.getNext();
            }
    
        }
     
    Thanks,
    Girish S.
  • Dineshkarlekar Profile Picture
    Dineshkarlekar 1,479 on at
    To remove record using button
    hi
    Girishs,
    Mohit Rampal,
    I cannot use delete method , it is having some other validations , i need to do it with dodelete() i have created the extension of table overide delete method but i need to pass the record from my clicked() event so my record will get deleted , can you pls tell me how can i pass current record from clicked eventhandler to dodeleted method so i get my record deleted.
    [ExtensionOf(tableStr(TrvExpTrans))]
    final class DTTrvExpTrans_Extension
    {
        public void doDelete()
        {
            next doDelete();
            if(!this.canExpenseLineBeDeleted())
            {
                return;
            }
    
        }
    
        /// <summary>
        /// List of approval Status for which expense line can be deleted
        /// </summary>
        /// <returns>
        ///     true if we can delete , false if not allowed
        /// </returns>
       Public boolean canExpenseLineBeDeleted()
        {
            TrvExpTable                     trvExpTable;
    
            if( trvExpTable.ApprovalStatus == TrvAppStatus::Pending)
            {
                return true;
            }
    
            return false;
        }
    }
    
     
  • Verified answer
    Mohit Rampal Profile Picture
    Mohit Rampal 12,552 Super User 2024 Season 1 on at
    To remove record using button
    If the feature to validate approval status is enabled in your environment then also Delete method will call the method DoDelete is calling to check approval status and your condition of pending status will fail.
  • Verified answer
    GirishS Profile Picture
    GirishS 27,821 Super User 2024 Season 1 on at
    To remove record using button
    Calling doDelete method is not best practice. It will skip some basic validations. To make it simple you can directly add the validation inside the clicked method. Refer to the below code and check whether it works.
    [FormControlEventHandler(formControlStr(TrvExpenses, DeleteExpenceLines), FormControlEventType::Clicked)]
        public static void DeleteExpenceLines_OnClicked(FormControl sender, FormControlEventArgs e)
        {
            FormButtonControl callerButton  = sender as FormButtonControl;
            FormRun                  form   = callerButton.formRun();
            FormDataSource   trvExpTrans_ds = form.dataSource(formDataSourceStr(TrvExpenses,TrvExpTrans)) as FormDataSource; // datasource name where the records to be deleted.
    
            //Multiselect helper class
    
            TrvExpTrans          _trvExpTrans ;//records to delete from
    
            MultiSelectionHelper  helper =  MultiSelectionHelper::construct();
            helper.parmDatasource( trvExpTrans_ds);
            _trvExpTrans  = helper.getFirst();
       
            while ( _trvExpTrans.RecId != 0)
            {
                if(_trvExpTrans..ApprovalStatus == TrvAppStatus::Pending)
                {
                    _trvExpTrans.delete();
    
                }
                _trvExpTrans = helper.getNext();
            }
    
        }

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,965 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,836 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans