Step-by-Step: Generate Salesforce Object Definition and API Files

A core feature of Playforce is its ability to extract Salesforce Object and Field metadata and automatically generate strongly typed structures and APIs.

Follow the steps below to generate object files tailored to your project’s needs.

1. Create a Playwright Test File

Create a file named get-salesforce-objects.spec.ts in your project.

Inside this file, list the Salesforce objects you want to generate definition and API files for. You can include both standard objects (like Account, Contact) and custom objects (which typically end with __c).

Example:

// tests/get-salesforce-objects.spec

import test from "@playwright/test";
import { generateSalesforceObjects } from 'playforce';

test.describe('Generate Salesforce Object Typed Definition & API files', () => {
  test('Generate Salesforce Object Typed Definition & API files', async ({}, testInfo) => {

    const objects = [
      'Account',
      'Case',
      'Contact',
      'Custom_Object__c',
      'Project__c',
    ];  

    //This will generate your typed defintion files and APIs in /requests/
    await Playforce.generateSalesforceObjects(objects, testInfo.config.rootDir)
    
  });
});

💡 Tip: You only need to list the object names. Playforce will fetch the metadata and handle the rest automatically.

2. Run the generation ‘test’

Run the generator just like any other Playwright test:

npx playwright test tests/get-salesforce-objects.spec

This will:

  •      • Connect to your Salesforce org
  •      • Fetch metadata for the listed objects
  •      • Generate strongly typed .ts definitions and Object APIs
  •      • Create or update the SalesforceRequests entry point
  •      • Save all generated files to ./requests/

✅ Once complete, your project will have strongly typed access to your Salesforce data, ready for use in tests and utilities.


What Gets Generated

Running a Generation will create and maintain the following structure in your Playwright project:

Typed Definition Files (./requests/defs/objects)

These files define the shape of each Salesforce object and are automatically generated based on your org’s metadata.

⚠️ Do not edit these files manually.

If the structure of an object changes in Salesforce, simply re-run the generator to update the definitions.

Example Acccount.def.ts file

// ⚠️ AUTO-GENERATED FILE — DO NOT EDIT
// Generated by Playforce 2025-05-26T10:09:18.861Z

export const objectType = "Account";

export interface Account {
	AccountNumber?:string                                       // string          "Account Number"             
	AccountSource?:string                                       // picklist        "Account Source"             
	Active__c?:string                                           // picklist        "Active"       
...}

export const Account = {
	AccountNumber: "AccountNumber",                             // string          "Account Number"             
	AccountSource: "AccountSource",                             // picklist        "Account Source"             
	Active__c: "Active__c",                                     // picklist        "Active"   
...}

These files provide full type safety when interacting with Salesforce records in your tests or helper functions.


SalesforceRequests.ts

This file serves as the entry point for interacting with your generated Salesforce object APIs.

You can use it directly in your tests or utilities, for example:

await SalesforceRequests.Contact.createAndGetObject({ FirstName: 'Ada', LastName: 'Lovelace' });

⚠️ Do not edit this file manually.

It is auto-generated and is refreshed whenever you add or remove objects in your generation list.

SalesforceRequests

// ⚠️ AUTO-GENERATED FILE — DO NOT EDIT
// Generated by Playforce 2025-05-26T10:09:18.861Z

import { AccountBase, accountMethods } from './salesforce/Account';
import { CaseBase, caseMethods } from './salesforce/Case';
import { ContactBase, contactMethods } from './salesforce/Contact';

/**
 * Request operations on the listed Salesforce objects.
 */

export const SalesforceRequests = {
   Account: { ...AccountBase, ...accountMethods(AccountBase)},
   Case: { ...CaseBase, ...caseMethods(CaseBase)},
   Contact: { ...ContactBase, ...contactMethods(ContactBase)},
};

This structure combines each object’s base fields and available request methods, giving you a type-safe and unified API for your Salesforce tests.


API files

These files are generated once for each newly added object in your generation list. They are not overwritten in future generations, so you can safely extend them with custom logic.

🛠️ Feel free to add custom helper methods inside these files.
They remain untouched on subsequent regenerations unless the file is deleted and regenerated.

Example (Generated API File)

// Generated by Playforce 2025-05-26T10:09:18.861Z
    
import { createTypedSalesforceObject, SalesforceObjectInterface } from 'playforce';
import { Case, objectType } from './defs/objects/Case.def';
export { Case, objectType };

export const CaseBase = createTypedSalesforceObject<Case>(objectType);

export function caseMethods(caseBase: SalesforceObjectInterface<Case>) {
 return {
    // Add method definitions here e.g.

    // getCaseObjectByEmail: async (email: string): Promise<Case> => {
    //     return await caseBase.getObjectByField(Case.Email, email)    
    // },
    // getCaseObjectByName: async (name: string): Promise<Case> => {
    //     return await caseBase.getObjectByField(Case.Name, name)    
    // },

  };
}

This structure provides a dedicated place to define reusable operations for each object, helping you keep your tests and utilities clean, consistent, and easy to maintain.