0

I am writing a VF page where I have to show list records with Add/Delete button. when I click Delete (X) button it is deleting the record and removing that particular row. But if I am adding a row with clicking Add button and deleting the same row it is throwing error. It should delete that row which was added that moment. Added my code piece below.

public Pagereference removeRow() {  

    String SelectedRowId = ApexPages.CurrentPage().getParameters().get('rowid'); 

    for (Integer i = 0; i < subConfigurationList.size(); i++) {
        Sub_Task_Configuration__c a = subConfigurationList[i];
        if (a.Id == SelectedRowId) {
            delete a;
            subConfigurationList.remove(i);
            break;
        } 
    }

return null;

}

enter image description here

For 1st remove button as shown in pic it is working fine but where there is no record if I click remove(X) button it throwing error.

Error below:

Invalid id: Error is in expression '{!removeRow}' in page task_con: Class.Task_con.removeRow: line 63, column 1

An unexpected error has occurred. Your development organization has been notified.

Please Help me in this!

0

Change

delete a;

to

Database.delete(a, false);

Which will ignore errors instead of throwing an exception.

Please note that you should probably validate why the deletion failed, in case you need to surface an error to the user (e.g. it's okay if it failed because the record does not exist or was deleted, but not if there was a validation or security error).

5
  • Thank you for your Reply! I have tried this but still having the same error. There is no validation or security boundation.
    – SFDC 44
    Aug 19 '19 at 12:28
  • @RAHULSRIVASTAVA What is line 63 in your code?
    – sfdcfox
    Aug 19 '19 at 12:29
  • line 63 is --> if (a.Id == SelectedRowId) {
    – SFDC 44
    Aug 19 '19 at 12:31
  • @RAHULSRIVASTAVA my guess is that rowId is not an "Id" data type. Make sure that rowId is a valid Salesforce ID, or use another method, such as identifying by row index.
    – sfdcfox
    Aug 19 '19 at 13:00
  • I have put the validation on top of For Loop still it is coming inside the loop. system.debug('***'+SelectedRowId); if(SelectedRowId != NULL || String.isNotBlank(SelectedRowId) || SelectedRowId != ''){ system.debug('***2'+SelectedRowId); I am getting debug 2.
    – SFDC 44
    Aug 20 '19 at 6:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.