diff --git a/FetchToSubgrid/components/AppWrapper.tsx b/FetchToSubgrid/components/AppWrapper.tsx index a9b308a..744d160 100644 --- a/FetchToSubgrid/components/AppWrapper.tsx +++ b/FetchToSubgrid/components/AppWrapper.tsx @@ -22,10 +22,9 @@ export const AppWrapper: React.FC = props => { const [error, setError] = React.useState(undefined); const fetchToSubgridProps: IFetchToSubgridProps = parseRawInput(props); - if (fetchToSubgridProps.error) setError(error); React.useEffect(() => { - setError(undefined); + setError(fetchToSubgridProps.error); }, [props.fetchXmlOrJson]); return ( diff --git a/FetchToSubgrid/components/FetchToSubgrid.tsx b/FetchToSubgrid/components/FetchToSubgrid.tsx index 477dffd..b90086d 100644 --- a/FetchToSubgrid/components/FetchToSubgrid.tsx +++ b/FetchToSubgrid/components/FetchToSubgrid.tsx @@ -53,9 +53,8 @@ export const FetchToSubgrid: React.FC = props => { React.useLayoutEffect(() => { if (allocatedWidth === -1) return; - listInputsHashCode.current = hashCode(`${allocatedWidth}${fetchXml}`); - }, [allocatedWidth]); + }, [allocatedWidth, columns]); React.useEffect(() => setCurrentPage(1), [pageSize, fetchXml]); @@ -79,10 +78,10 @@ export const FetchToSubgrid: React.FC = props => { const fetchItems = async () => { isButtonActive = false; setIsLoading(true); - totalRecordsCount.current = await dataverseService.getRecordsCount(fetchXml ?? ''); if (isDialogAccepted) return; try { + totalRecordsCount.current = await dataverseService.getRecordsCount(fetchXml ?? ''); const newFetchXml = addOrderToFetch(fetchXml, sortingData); const data: IItemsData = { diff --git a/FetchToSubgrid/services/dataverseService.ts b/FetchToSubgrid/services/dataverseService.ts index f43ccaa..d1739dd 100644 --- a/FetchToSubgrid/services/dataverseService.ts +++ b/FetchToSubgrid/services/dataverseService.ts @@ -59,7 +59,7 @@ export class DataverseService implements IDataverseService { public async getEntityDisplayName(entityName: string): Promise { const entityMetadata: EntityMetadata = await this._context.utils.getEntityMetadata(entityName); - return entityMetadata._displayName; + return entityMetadata?._displayName; } public async getTimeZoneDefinitions(): Promise { @@ -105,6 +105,9 @@ export class DataverseService implements IDataverseService { } public async getRecordsCount(fetchXml: string): Promise { + const top: number = this.getTopInFetchXml(fetchXml); + if (top) return top; + let numberOfRecords = 0; let page = 0; let pagingCookie: string | null = null; @@ -117,6 +120,7 @@ export class DataverseService implements IDataverseService { const xmlDoc: Document = parser.parseFromString(xml, 'text/xml'); const fetch: Element = xmlDoc.getElementsByTagName('fetch')?.[0]; + fetch?.removeAttribute('top'); fetch?.removeAttribute('count'); fetch?.removeAttribute('page'); @@ -188,7 +192,7 @@ export class DataverseService implements IDataverseService { const confirmOptions = { height: 200, width: 450 }; const confirmStrings = { - text: `Do you want to delete this ${entityMetadata._displayName}? + text: `Do you want to delete this ${entityMetadata?._displayName}? You can't undo this action.`, title: 'Confirm Deletion', }; @@ -219,4 +223,16 @@ export class DataverseService implements IDataverseService { private getAllocatedWidth(): number { return this._context.mode.allocatedWidth; } + + private getTopInFetchXml(fetchXml: string): number { + const parser: DOMParser = new DOMParser(); + const xmlDoc: Document = parser.parseFromString(fetchXml, 'text/xml'); + + const top: string | null | undefined = xmlDoc.querySelector('fetch')?.getAttribute('top'); + + if (top) { + return Number(top); + } + return 0; + } } diff --git a/FetchToSubgrid/utilities/d365Utils.ts b/FetchToSubgrid/utilities/d365Utils.ts index c392486..de0b9dc 100644 --- a/FetchToSubgrid/utilities/d365Utils.ts +++ b/FetchToSubgrid/utilities/d365Utils.ts @@ -116,6 +116,8 @@ const createColumnsForEntity = ( const columns: IColumn[] = []; attributesFieldNames.forEach((name, index) => { + if (!displayNameCollection) return; + let displayName = name === `${entityName}id` ? 'Primary Key' : displayNameCollection[name].DisplayName; @@ -167,7 +169,7 @@ export const getEntityData = (props: IItemProps, dataverseService: IDataverseSer } = props; if (attributeType === AttributeType.Number) { - const format: string = entityMetadata.Attributes._collection[fieldName].Format; + const format: string = entityMetadata?.Attributes?._collection[fieldName]?.Format; const field: string = dataverseService.getWholeNumberFieldName( format, entity, @@ -324,7 +326,7 @@ export const getColumns = async ( entityName, attributesFieldNames); - const displayNameCollection: Dictionary = entityMetadata.Attributes._collection; + const displayNameCollection: Dictionary = entityMetadata?.Attributes._collection; const linkEntityAttFieldNames: Dictionary = getLinkEntitiesNamesFromFetchXml( fetchXml ?? '');