1+ /* eslint-disable prefer-destructuring */
12import { IInputs } from '../generated/ManifestTypes' ;
23import { WholeNumberType } from '../@types/enums' ;
34import { IAppWrapperProps } from '../components/AppWrapper' ;
@@ -23,12 +24,20 @@ export interface IDataverseService {
2324 openPrimaryEntityForm ( entity : Entity , entityName : string ) : void ;
2425 openErrorDialog ( error : Error ) : Promise < void > ;
2526 openRecordDeleteDialog ( entityName : string ) : Promise < DialogResponse > ;
27+ getRelationships ( parentEntityName : string ) : any ;
28+ openNewRecord ( entityName : string ) : void ;
2629 deleteSelectedRecords (
2730 selectedRecordIds : string [ ] ,
2831 entityName : string ,
2932 ) : Promise < void > ;
3033}
3134
35+ type RelationshipEntity = {
36+ ReferencingEntity : string ;
37+ ReferencingAttribute : string ;
38+ MetadataId : string ;
39+ } ;
40+
3241export class DataverseService implements IDataverseService {
3342 private _context : ComponentFramework . Context < IInputs > ;
3443
@@ -73,6 +82,49 @@ export class DataverseService implements IDataverseService {
7382 return results ;
7483 }
7584
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+
76128 public getWholeNumberFieldName (
77129 format : string ,
78130 entity : Entity ,
0 commit comments