Skip to content

Code Generation

Charlie Jonas edited this page Apr 26, 2019 · 13 revisions

While some aspects of ts-force can be used without it, the real value of this libraries comes from generated classes. The code generation command has been split out to the ts-force-gen package so that it can easily be excluded from production builds.

  1. npm install ts-force-gen -d.

NOTE: Your installed version of ts-force-gen should ALWAYS match your ts-force Major and Minor version (EG: 2.0.x).

  1. create a file in the root call ts-force-config.json
  2. Add the following:
{
  "$schema": "https://raw.githubusercontent.com/ChuckJonas/ts-force/master/ts-force-gen/ts-force-config.schema.json",
  "auth": {
    "username": "SET_THIS_TO_DX_USER"
  },
  "sObjects": [
      "Account",
      "Contact",
      "User"
    ],
  "outPath": "./src/generated"
}

NOTE: you can generate the classes to a single file by specifying a .ts file extension in outPath.

For username, you will need to use a sfdx-cli authorized user of a developer or scratch org which you can muck up (see ts-force-gen readme for other authentication & command line options).

  1. run npx ts-force-gen -j ts-force-config.json. Using npx is preferred to ensure compatibility between ts-force & ts-force gen.
  2. Look at ./src/generated/ and scan over what has been created. You'll see a class for each SObject. Notice that the properties have all been 'prettified' to javascript standard naming conventions.

Handling Identifier Conflicts / Custom Mappings

  1. Open your dev org. On Account, create a new text field with API name of Name (EG: Name__c once created)
  2. run ts-force-gen -j ts-force-config.json again
  3. Open ./src/generated/account.ts. Note the error: Duplicate identifier 'name'.

In this case, the auto-mapping is in conflict with the standard name field. You can override the auto-mapping by replacing 'Account' literal string with:

{
  "apiName": "Account",
  "className": "MyAccount",
  "fieldMappings": [
    {
      "apiName" : "Name__c",
      "propName": "nameCustom"
    }
  ]
}
  1. Run the generate cmd again and you'll now see that the generated class is now called MyAccount and Name__c has been mapped to nameCustom.

Clone this wiki locally