-
Notifications
You must be signed in to change notification settings - Fork 48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add sample code for automated service document creation though invocable action #80
Merged
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ae653d9
Add sample code for automated service document creation though invoca…
yongbozuo 41753e0
Add sample code for automated service document creation though invoca…
yongbozuo 0716ba0
add comment to trigger the pr update
yongbozuo 2795f9e
address comments
yongbozuo dd9c28c
address the comments
yongbozuo 0cc8909
update readme
yongbozuo 7bd6c85
comment out the comments
yongbozuo 70950a1
address comments
yongbozuo 02d998b
update prettier ignore
yongbozuo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions
4
ServiceDocumentSampleComponents/README.md → ServiceDocumentSamples/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
64 changes: 64 additions & 0 deletions
64
...cumentSamples/force-app/main/default/classes/FireCreateServiceDocumentInvocableAction.cls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
public with sharing class FireCreateServiceDocumentInvocableAction { | ||
|
||
private static final String API_VERSION = 'v58.0'; | ||
|
||
private static final String CREATE_SERVICE_DOCUMENT_ACTION_PATH = '/services/data/'+ API_VERSION +'/actions/standard/createServiceDocument'; | ||
|
||
private static final String AUTHORIZATION_HEADER = 'Authorization'; | ||
private static final String AUTHORIZATION_BEARER_PREFIX = 'Bearer '; | ||
|
||
private static final String CONTENT_TYPE_HEADER = 'Content-Type'; | ||
private static final String CONTENT_TYPE_JSON = 'application/json'; | ||
|
||
private static final String ACCEPT_HEADER = 'Accept'; | ||
|
||
/** | ||
* recordId: recordId to generate service document for. WO/WOLI/SA id | ||
* templateId: The ID of Service document template to generate document for. The ID starts with '0M0' | ||
* locale: (Optional) Locale to generate the document in. Example valid formats: 'ru', 'ru_PL' | ||
* title: (Optional) Used to name the document saved | ||
*/ | ||
@future (callout=true) | ||
public static void TriggerCreateServiceDocumentInvocableAction(String recordId, String templateId, String locale, String title) { | ||
|
||
// System.debug('Attempting to queue Create Service Document'); | ||
// System.debug('RecordId: ' + recordId); | ||
// System.debug('TemplateId: ' + templateId); | ||
// System.debug('Locale: ' + locale); | ||
// System.debug('Title: ' + title); | ||
|
||
String sessionId = UserInfo.getSessionId(); | ||
|
||
HttpRequest httpRequest = new HttpRequest(); | ||
|
||
httpRequest.setMethod('POST'); | ||
httpRequest.setEndpoint(URL.getOrgDomainUrl().toExternalForm() + CREATE_SERVICE_DOCUMENT_ACTION_PATH); | ||
httpRequest.setHeader(AUTHORIZATION_HEADER, AUTHORIZATION_BEARER_PREFIX + sessionId); | ||
httpRequest.setHeader(CONTENT_TYPE_HEADER, CONTENT_TYPE_JSON); | ||
httpRequest.setHeader(ACCEPT_HEADER, CONTENT_TYPE_JSON); | ||
|
||
JSONGenerator jg = JSON.createGenerator(false); | ||
jg.writeStartObject(); | ||
jg.writeFieldName('inputs'); | ||
jg.writeStartArray(); | ||
|
||
jg.writeStartObject(); | ||
jg.writeObjectField('recordId', recordId); | ||
jg.writeObjectField('templateId', templateId); | ||
if (locale != null) { | ||
jg.writeObjectField('locale', locale); | ||
} | ||
if (title != null) { | ||
jg.writeObjectField('title', title); | ||
} | ||
jg.writeEndObject(); | ||
jg.writeEndArray(); | ||
jg.writeEndObject(); | ||
|
||
httpRequest.setBody( jg.getAsString() ); | ||
|
||
Http http = new Http(); | ||
HttpResponse response = http.send(httpRequest); | ||
System.debug(response.getBody()); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
...ples/force-app/main/default/classes/FireCreateServiceDocumentInvocableAction.cls-meta.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata"> | ||
<apiVersion>58.0</apiVersion> | ||
<status>Active</status> | ||
</ApexClass> |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
140 changes: 140 additions & 0 deletions
140
ServiceDocumentSamples/force-app/main/default/triggers/ServiceDocumentCreation.trigger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
trigger ServiceDocumentCreation on DocumentRecipient (after insert) { | ||
|
||
//TODO: | ||
//1. based on the number of signatures in your template, add the template id to a set below. | ||
//2. if you have a template with more than 4 signatures | ||
// a. add another Set of templates following the pattern below. | ||
// b. add the corresponding branch in the if-else branch starting at line 56 | ||
//3. example: Set<String> SA_TEMPLATES_WITH_SIGNATURES_3 = new Set<String>{'0M0xx0000004CFUCA2'}; | ||
Set<String> WO_TEMPLATES_WITH_1_SIGNATURES = new Set<String>{''}; | ||
Set<String> WO_TEMPLATES_WITH_2_SIGNATURES = new Set<String>{''}; | ||
Set<String> WO_TEMPLATES_WITH_3_SIGNATURES = new Set<String>{''}; | ||
Set<String> WO_TEMPLATES_WITH_4_SIGNATURES = new Set<String>{''}; | ||
|
||
Set<String> WOLI_TEMPLATES_WITH_1_SIGNATURES = new Set<String>{''}; | ||
Set<String> WOLI_TEMPLATES_WITH_2_SIGNATURES = new Set<String>{''}; | ||
Set<String> WOLI_TEMPLATES_WITH_3_SIGNATURES = new Set<String>{''}; | ||
Set<String> WOLI_TEMPLATES_WITH_4_SIGNATURES = new Set<String>{''}; | ||
|
||
Set<String> SA_TEMPLATES_WITH_1_SIGNATURES = new Set<String>{''}; | ||
Set<String> SA_TEMPLATES_WITH_2_SIGNATURES = new Set<String>{''}; | ||
Set<String> SA_TEMPLATES_WITH_3_SIGNATURES = new Set<String>{''}; | ||
Set<String> SA_TEMPLATES_WITH_4_SIGNATURES = new Set<String>{''}; | ||
|
||
|
||
// TODO: Set threshold for when you want to fire the trigger for when a template not added to an above sets | ||
// Eg: If you set this to 3, then if we cannot match the template being used, we'll automatically | ||
// fire the createServiceDocument invocable action when 3 signatures are collected | ||
final Integer DEFAULT_NUM_SIGNATURES = 3; | ||
|
||
|
||
for (DocumentRecipient dr : Trigger.new) { | ||
ID documentId = dr.DocumentId; | ||
if (documentId.getSobjectType() == Schema.ServiceReport.SObjectType) { | ||
|
||
List<ServiceReport> serviceReports = [SELECT Id, ParentId, DocumentTemplate FROM ServiceReport WHERE Id = :documentId]; | ||
String templateId = serviceReports.get(0).DocumentTemplate; | ||
System.debug('Service Report Identified: ' + documentId); | ||
System.debug('Template Id: ' + templateId); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comment out system debug lines |
||
|
||
ID baseRecordId = serviceReports.get(0).ParentId; | ||
System.debug('Base record Id: ' + baseRecordId); | ||
|
||
//currently already collected signatures | ||
List<DocumentRecipient> collectedSignaturesRelatedDRs = [SELECT Id FROM DocumentRecipient WHERE DocumentId = :documentId]; | ||
System.debug('Number of collected signatures related DocumentRecipients: ' + collectedSignaturesRelatedDRs.size()); | ||
|
||
// There is currently no way to retrieve the number of signatures | ||
// on a template. Thus, for now we need to hardcode associations of | ||
// template IDs and the number of signature records to collect before automatic | ||
// invocation of PDFication. | ||
|
||
// Below, we've provided an example organized by template entity, but more | ||
// if conditions could be added within the switch blocks. If no specific template | ||
// matches, we also have a default condition which will always submit the service | ||
// document once DEFAULT_NUM_SIGNATURES document recipients have been collected. | ||
|
||
|
||
// Work Order Templates | ||
// The if-else branches can be combined by the number of signatures to reduce the number of branches, | ||
// but, in that case, it will not be able to add debug information for each different entity | ||
if (WO_TEMPLATES_WITH_1_SIGNATURES.contains(templateId)) { | ||
System.debug('Work Order Template block'); | ||
//only when all the signatures have been collected, then fire the trigger | ||
if (collectedSignaturesRelatedDRs.size() == 1) { | ||
FireCreateServiceDocumentInvocableAction.TriggerCreateServiceDocumentInvocableAction(baseRecordId, templateId, null, null); | ||
} | ||
} else if (WO_TEMPLATES_WITH_2_SIGNATURES.contains(templateId)) { | ||
System.debug('Work Order Template block'); | ||
if (collectedSignaturesRelatedDRs.size() == 2) { | ||
FireCreateServiceDocumentInvocableAction.TriggerCreateServiceDocumentInvocableAction(baseRecordId, templateId, null, null); | ||
} | ||
} else if (WO_TEMPLATES_WITH_3_SIGNATURES.contains(templateId)) { | ||
System.debug('Work Order Template block'); | ||
if (collectedSignaturesRelatedDRs.size() == 3) { | ||
FireCreateServiceDocumentInvocableAction.TriggerCreateServiceDocumentInvocableAction(baseRecordId, templateId, null, null); | ||
} | ||
} else if (WO_TEMPLATES_WITH_4_SIGNATURES.contains(templateId)) { | ||
System.debug('Work Order Template block'); | ||
if (collectedSignaturesRelatedDRs.size() == 4) { | ||
FireCreateServiceDocumentInvocableAction.TriggerCreateServiceDocumentInvocableAction(baseRecordId, templateId, null, null); | ||
} | ||
} | ||
|
||
|
||
// Work Order Line Item Templates | ||
else if (WOLI_TEMPLATES_WITH_1_SIGNATURES.contains(templateId)) { | ||
System.debug('Work Order Line Item Template block'); | ||
if (collectedSignaturesRelatedDRs.size() == 1) { | ||
FireCreateServiceDocumentInvocableAction.TriggerCreateServiceDocumentInvocableAction(baseRecordId, templateId, null, null); | ||
} | ||
} else if (WOLI_TEMPLATES_WITH_2_SIGNATURES.contains(templateId)) { | ||
System.debug('Work Order Line Item Template block'); | ||
if (collectedSignaturesRelatedDRs.size() == 2) { | ||
FireCreateServiceDocumentInvocableAction.TriggerCreateServiceDocumentInvocableAction(baseRecordId, templateId, null, null); | ||
} | ||
} else if (WOLI_TEMPLATES_WITH_3_SIGNATURES.contains(templateId)) { | ||
System.debug('Work Order Line Item Template block'); | ||
if (collectedSignaturesRelatedDRs.size() == 3) { | ||
FireCreateServiceDocumentInvocableAction.TriggerCreateServiceDocumentInvocableAction(baseRecordId, templateId, null, null); | ||
} | ||
} else if (WOLI_TEMPLATES_WITH_4_SIGNATURES.contains(templateId)) { | ||
System.debug('Work Order Line Item Template block'); | ||
if (collectedSignaturesRelatedDRs.size() == 4) { | ||
FireCreateServiceDocumentInvocableAction.TriggerCreateServiceDocumentInvocableAction(baseRecordId, templateId, null, null); | ||
} | ||
} | ||
|
||
// Service Appointment Templates | ||
else if (SA_TEMPLATES_WITH_1_SIGNATURES.contains(templateId)) { | ||
System.debug('Service Appointment Template block'); | ||
if (collectedSignaturesRelatedDRs.size() == 1) { | ||
FireCreateServiceDocumentInvocableAction.TriggerCreateServiceDocumentInvocableAction(baseRecordId, templateId, null, null); | ||
} | ||
} else if (SA_TEMPLATES_WITH_2_SIGNATURES.contains(templateId)) { | ||
System.debug('Service Appointment Template block'); | ||
if (collectedSignaturesRelatedDRs.size() == 2) { | ||
FireCreateServiceDocumentInvocableAction.TriggerCreateServiceDocumentInvocableAction(baseRecordId, templateId, null, null); | ||
} | ||
} else if (SA_TEMPLATES_WITH_3_SIGNATURES.contains(templateId)) { | ||
System.debug('Service Appointment Template block'); | ||
if (collectedSignaturesRelatedDRs.size() == 3) { | ||
FireCreateServiceDocumentInvocableAction.TriggerCreateServiceDocumentInvocableAction(baseRecordId, templateId, null, null); | ||
} | ||
} else if (SA_TEMPLATES_WITH_4_SIGNATURES.contains(templateId)) { | ||
System.debug('Service Appointment Template block'); | ||
if (collectedSignaturesRelatedDRs.size() == 4) { | ||
FireCreateServiceDocumentInvocableAction.TriggerCreateServiceDocumentInvocableAction(baseRecordId, templateId, null, null); | ||
} | ||
} | ||
|
||
//default case | ||
else { | ||
System.debug('Default block'); | ||
if (collectedSignaturesRelatedDRs.size() == DEFAULT_NUM_SIGNATURES) { | ||
FireCreateServiceDocumentInvocableAction.TriggerCreateServiceDocumentInvocableAction(baseRecordId, templateId, null, null); | ||
} | ||
} | ||
} | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
...eDocumentSamples/force-app/main/default/triggers/ServiceDocumentCreation.trigger-meta.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?xml version='1.0' encoding='UTF-8'?> | ||
<ApexTrigger xmlns="http://soap.sforce.com/2006/04/metadata"> | ||
<apiVersion>58.0</apiVersion> | ||
<status>Active</status> | ||
</ApexTrigger> |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel we can rephrase the wording here.
What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update.