Skip to content

GenerateEntity

Niels Steenbeek edited this page Mar 17, 2023 · 13 revisions

CLI Command generate Entity

Command

hso-d365 generate Entity <name> [--skipForms] [--entityLogicalName x]

Description

CLI Command 'generate Entity' generates an Entity. The flag --skipForms is optional. When set, the command will not generate the forms files and .formContext.ts file.

Generated files

Following files will be generated:

  1. src/[name]/[name].enum.ts
  2. src/[name]/[name].formContext.ts
  3. src/[name]/[name].model.ts
  4. src/[name]/[name].service.ts
  5. src/[name]/build.json
  6. src/[name]/[formName]/[formName].ts
  7. src/[name]/[formName]/[formName].form.ts
  8. src/[name]/[formName]/[formName].formContext.ts

[name]/build.json

In this file you can turn components on/off.

[name]/[formName]/[formName].ts

This is the root file for every form. It mentions the function you have to add to your D365 form or ribbon:

  • onLoad function for your form (take care to check the 'pass formcontext' checkbox)
  • myRibbonMethod for your ribbon Normally you don't touch this file that much.

[name]/[formName]/[formName].form.ts

This is the file for form logic. The onLoad is the starting point for all your business logic.

import {Translation} from '../../translation/Translation';
import {AccountService} from '../Account.service';
import {AccountModel} from '../Account.model';
import {AccountHSOFormContext} from './AccountHSO.formContext';

export class AccountHSOForm extends AccountHSOFormContext {
    static async onLoad(executionContext: Xrm.Events.EventContext): Promise<void> {
        const accounts: AccountModel[] = await AccountService.retrieveMultipleRecords({
            select: ['name', 'description'],
            filters: [{
                type: 'and',
                conditions: [{
                    attribute: 'name',
                    operator: 'eq',
                    value: 'test'
                }]
            }]
        });
        for (const account of accounts) {
            console.log(`${account.name} has description ${account.description});
        }
    }
}

[name].model.ts

This file contains all fields/NavigationPropertyNames for the odata query. The [name].service.ts class needs it.

export interface AccountModel extends Model {
    name?: string;
    description?: string;
}

[name].service.ts

This file wraps the Xrm.WebApi methods and also knows the logicalName. Here you can add [name] Entity specific API calls and actions. Normally you use the Service class in your Form class instead of using the WebApi class directly. See more about WebApi.

Help

Use command

hso-d365 generate Entity --help