-
Notifications
You must be signed in to change notification settings - Fork 23
Code Generation
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.
-
npm install ts-force-gen -d.
NOTE: Your installed version of
ts-force-genshould ALWAYS match yourts-forceMajor and Minor version (EG:2.0.x).
- create a file in the root call
ts-force-config.json - 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
.tsfile extension inoutPath.
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).
- run
npx ts-force-gen -j ts-force-config.json. Using npx is preferred to ensure compatibility between ts-force & ts-force gen. - 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.
- Open your dev org. On Account, create a new text field with API name of
Name(EG:Name__conce created) - run
ts-force-gen -j ts-force-config.jsonagain - 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"
}
]
}- Run the generate cmd again and you'll now see that the generated class is now called
MyAccountandName__chas been mapped tonameCustom.