Skip to content

Commit 0a4c9e8

Browse files
Merge pull request #19 from BeverCRM/bug/openLookupForm
fixed paging issue
2 parents c77522d + 79979d5 commit 0a4c9e8

File tree

7 files changed

+35
-39
lines changed

7 files changed

+35
-39
lines changed

FetchToSubgrid/components/FetchToSubgrid.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,6 @@ export const FetchToSubgrid: React.FC<IFetchToSubgridProps> = props => {
5757
listInputsHashCode.current = hashCode(`${allocatedWidth}${fetchXml}`);
5858
}, [allocatedWidth]);
5959

60-
React.useMemo(async () => {
61-
totalRecordsCount.current = await dataverseService.getRecordsCount(fetchXml ?? '');
62-
}, [fetchXml, isDialogAccepted]);
63-
6460
React.useEffect(() => setCurrentPage(1), [pageSize, fetchXml]);
6561

6662
React.useEffect(() => {
@@ -83,6 +79,7 @@ export const FetchToSubgrid: React.FC<IFetchToSubgridProps> = props => {
8379
const fetchItems = async () => {
8480
isButtonActive = false;
8581
setIsLoading(true);
82+
totalRecordsCount.current = await dataverseService.getRecordsCount(fetchXml ?? '');
8683
if (isDialogAccepted) return;
8784

8885
try {

FetchToSubgrid/components/Footer.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export const Footer: React.FC<IFooterProps> = props => {
2828
} = props;
2929

3030
const firstItemIndex = (currentPage - 1) * pageSize + 1;
31-
const lastItemIndex = firstItemIndex + pageSize - 1;
31+
const lastItemIndex = Math.min(firstItemIndex + pageSize - 1, totalRecordsCount);
3232
const nextButtonDisabled = Math.ceil(totalRecordsCount / pageSize) <= currentPage;
3333

3434
function moveToFirst() {

FetchToSubgrid/components/LinkableItems.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const LinkableItem: React.FC<ILinkableItemProps> = ({
1414
if (item.isLinkEntity) {
1515
dataverseService.openLinkEntityRecordForm(item.entity, item.fieldName);
1616
}
17-
else if (checkIfAttributeIsEntityReference(item.AttributeType)) {
17+
else if (checkIfAttributeIsEntityReference(item.attributeType)) {
1818
dataverseService.openLookupForm(item.entity, item.fieldName);
1919
}
2020
else {

FetchToSubgrid/services/dataverseService.ts

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export class DataverseService implements IDataverseService {
4747
fetchXmlOrJson,
4848
allocatedWidth: this.getAllocatedWidth(),
4949
default: {
50-
fetchXml: this._context.parameters.defaultFetchXmlProperty.raw,
50+
fetchXml: this._context.parameters.defaultFetchXml.raw,
5151
pageSize: defaultPageSize,
5252
newButtonVisibility: this._context.parameters.newButtonVisibility.raw === '1',
5353
deleteButtonVisibility: this._context.parameters.deleteButtonVisibility.raw === '1',
@@ -105,26 +105,34 @@ export class DataverseService implements IDataverseService {
105105
}
106106

107107
public async getRecordsCount(fetchXml: string): Promise<number> {
108-
let pagingCookie = null;
109108
let numberOfRecords = 0;
110109
let page = 0;
111-
const parser: DOMParser = new DOMParser();
112-
const xmlDoc: Document = parser.parseFromString(fetchXml, 'text/xml');
113-
const fetch: Element = xmlDoc.getElementsByTagName('fetch')?.[0];
110+
let pagingCookie: string | null = null;
114111

115112
const entityName: string = getEntityNameFromFetchXml(fetchXml);
116-
fetch?.removeAttribute('count');
117113
const changedAliasNames: string = changeAliasNames(fetchXml);
118114

119-
do {
115+
const updateFetchXml = (xml: string, page: number): string => {
116+
const parser: DOMParser = new DOMParser();
117+
const xmlDoc: Document = parser.parseFromString(xml, 'text/xml');
118+
const fetch: Element = xmlDoc.getElementsByTagName('fetch')?.[0];
119+
120+
fetch?.removeAttribute('count');
120121
fetch?.removeAttribute('page');
121-
fetch.setAttribute('page', `${++page}`);
122-
// eslint-disable-next-line no-invalid-this
122+
123+
fetch.setAttribute('page', `${page}`);
124+
125+
const serializer = new XMLSerializer();
126+
return serializer.serializeToString(xmlDoc);
127+
};
128+
129+
do {
130+
const updatedFetchXml: string = updateFetchXml(changedAliasNames, ++page);
123131
const data: any = await this._context.webAPI.retrieveMultipleRecords(
124-
entityName, `?fetchXml=${encodeURIComponent(changedAliasNames)}`);
132+
entityName, `?fetchXml=${encodeURIComponent(updatedFetchXml)}`);
133+
125134
numberOfRecords += data.entities.length;
126135
pagingCookie = data.fetchXmlPagingCookie;
127-
128136
} while (pagingCookie);
129137

130138
return numberOfRecords;
@@ -153,10 +161,16 @@ export class DataverseService implements IDataverseService {
153161
}
154162

155163
public openLookupForm(entity: Entity, fieldName: string): void {
156-
const entityName: string = entity[
157-
`_${fieldName}_value@Microsoft.Dynamics.CRM.lookuplogicalname`];
158-
159-
const entityId: string = entity[`_${fieldName}_value`];
164+
let entityName: string;
165+
let entityId: string;
166+
if (fieldName.startsWith('alias')) {
167+
entityName = entity[`${fieldName}@Microsoft.Dynamics.CRM.lookuplogicalname`];
168+
entityId = entity[`${fieldName}`];
169+
}
170+
else {
171+
entityName = entity[`_${fieldName}_value@Microsoft.Dynamics.CRM.lookuplogicalname`];
172+
entityId = entity[`_${fieldName}_value`];
173+
}
160174
this.openRecordForm(entityName, entityId);
161175
}
162176

FetchToSubgrid/styles/comandBarStyles.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ export const dataSetStyles = mergeStyleSets({
1616
},
1717
buttons: {
1818
height: '44px',
19-
paddingRight: '20px',
2019
},
2120
detailsList: {
2221
paddingTop: '0px',

FetchToSubgrid/utilities/utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,16 @@ interface IJsonProps {
2424
type JsonAllowedProps = Array<keyof IJsonProps>;
2525

2626
export const checkIfAttributeIsEntityReference = (attributeType: AttributeType): boolean => {
27-
const attributetypes: AttributeType[] = [
27+
const attributeTypes: AttributeType[] = [
2828
AttributeType.Lookup,
2929
AttributeType.Owner,
3030
AttributeType.Customer,
3131
];
3232

33-
return attributetypes.includes(attributeType);
33+
return attributeTypes.includes(attributeType);
3434
};
3535

36-
export const checkIfAttributeRequiresFormattedValue = (attributeType: AttributeType) => {
36+
export const checkIfAttributeRequiresFormattedValue = (attributeType: AttributeType): boolean => {
3737
const attributeTypes: AttributeType[] = [
3838
AttributeType.Money,
3939
AttributeType.PickList,

Solution/README.md

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)