function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RbnRbn 

Problem in deleting Notes using VF Page

Hi..

 

I have to delete Notes and Attachment from A VF Section(which consists of all rolled up Notes And Attachments of contacts & Accounts.)

 

right now the delete functionality is working for Attachment.but when i click on Note its Not working

 

below is my code:::

 public void DeleteAccount()
       {
          // if for any reason we are missing the reference 
       system.debug('@@@SelectedAttachmentId '+SelectedAttachmentId);
        
          if (SelectedAttachmentId == null) {
          
             return;
          }
          // find the attachment record within the collection
          Attachment tobeDeleted = null;
          Note tobeDeleted1 = null;
          if(IsNote == True)
          {
          list<Note> not1 = new list<Note>();
            not1 =[select id,Title from Note where Id =: SelectedAttachmentId ];
             system.debug('@@@SelectedAttachmentId '+SelectedAttachmentId);
            system.debug('@@@attach '+not1 );
          for(Note n: not1 )
           if (n.Id == SelectedAttachmentId )
           {
           system.debug('INNNNNNNN');
              tobeDeleted1 = n;
              break;
           }
                 }
          else
          {
          list<Attachment> attach = new list<Attachment>();
            attach=[select id,name,LastModifiedDate,CreatedById  from Attachment where Id =: SelectedAttachmentId ];
             system.debug('@@@SelectedAttachmentId '+SelectedAttachmentId);
             system.debug('@@@attach '+attach);
          for(Attachment a : attach)
           if (a.Id == SelectedAttachmentId ) {
           system.debug('INNNNNNNN');
              tobeDeleted = a;
              break;
           }
          if (tobeDeleted1 != null) {
          system.debug('INNNNNNNN---> '+tobeDeleted); 
           Delete tobeDeleted1;
          }

          //if notes and attachment record found delete it
          if (tobeDeleted != null) {
          system.debug('INNNNNNNN---> '+tobeDeleted); 
           Delete tobeDeleted;
          }
          }
         
          //refreshing the data
          LoadData();
       }    
      
      
    }

 thanks in Advance

souvik9086souvik9086

You are using SelectedAttachmentId in the query. In SelectedAttachmentId, which object record id is present? Is it contains Note ID? If not then it can't be fetched.

 

not1 =[select id,Title from Note where Id =: SelectedAttachmentId ];

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

Puja_mfsiPuja_mfsi

Hi,

I have a doubt in uor code ..You have put the delete code inside the else block.And when u want to delete the Note  then the control go through the If part and yot assign the value on tobeDeleted1.This is correct

 

if(IsNote == True)
{
       list<Note> not1 = new list<Note>();
       not1 =[select id,Title from Note where Id =: SelectedAttachmentId ];
      for(Note n: not1 )
           if (n.Id == SelectedAttachmentId ) {
                   tobeDeleted1 = n;
                   break;
          }
}

 

But why r u putting the deleted code inside the else block.because if the if part is executed you else bloak is not executed at the same time.you need to add the deleted code outside the if else block.

 

else
{
         list<Attachment> attach = new list<Attachment>();
         attach=[select id,name,LastModifiedDate,CreatedById from Attachment where Id =: SelectedAttachmentId ];
          for(Attachment a : attach)
                if (a.Id == SelectedAttachmentId ) {
                       tobeDeleted = a;
                       break;
                }
        if (tobeDeleted1 != null) {
             Delete tobeDeleted1;
        }

       if (tobeDeleted != null) {
              Delete tobeDeleted;
       }
}

 

You need to put the red coloured code outside the if-else code.

 

Please let me know if u have any problem on same and if this post helps u please throw KUDOS by click on star at left.