Professional Services Automation Apex API Developer Reference

pse.SObjectCloneMapper

global virtual with sharing class SObjectCloneMapper

The pse.SObjectCloneMapper class allows you to define the structure of SObjects that you want to clone. This object can be used by a cloning service to describe the data to be copied.

Properties

Name Type Description
SObjectRecordIdsToClone Set<Id> A set of Ids to identify the records you want to clone from this SObjectType. Leave null to clone all Ids found through relationships. If an empty list is passed, an attempt is not made to clone sObjects of this type.
SObjectTypeToClone Schema.SObjectType The SObjectType to clone.
SObjectFieldsToClone Set<pse.SObjectCloneMapper.Field> The set of fields from SObjectTypeToClone to be copied from the cloned records. Fields on SObjectTypeToClone which are not in this set will have default values in the cloned records and will not have values copied from the source records.
SObjectRecordsToOverride Map<Id, SObject> A map of IDs to SObject records that are used to override cloned template records. To override the records, use the template record ID as the map key, and an SObject with values as the map value.

Methods

SObjectCloneMapper

global SObjectCloneMapper(Schema.SObjectType sObjectTypeToClone, Set<pse.SObjectCloneMapper.Field> sObjectFieldsToClone)

A Default constructor with minimum required properties.

Input Parameters

Name Type Description
sObjectTypeToClone Schema.SObjectType The SObjectType of the object to be cloned.
sObjectFieldsToClone Set<pse.SObjectCloneMapper.Field> The set of SObjectCloneMapper.Fields to be cloned.

Sample Code

//Note: This sample code is for demonstration purposes only. It is not intended for
//use in a production environment, is not guaranteed against defects or errors, and
//is in no way optimized or streamlined.

// Define the object fields which are required to be cloned.
// Define the default value to be used for the field. When not defined, the value is copied from the template project.
pse.SObjectCloneMapper.Field mapperField1 =
    new pse.SObjectCloneMapper.Field(Schema.pse__Proj__c.Name);
mapperField1.defaultValue = 'MapperProjectName';
pse.SObjectCloneMapper.Field mapperField2 =
    new pse.SObjectCloneMapper.Field(Schema.pse__Proj__c.pse__Is_Active__c);
mapperField2.DefaultValue = true;
pse.SObjectCloneMapper.Field mapperField3 =
    new pse.SObjectCloneMapper.Field(Schema.pse__Proj__c.pse__Is_Billable__c);
mapperField3.DefaultValue = true;

// Group together the pse.SObjectCloneMapper.Field instantiated above for each object and prepare a set.
Set<pse.SObjectCloneMapper.Field> setMapperFields =
    new Set<pse.SObjectCloneMapper.Field>{mapperField1, mapperField2, mapperField3};

// Instantiate the pse.SObjectCloneMapper using the prepared set of pse.SObjectCloneMapper.Field for each object.
pse.SObjectCloneMapper mapp = new pse.SObjectCloneMapper(pse__Proj__c.SObjectType, setMapperFields);

pse.SObjectCloneMapper.NullFieldValue

global inherited sharing class NullFieldValue

Used to represent an explicit null value. When the DefaultValue of a field is set to an instance of this class, the cloned records are created with a null in that field. The cloner should copy values for that field from the template when a default value is not provided.

Sample Code

//Note: This sample code is for demonstration purposes only. It is not intended for
//use in a production environment, is not guaranteed against defects or errors, and
//is in no way optimized or streamlined.

/**
 * As an example, pse__Action_Date__c is a custom field and has been added to pse__Proj__c. This field has a default
 * value such that when a new project is created, the pse__Action_Date__c is set to a month in the future. The template
 * has a value in pse__Action_Date__c which should not be present in the cloned project. When a project is copied from
 * this template using pse.CreateProjectFromTemplateService, the business rules are that it should not have a date
 * in this field. If pse__Action_Date__c is given to pse.CreateProjectFromTemplateService (either as a mapper, or in a
 * FieldSet) then the tool will copy the value from the template. If the field is not provided, then the project is
 * created with a default value of next month. By using NullFieldValue, the cloned project can be created with null pse__Action_Date__c.
 */

// Define a Field for pse__Action_Date__c to force the value to be null in the cloned project and not copy the value from the template project.
pse.SObjectCloneMapper.Field actionDateFieldMapper =
    new pse.SObjectCloneMapper.Field(Schema.pse__Proj__c.pse__Action_Date__c);
actionDateFieldMapper.DefaultValue = new pse.SObjectCloneMapper.NullFieldValue();

// Instantiate the SObjectCloneMapper using the prepared set of pse.SObjectCloneMapper.Field for each object.
pse.SObjectCloneMapper mapp = new pse.SObjectCloneMapper(pse__Proj__c.SObjectType,
    new Set<pse.SObjectCloneMapper.Field>{actionDateFieldMapper});

// Prepare the required fields for the ProjectRequest instance to be created.
Id templateProjectId = Id.valueOf('a1Q000000000000'); // Our template project.
Date startDateForProject = Date.Today();

pse.CreateProjectFromTemplateService.CreateProjectFromTemplateRequest request =
    new pse.CreateProjectFromTemplateService.CreateProjectFromTemplateRequest(templateProjectId, startDateForProject);
request.mappers = new List<pse.SObjectCloneMapper> { mapp };

// Pass the request to the service which will create the project with no date in pse__Action_Date__c as intended.
pse.CreateProjectFromTemplateService.createProjectsFromTemplate(
        new List<pse.CreateProjectFromTemplateService.CreateProjectFromTemplateRequest>{request});

pse.SObjectCloneMapper.Field

global inherited sharing class Field

Each Field provides information about a field on the sObject you want to clone. Add instances of this class to SObjectFieldsToClone on SObjectCloneMapper.

Properties

Name Type Description
DefaultValue Object A value entered here is applied to the field of the cloned record. Leaving this value as null copies the field from the template. Other behavior may depend on configuration and settings. Setting DefaultValue to an instance of SObjectCloneMapper.NullFieldValue causes the cloned object to be created with a null value in this field.

Methods

Field

global Field(Schema.SObjectField field)

A default constructor with minimum required properties.

Input Parameters

Name Type Description
field Schema.SObjectField The Schema.SObjectField you want to clone.
© Copyright 2009–2024 Certinia Inc. All rights reserved. Various trademarks held by their respective owners.