vlocode-project - v1.40.0-beta-4
    Preparing search index...

    Class SalesforceProfile

    Represents a Salesforce user permissions model, providing methods to manage and manipulate metadata for profiles and permission sets. This class allows for loading, merging, updating, and saving metadata related to user permissions, such as field permissions, object permissions, Apex class access, and record type visibility.

    • The class supports XML parsing and serialization for Salesforce metadata.
    • Changes to the metadata are tracked, and only updated fields are saved back to Salesforce.
    • Provides utility methods for checking, adding, updating, and removing permissions for various Salesforce components like fields, pages, classes, and record types.
    const profile = new SalesforceUserPermissions('Profile', 'CustomProfile');
    profile.addField('Account.Name', 'editable');
    profile.setRecordTypeVisibility('Account.CustomRecordType', true);
    await profile.save(connection);

    Hierarchy (View Summary)

    Index

    Constructors

    Properties

    "#hasChanges": boolean = false
    developerName: string
    type: "PermissionSet" | "Profile"

    Accessors

    Methods

    • Adds or updates the access permissions for an Apex class. If the class does not exist in the current metadata, it will be added with the specified enabled state. If the class already exists, its enabled state will be updated if it differs from the provided value.

      Parameters

      • name: string

        The name of the Apex class to add or update.

      • enabled: boolean

        A boolean indicating whether access to the class is enabled.

      Returns void

    • Adds a field permission to the profile model. If the field already exists, its permissions are updated.

      Parameters

      • field: string

        The name of the field to add or update.

      • access: SalesforceFieldPermission

        The access level for the field, which can be 'editable' or 'readable'. - 'editable': Grants both read and write access to the field. - 'readable': Grants read-only access to the field.

      Returns void

      If the field already exists in the profile model, its permissions are updated based on the provided access level. If the field does not exist, it is added to the profile model with the specified permissions.

      profile.addField('Account.Name', 'editable'); // Adds or updates the field with editable access.
      profile.addField('Account.Phone', 'readable'); // Adds or updates the field with readable access.
    • Adds or updates the access permissions for an Apex page. If the page already exists in the permissions list, its enabled status is updated. Otherwise, the page is added to the metadata with the specified enabled status.

      Parameters

      • name: string

        The name of the Apex page to add or update.

      • enabled: boolean

        A boolean indicating whether the page access should be enabled.

      Returns void

    • Checks if the profile contains a specific field.

      Parameters

      • ...field: string[]

        One or more strings representing the field name(s) to check. If multiple strings are provided, they are joined with a dot (.) to form a composite field name.

      Returns boolean

      true if the profile contains the specified field, otherwise false.

    • Loads and parses metadata XML content, merging the parsed data into the current instance.

      Parameters

      • metadataXml: string | Buffer<ArrayBufferLike>

        The metadata XML content to load, provided as a string or Buffer.

      Returns this

      The current instance with the merged data.

      The method uses custom parsing logic to handle specific data types:

      • Boolean values are parsed from "true" or "false" strings (case-insensitive).
      • Integer values are parsed from numeric strings.
      • Floating-point values are parsed from decimal strings.
      • Other values are returned as-is.

      Array properties are determined based on the arrayProperties configuration.

    • Merges the provided partial user permission metadata into the current metadata object. This method normalizes keys to ensure case-insensitivity and handles merging of array values. If the key exists in the current metadata and is an array, the values are appended. Otherwise, the value is directly assigned to the metadata. Unmapped properties are preserved and added to the metadata.

      Parameters

      Returns void

    • Removes an item from the specified metadata and metadataUpdates arrays based on the provided key and name.

      Type Parameters

      • K extends
            | "description"
            | "objectPermissions"
            | "applicationVisibilities"
            | "classAccesses"
            | "customMetadataTypeAccesses"
            | "customPermissions"
            | "customSettingAccesses"
            | "externalDataSourceAccesses"
            | "fieldPermissions"
            | "flowAccesses"
            | "pageAccesses"
            | "recordTypeVisibilities"
            | "userPermissions"
            | "fullName"

      Parameters

      • key: K

        The key representing the property in the metadata object. This key must correspond to an array property.

      • name: string

        The name or identifier of the item to be removed. This is matched against the field specified by PermissionNameFields[key].

      Returns void

      Throws an error if the provided key does not correspond to an array property.

    • Sets the visibility of a specific record type for the current profile. If the record type visibility already exists, it updates the visibility. Otherwise, it adds a new record type visibility entry to the metadata.

      Parameters

      • recordTypeName: string | { name: string; objectType: string }

        The name of the record type to set visibility for.

      • visible: boolean = true

        The visibility status to set for the record type. Defaults to true.

      Returns void

    • Update entries on an array property of the profile object. If the new value is different than the current value, the change will be tracked.

      Type Parameters

      • K extends
            | "description"
            | "objectPermissions"
            | "applicationVisibilities"
            | "classAccesses"
            | "customMetadataTypeAccesses"
            | "customPermissions"
            | "customSettingAccesses"
            | "externalDataSourceAccesses"
            | "fieldPermissions"
            | "flowAccesses"
            | "pageAccesses"
            | "recordTypeVisibilities"
            | "userPermissions"
            | "fullName"

      Parameters

      Returns void

    • Validates and ensures the correct default record type assignment for a given object type.

      This method checks the record types associated with the specified object type and ensures that the default record type is properly assigned based on visibility rules:

      • If there are visible record types, exactly one of them must be marked as the default.
      • If no record types are visible, no default should be assigned.

      If the current default assignment is invalid, this method updates the record type visibilities to ensure compliance with the rules. It assigns the first visible record type as the default if necessary.

      Parameters

      • objectType: string

        The API name of the object type for which to validate default record type assignment.

      Returns void

    • Load a profile from XML buffer or string that repeats the profile metadata

      Parameters

      • name: string

        Name of the profile; for metadata this is the file name without extension.

      • profileXmlBody: string | Buffer<ArrayBufferLike>

        String or buffer containing the profile XML

      Returns SalesforceProfile

      A profile object