1
+ /* eslint-disable prefer-destructuring */
1
2
import { IInputs } from '../generated/ManifestTypes' ;
2
3
import { WholeNumberType } from '../@types/enums' ;
3
4
import { IAppWrapperProps } from '../components/AppWrapper' ;
@@ -23,12 +24,20 @@ export interface IDataverseService {
23
24
openPrimaryEntityForm ( entity : Entity , entityName : string ) : void ;
24
25
openErrorDialog ( error : Error ) : Promise < void > ;
25
26
openRecordDeleteDialog ( entityName : string ) : Promise < DialogResponse > ;
27
+ getRelationships ( parentEntityName : string ) : any ;
28
+ openNewRecord ( entityName : string ) : void ;
26
29
deleteSelectedRecords (
27
30
selectedRecordIds : string [ ] ,
28
31
entityName : string ,
29
32
) : Promise < void > ;
30
33
}
31
34
35
+ type RelationshipEntity = {
36
+ ReferencingEntity : string ;
37
+ ReferencingAttribute : string ;
38
+ MetadataId : string ;
39
+ } ;
40
+
32
41
export class DataverseService implements IDataverseService {
33
42
private _context : ComponentFramework . Context < IInputs > ;
34
43
@@ -73,6 +82,49 @@ export class DataverseService implements IDataverseService {
73
82
return results ;
74
83
}
75
84
85
+ public async openNewRecord ( entityName : string ) : Promise < void > {
86
+ // @ts -ignore
87
+ const contextPage = this . _context . page ;
88
+ // @ts -ignore
89
+ const lookupName : string = this . _context . mode . contextInfo . entityRecordName ;
90
+ const parentEntityName : string = contextPage . entityTypeName ;
91
+ const entityId : string = contextPage . entityId ;
92
+
93
+ const relationshipEntities : RelationshipEntity [ ] = await this . getRelationships (
94
+ parentEntityName ) ;
95
+
96
+ const relationship : RelationshipEntity | undefined = relationshipEntities . find (
97
+ relationshipEntity => relationshipEntity . ReferencingEntity === entityName ) ;
98
+
99
+ if ( relationship ) {
100
+ const lookup : { id : string , name : string , entityType : string } =
101
+ { id : entityId , name : lookupName , entityType : parentEntityName } ;
102
+ const formParameters : { [ key : string ] : string } =
103
+ { [ relationship . ReferencingAttribute ] : JSON . stringify ( lookup ) } ;
104
+
105
+ this . _context . navigation . openForm ( { entityName } , formParameters )
106
+ . then ( ( success : unknown ) => console . log ( success ) )
107
+ . catch ( ( error : unknown ) => console . log ( error ) ) ;
108
+ }
109
+ else {
110
+ this . _context . navigation . openForm ( { entityName } ) ;
111
+ }
112
+ }
113
+
114
+ public async getRelationships ( parentEntityName : string ) : Promise < RelationshipEntity [ ] > {
115
+ // @ts -ignore
116
+ const contextPage = this . _context . page ;
117
+ const entityDefinitions = `/api/data/v9.2/EntityDefinitions(LogicalName='${ parentEntityName } ')` ;
118
+ const relationships = `OneToManyRelationships($select=ReferencingEntity,ReferencingAttribute)` ;
119
+ const logicalName = '&$select=LogicalName' ;
120
+
121
+ const response : Response = await fetch (
122
+ `${ contextPage . getClientUrl ( ) } ${ entityDefinitions } ?$expand=${ relationships } ${ logicalName } ` ) ;
123
+
124
+ const result = await response . json ( ) ;
125
+ return result . OneToManyRelationships ;
126
+ }
127
+
76
128
public getWholeNumberFieldName (
77
129
format : string ,
78
130
entity : Entity ,
0 commit comments