How to fix (An Internal error has occoued)). error status code 5001 in Pivot Filter option with Excel javascript API

vikas 1 Reputation point
2023-06-22T09:14:35.5233333+00:00
I am using Excel Javascript API to build Excel add-in with Pivot table (PivotFilter Function), I can add the filter with the following code from document.
but when I use remove function it removing the filter which I added, after agian I add in filter It getting error(An Internal error has occoued) 
after excel is closed.

 

This below code only I did:
async function add_pivot_filter() {
  await Excel.run(async (context) => {
    await context.sync();
    let pivotTable = context.workbook.worksheets.getActiveWorksheet().pivotTables.getItem("Farm Sales");
    // check if the PivotTable already has rows
    await context.sync();
    // addind one filter
    let classHierarchy = pivotTable.filterHierarchies.getItemOrNullObject("Classification");
    await context.sync();
    if (classHierarchy.isNullObject) {
      classHierarchy = pivotTable.filterHierarchies.add(pivotTable.hierarchies.getItem("Classification"));

      await context.sync()
    }
    // again adding another one filter
    let framHierarchy = pivotTable.filterHierarchies.getItemOrNullObject("Farm");
    await context.sync();
    if (framHierarchy.isNullObject) {
      framHierarchy = pivotTable.filterHierarchies.add(pivotTable.hierarchies.getItem("Farm"));

      await context.sync()
    }
    // removing one filter
    let filter_remove = pivotTable.filterHierarchies.getItemOrNullObject("Classification");
    await context.sync();
    if (filter_remove.isNullObject == false) {
      pivotTable.filterHierarchies.remove(filter_remove);
      await context.sync();
    }
    // again adding one filter
    let TypeHierarchy = pivotTable.filterHierarchies.getItemOrNullObject("Type");
    await context.sync();
    if (TypeHierarchy.isNullObject) {
      TypeHierarchy = pivotTable.filterHierarchies.add(pivotTable.hierarchies.getItem("Type"));

      await context.sync()
    }
  });
}

Note:
* I am adding one filter it adding without any error.
* Again adding another one filter it adding without any error.
* After I removing one filter it removing.
* After adding one filter again the excel sheet was closed.
The error was((An Internal error has occoued)). error status code 5001
Microsoft 365 and Office | Development | Office JavaScript API
Microsoft 365 and Office | Excel | For business | Windows
0 comments No comments
{count} votes

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.