Professional Services Automation Apex API Developer Reference

pse.CreateProjectFromTemplateService

global with sharing class CreateProjectFromTemplateService

A service used to clone one or more projects from templates. You can use this service in conjunction with SObjectCloneMapper, as the mappers on a clone request, to add extra fields to objects or extra objects that are copied from the template. You must include the fields describing the relationship between extra objects and the template project.
Although the API can accept a list of requests, we recommend that you only process single requests. This is because projects are created synchronously, which means triggers, flows, workflow rules, and similar processing can cause SOQL limits to be reached, even with a small number of requests (up to a maximum of three to five requests in most cases).
For example, to copy a new custom object with a Project lookup field to a cloned project, add a mapper to the request and add all the fields to copy from the custom object, including the lookup field.

Methods

createProjectsFromTemplates

global static List<pse.CreateProjectFromTemplateService.CreateProjectResponse> createProjectsFromTemplates(List<pse.CreateProjectFromTemplateService.CreateProjectFromTemplateRequest> requests)

This method creates new projects from a project template using details provided in the CreateProjectFromTemplateRequest list. The following configuration is used:
• budget-name-suffix
• default-budget-status
• default-budget-type
• default-is-active
• default-time-credited
• default-time-excluded
• default-is-billable

Input Parameters

Name Type Description
requests List<pse.CreateProjectFromTemplateService.CreateProjectFromTemplateRequest> The list of CreateProjectFromTemplateRequests.

Return Value

This service returns CreateProjectResponses in a list that parallels the input list.

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.

// Prepare the required fields for the CreateProjectFromTemplateRequest instance to be created.
Date startDateForProject = Date.Today();// Give a project start date to be used to populate the start date of project.
Id templateProjectId = Id.valueOf('a1Q1a0000006VJ4');// Give a valid template project Id to clone the new project.

// Instantiate the request to create project from template.
pse.CreateProjectFromTemplateService.CreateProjectFromTemplateRequest PROJ_REQ1 =
    new pse.CreateProjectFromTemplateService.CreateProjectFromTemplateRequest(templateProjectId, startDateForProject);
pse.CreateProjectFromTemplateService.CreateProjectFromTemplateRequest PROJ_REQ2 =
    new pse.CreateProjectFromTemplateService.CreateProjectFromTemplateRequest(templateProjectId, startDateForProject);

// Call the service to create the projects using the request list and store a list of responses.
List<pse.CreateProjectFromTemplateService.CreateProjectResponse> PROJ_RESPONSES =
    pse.CreateProjectFromTemplateService.createProjectsFromTemplates(
        new List<pse.CreateProjectFromTemplateService.CreateProjectFromTemplateRequest>{PROJ_REQ1, PROJ_REQ2});

// Loop for each response and detect whether requests were successfully processed.
for(pse.CreateProjectFromTemplateService.CreateProjectResponse response : PROJ_RESPONSES)
{
    if( response.isSuccess() ) // Get the new project Ids from the response when successfully processed in the request.
    {
        System.debug('Request for this response :' + response.request);
        System.debug('New project Id for the successful cloned project :' + response.NewProjectId); // All related information can be queried using this project Id.
    }
    else // Get the errors from the response for unsuccessful requests.
    {
        System.debug('Request for this response :' + response.Request);
        System.debug('Errors related to unsuccessful request :' + response.Errors);
    }
}

createProjectsFromTemplates

global static List<pse.CreateProjectFromTemplateService.CreateProjectResponse> createProjectsFromTemplates(List<pse.CreateProjectFromTemplateService.CreateProjectFromProjectRequest> requests)

This method creates new projects from a project template using details provided in the CreateProjectFromTemplateRequest list. The following configuration is used:
• budget-name-suffix
• default-budget-status
• default-budget-type
• default-is-active
• default-time-credited
• default-time-excluded
• default-is-billable

Input Parameters

Name Type Description
requests List<pse.CreateProjectFromTemplateService.CreateProjectFromProjectRequest> The list of CreateProjectFromProjectRequest.

Return Value

This service returns CreateProjectResponses in a list that parallels the input list.

createProjectsFromTemplates

global static List<pse.CreateProjectFromTemplateService.CreateProjectResponse> createProjectsFromTemplates(List<pse.CreateProjectFromTemplateService.CreateProjectFromTemplateAndAccountRequest> requests)

This method creates new projects from a project template using details provided in the requests list. The project will be linked to the account ID passed in the CreateProjectFromTemplateAndAccountRequest. The following configuration is used:
• budget-name-suffix
• default-budget-status
• default-budget-type
• default-is-active
• default-time-credited
• default-time-excluded
• default-is-billable

Input Parameters

Name Type Description
requests List<pse.CreateProjectFromTemplateService.CreateProjectFromTemplateAndAccountRequest> The list of CreateProjectFromTemplateAndAccountRequests.

Return Value

This service returns CreateProjectResponses in a list that parallels the input list.

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.

// Prepare the required fields for the AccountRequest instance to be created.
Date startDateForProject = Date.Today();// Give a start date to populate the project.
Id templateProjectId = Id.valueOf('a1Q000000000000');// Give a valid template project Id to clone the new project.
Id accountId = Id.valueOf('001000000000000');// Give a valid account Id to link to the new project.

// Instantiate the request for create project from template.
pse.CreateProjectFromTemplateService.CreateProjectFromTemplateAndAccountRequest ACC_PROJ_REQ1 =
    new pse.CreateProjectFromTemplateService.CreateProjectFromTemplateAndAccountRequest(accountId, templateProjectId, startDateForProject);

// Call the service to create the projects using the request list and store list of response
List<pse.CreateProjectFromTemplateService.CreateProjectResponse> ACC_PROJ_RESPONSES =
    pse.CreateProjectFromTemplateService.createProjectsFromTemplates(
        new List<pse.CreateProjectFromTemplateService.CreateProjectFromTemplateAndAccountRequest>{ACC_PROJ_REQ1});

// Loop for each response to detect whether all requests were successfully processed.
for(pse.CreateProjectFromTemplateService.CreateProjectResponse response : ACC_PROJ_RESPONSES)
{
    if( response.isSuccess() ) // Get the new project Ids from the response when successfully processed in the request.
    {
        System.debug('Request for this response :' + response.Request);
        System.debug('New project Id for the successful cloned project :' + response.NewProjectId); // All related information can be queried using this project Id.
    }
    else // Get the errors from the response for an unsuccessful request.
    {
        System.debug('Request for this response :' + response.Request);
        System.debug('Errors related to unsuccessful request :' + response.Errors);
    }
}

createProjectsFromTemplates

global static List<pse.CreateProjectFromTemplateService.CreateProjectResponse> createProjectsFromTemplates(List<pse.CreateProjectFromTemplateService.CreateProjectFromTemplateAndOpportunityRequest> requests)

This method creates new projects from a template project using details provided in the CreateProjectFromTemplateAndOpportunityRequest list. The project will be linked to the opportunity ID passed in the CreateProjectFromTemplateAndOpportunityRequest. The following configuration is used:
• project-name-suffix
• budget-name-suffix
• default-budget-status
• default-budget-type
• default-is-active
• default-time-credited
• default-time-excluded
• default-is-billable
• create-project-with-no-opp-update-permission

Input Parameters

Name Type Description
requests List<pse.CreateProjectFromTemplateService.CreateProjectFromTemplateAndOpportunityRequest> The list of CreateProjectFromTemplateAndOpportunityRequest.

Return Value

This service returns CreateProjectResponses in a list that parallels the input list.

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.

// Prepare the required fields for the OpportunityRequest instance to be created.
Date startDateForProject = Date.Today();// Provide a start date to populate the project.
Id templateProjectId = Id.valueOf('a1Q1a0000006VJ4');// Provide a valid template project Id to clone the new project.
Id opportunityId = Id.valueOf('00637000005rwZq');// Provide a valid opportunity Id to link to the new project.

// Instantiate the request.
pse.CreateProjectFromTemplateService.CreateProjectFromTemplateAndOpportunityRequest OPP_PROJ_REQ1 =
    new pse.CreateProjectFromTemplateService.CreateProjectFromTemplateAndOpportunityRequest(opportunityId, templateProjectId, startDateForProject);

// Call the service to create the projects using the request list and store a list of responses.
List<pse.CreateProjectFromTemplateService.CreateProjectResponse> OPP_PROJ_RESPONSES =
    pse.CreateProjectFromTemplateService.createProjectsFromTemplates(
        new List<pse.CreateProjectFromTemplateService.CreateProjectFromTemplateAndOpportunityRequest>{OPP_PROJ_REQ1});

// Loop for each response to detect whether all requests were successfully processed.
for(pse.CreateProjectFromTemplateService.CreateProjectResponse response : OPP_PROJ_RESPONSES)
{
    if( response.isSuccess() ) // Obtain the new project Ids from the response when successfully processed in the request.
    {
        System.debug('Request for this response :' + response.request);
        System.debug('New project Id for the successful cloned project :' + response.NewProjectId); // All related information can be queried using this project Id.
    }
    else // Obtain the errors from the response for an unsuccessful request.
    {
        System.debug('Request for this response :' + response.Request);
        System.debug('Errors related to unsuccessful request :' + response.Errors);
    }
}

pse.CreateProjectFromTemplateService.CreateProjectRequest

global with sharing abstract class CreateProjectRequest

The request structure for the CreateProjectFromTemplateService.

Properties

Name Type Description
IsActive Boolean Indicates if the cloned project is active.
IsTemplate Boolean Indicates if the cloned project is marked as a template.
ProjectName String Indicates the name of the cloned project.
ProjectOwnerId Id The project owner ID.
StartDate Date The intended project start date. This is also used to calculate a date offset value to update other project and related object date fields.
If the Work_Calendar__c field on Project_Task__c sObject has been included in either Project Task field set or passed as a Mapper on this request then Project Tasks will be adjusted into working time after the offset has applied. This will determine which days are working days according to the Work_Calendar__c on each Project Task.
TemplateProjectId Id The template project ID used as a template to create a new project. Any data described by the Mappers must have a relationship with this record (or a sequence of relationships that can be followed to the record) or that data is not copied.
Mappers List<pse.SObjectCloneMapper> The SObjectCloneMapper list defining which fields are copied for which SObjects when creating a new project from a template. This is in addition to the default objects and fields as well as the fields contained in the 'CreateProjFromTemp' field sets.
UseCurrentTimePeriod Boolean Set to true to ensure the cloned project is in the current time period.
CreateBudget Boolean Set to false if you do not want to create a budget.

pse.CreateProjectFromTemplateService.CreateProjectFromTemplateRequest

global with sharing class CreateProjectFromTemplateRequest extends CreateProjectRequest

The request structure for creating projects from templates only. To be used with createProjectsFromTemplates.

This class extends pse.CreateProjectFromTemplateService.CreateProjectRequest

Methods

CreateProjectFromTemplateRequest

global CreateProjectFromTemplateRequest(Id templateProjectId, Date startDate)

A default constructor with minimum required properties.

Input Parameters

Name Type Description
templateProjectId Id The project template ID.
startDate Date The intended project start date.

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 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);

// Prepare the required fields for the ProjectRequest instance to be created.
Id templateProjectId = Id.valueOf('a1Q000000000000');// Give a valid template project Id used to clone the new project.
Date startDateForProject = Date.Today();// Provide a project start date.

pse.CreateProjectFromTemplateService.CreateProjectFromTemplateRequest PROJ_REQ1 =
    new pse.CreateProjectFromTemplateService.CreateProjectFromTemplateRequest(templateProjectId, startDateForProject);
pse.CreateProjectFromTemplateService.CreateProjectFromTemplateRequest PROJ_REQ2 =
    new pse.CreateProjectFromTemplateService.CreateProjectFromTemplateRequest(templateProjectId, startDateForProject);

PROJ_REQ1.IsActive = true;
PROJ_REQ1.IsTemplate = false;
PROJ_REQ1.ProjectName = 'ExampleProjectName';
PROJ_REQ1.ProjectOwnerId = Id.valueOf('0051a000000aeDc');
PROJ_REQ1.Mappers = new List<pse.SObjectCloneMapper> { mapp };

PROJ_REQ2.IsActive = true;
PROJ_REQ2.IsTemplate = false;
PROJ_REQ2.ProjectName = 'ExampleProjectNameSecond';
PROJ_REQ2.ProjectOwnerId = Id.valueOf('0051a000000aeDc');
PROJ_REQ2.Mappers = new List<pse.SObjectCloneMapper> { mapp };

pse.CreateProjectFromTemplateService.CreateProjectFromProjectRequest

global with sharing class CreateProjectFromProjectRequest extends CreateProjectRequest

The request structure for creating projects from any other project. To be used with createProjectsFromTemplates.

This class extends pse.CreateProjectFromTemplateService.CreateProjectRequest

Methods

CreateProjectFromProjectRequest

global CreateProjectFromProjectRequest(Id templateProjectId, Date startDate)

A default constructor with minimum required properties.

Input Parameters

Name Type Description
templateProjectId Id The project template ID.
startDate Date The intended project start date.

pse.CreateProjectFromTemplateService.CreateProjectFromTemplateAndAccountRequest

global with sharing class CreateProjectFromTemplateAndAccountRequest extends CreateProjectRequest

The request structure for creating projects from templates with certain data coming from an account. To be used with createProjectsFromTemplatesAndAccounts.

This class extends pse.CreateProjectFromTemplateService.CreateProjectRequest

Properties

Name Type Description
AccountId Id The account ID.

Methods

CreateProjectFromTemplateAndAccountRequest

global CreateProjectFromTemplateAndAccountRequest(Id accountId, Id templateProjectId, Date startDate)

A default constructor with minimum required properties.

Input Parameters

Name Type Description
accountId Id The account ID.
templateProjectId Id The project template ID.
startDate Date The intended project start date.

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 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);

// Prepare the required fields for the AccountRequest instance to be created.
Account account = [Select Id from Account where Name = 'Customer1']; // Give the name of the account to associate with the new cloned project.
pse__Proj__c proj = [Select Id from pse__Proj__c where pse__Project_ID__c = 'PR-000038']; // Give a valid template project name for the new cloned project.

Id accountId = account.Id;
Id templateProjectId = proj.Id;
Date startDateForProject = Date.Today();// Give a project start date to populate the new cloned project start date.

pse.CreateProjectFromTemplateService.CreateProjectFromTemplateAndAccountRequest ACC_PROJ_REQ1 =
    new pse.CreateProjectFromTemplateService.CreateProjectFromTemplateAndAccountRequest(accountId, templateProjectId, startDateForProject);

ACC_PROJ_REQ1.IsActive = true;
ACC_PROJ_REQ1.IsTemplate = false;
ACC_PROJ_REQ1.ProjectName = 'ExampleProjectName';
ACC_PROJ_REQ1.ProjectOwnerId = Id.valueOf('0051a000000aeDc');
ACC_PROJ_REQ1.Mappers = new List<pse.SObjectCloneMapper> { mapp };

pse.CreateProjectFromTemplateService.CreateProjectFromTemplateAndOpportunityRequest

global with sharing virtual class CreateProjectFromTemplateAndOpportunityRequest extends CreateProjectRequest

The request structure for creating projects from templates with certain data coming from an opportunity. To be used with createProjectsFromTemplatesAndOpportunities.

This class extends pse.CreateProjectFromTemplateService.CreateProjectRequest

Properties

Name Type Description
OpportunityId Id The Opportunity ID.

Methods

CreateProjectFromTemplateAndOpportunityRequest

global CreateProjectFromTemplateAndOpportunityRequest(Id opportunityId, Id templateProjectId, Date startDate)

A default constructor with minimum required properties.

Input Parameters

Name Type Description
opportunityId Id The opportunity ID.
templateProjectId Id The project template ID.
startDate Date The intended project start date.

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 required object fields to be cloned.
// Define the default value to be used for the field. When not defined, the value is copied from the template project.
// When default values are not defined for CurrencyIsoCode, Region, Practice, Group and Account, values are copied from the associated opportunity.
// When a default value is not provided for a Name, the value is generated based on the opportunity name and configuration.
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 SObjectCloneMapper using the prepared set of SObjectCloneMapper.Field for each object.
pse.SObjectCloneMapper cloneMap = new pse.SObjectCloneMapper(pse__Proj__c.SObjectType, setMapperFields);

// Prepare the required fields for the OpportunityRequest instance to be created.
Opportunity useOpportunity = [Select Id from Opportunity where Name = 'Test_Clone_Opportunity']; // Provide the name of the opportunity to associate with the newly cloned project.
pse__Proj__c proj = [Select Id from Proj__c where Project_ID__c = 'PR-000038']; // Provide a valid template project name for the newly cloned project.

Id opportunityId = useOpportunity.Id;
Id templateProjectId = proj.Id;
Date startDateForProject = Date.Today();// Provide a project start date to populate the new cloned project.

pse.CreateProjectFromTemplateService.CreateProjectFromTemplateAndOpportunityRequest OPP_PROJ_REQ1 =
    new pse.CreateProjectFromTemplateService.CreateProjectFromTemplateAndOpportunityRequest(opportunityId, templateProjectId, startDateForProject);

OPP_PROJ_REQ1.IsActive = true;
OPP_PROJ_REQ1.IsTemplate = false;
OPP_PROJ_REQ1.ProjectName = 'ExampleProjectName';
OPP_PROJ_REQ1.ProjectOwnerId = Id.valueOf('0051a000000aeDc');
OPP_PROJ_REQ1.Mappers = new List<pse.SObjectCloneMapper> { cloneMap };

pse.CreateProjectFromTemplateService.CreateProjectFromProjectAndOpportunityRequest

global with sharing class CreateProjectFromProjectAndOpportunityRequest extends CreateProjectFromTemplateAndOpportunityRequest

The request structure for creating projects based on other projects and including data from an opportunity. To be used with createProjectsFromTemplates.

This class extends pse.CreateProjectFromTemplateService.CreateProjectFromTemplateAndOpportunityRequest

Methods

CreateProjectFromProjectAndOpportunityRequest

global CreateProjectFromProjectAndOpportunityRequest(Id opportunityId, Id templateProjectId, Date startDate)

pse.CreateProjectFromTemplateService.CreateProjectResponse

global inherited sharing class CreateProjectResponse

The response structure returned for each request passed to createProjectsFromTemplates.

Properties

Name Type Description
Request pse.CreateProjectFromTemplateService.CreateProjectRequest The request associated with the response of CreateProjectFromTemplateService.
NewProjectId Id The new project ID. This may be set even though isSuccess is false.
Errors List<pse.CreateProjectFromTemplateService.CreateProjectError> Stores error messages that occur when executing methods from CreateProjectFromTemplateService.
QueuedJobId Id The CreateProjectFromTemplateService may spawn a Queueable to complete the work in a later transaction. When this occurs, this is the ID of the AsyncApexJob controlling the work. The record can be queried to monitor its status. When multiple requests are processed, only the QueuedJobId of the first response is set.

Methods

isSuccess

global Boolean isSuccess()

Indicates whether the project was successfully created. Even if isSuccess() is false, it is possible that the project was still created. That can happen, for example, when the there was a problem creating a budget to link to the new project.

pse.CreateProjectFromTemplateService.CreateProjectError

global with sharing class CreateProjectError

Stores error messages that occur when executing methods from CreateProjectFromTemplateService.

Properties

Name Type Description
Message String The error message.
© Copyright 2009–2024 Certinia Inc. All rights reserved. Various trademarks held by their respective owners.