...
For performing custom calculations in our Campaign Builder built-in -built features like Salesforce formulas and the campaign builder layout settings may not suffice in certain use cases.
...
Note |
---|
Please take note that you will need technical knowledge to implement this in your ADvendio instance. |
Table of Contents |
---|
When do I need to extend the campaign builder with a custom script?
Problem | Pre-Installed Campaign Builder | Custom JavaScript |
---|---|---|
Fast and configurable campaign item editing | ✅ | ✅ |
Conditional tab and field visibilities | ✅ | ✅ |
✅ | ✅ | |
Performing SOQL queries to fetch data | ❌ | ✅ |
Complex calculations in JavaScript | ❌ | ✅ |
...
The campaignBuilder
is the API to interact with the campaign builder. You can listen on field changes, get field values, and set field values.
...
Available methods
...
Methode name | Description | Example | ||
---|---|---|---|---|
| Listen on changes on a specific field. You can retrieve the record where the change happened as well as the old (from) and the new value (to) and if this is a parent record (package header). | |||
| Set a specific value directly in the record object based on the field path.
| |||
| Get a specific value from the record object based on the field path. | |||
| Run a SOQL query by parameters to query data from records that are not included in the Campaign Builder. To prevent security vulnerabilities, the | |||
| Ask if the record has child records as package components. If yes, you can react and perform other actions. | |||
| Use this method to iterate through children records of a parent item. | |||
| Allows to create a function that will be called when every record is loaded in the campaign builder. After that it allows the user to execute code right after all the records are loaded into the Campaign Builder, allowing to modify data, execute queries, copy values, etc. | |||
| Allows to apply changes to a list of previously queried Campaign Builder items.
| |||
| Set a custom message for an specific field based on the fieldpath and the recordId. | - | ||
| Return true if the field has | - | ||
| Return all records of a related field inside a children record, if the fields inside are not a related field it will return an empty array | - | ||
| Return all the records of a related field if it’s not a related field it will return an empty array | - | ||
| Return an array with all the fields that has | - |
...
Examples
Discount limiting example
...
Code Block |
---|
window.advendioCampaignBuilder = campaignBuilder => { // 1. Listen on changes on discount__c field campaignBuilder.on("advendio__rate_discount_4__c", async ({ recordId, from, to, record }) => { if (Number(to) > 10) { campaignBuilder.updateFieldFromRecord(record, "advendio__rate_discount_4__c", Math.min(to, 10)); } }); }; |
Querying
...
To query data from records that are not included in the campaign builder, you can use the campaignBuilder.query
method.
...
Accounts
Imagine a case where you would like to request the id of an Account based on a name search.
Code Block | ||
---|---|---|
| ||
window.advendioCampaignBuilder = async (campaignBuilder) => { const result = await campaignBuilder.query({ objectName: 'Account', fields: 'Name', limit: 5, offset: 0, conditions: [{ fieldName: "Name", condition: "!=", value: "Acme", scapeQuotes: true }] }); console.log(result); // array of accounts }; |
Editing Children Records
In the following example, every time you modify the advendio__quantity__c
field in a record with subItems, it will multiply the value of every children child frequency by 2.
If the record doesn’t have children records, it will not modify the frequency value.
Code Block | ||
---|---|---|
| ||
window.advendioCampaignBuilder = campaignBuilder => { campaignBuilder.on("advendio__quantity__c", ({ recordId, from, to, record }) => { if (campaignBuilder.hasChildRecord(record)) { let childrenRecord = campaignBuilder.getChildRecords(record); for (let child of childrenRecord) { campaignBuilder.updateFieldFromRecord(child, "advendio__frequency__c", campaignBuilder.getFieldFromRecord(child, "advendio__frequency__c") * 2); } console.log('All children modified'); } }); }; |
Editing Parent Records
In the following example, every time you modify the advendio__linedescription__c
field in a child record, it will change the value on the parent field and will add a (modified from children)
text.
Code Block | ||
---|---|---|
| ||
window.advendioCampaignBuilder = campaignBuilder => { campaignBuilder.on("advendio__linedescription__c", ({ recordId, from, to, record, parent }) => { if (parent != null) { campaignBuilder.updateFieldFromRecord(parent, "advendio__linedescription__c", to + " (modified from children)"); } }); }; |
...
Editing Values
The following example will showcase the onDocumentReady method, that allows the user to execute code right after all the records are loaded into the Campaign Builder, allowing to modify data, execute queries, copy values, etc.
...
which will update certain fields in the Campaign Builders items.
Code Block |
---|
window.advendioCampaignBuilder = campaignBuilder => { campaignBuilder.onDocumentReady(({ records }) => { console.log('On Document ready'); campaignBuilder.updateFieldFromRecord(records[0], "advendio__from_date__c", "2022-01-04"); campaignBuilder.updateFieldFromRecord(records[0], "advendio__until_date__c", "2022-01-23"); campaignBuilder.updateFieldFromRecord(records[0], "advendio__quantity__c", 10); campaignBuilder.updateRecords(records); console.log(records); }); }; |
Note |
---|
Important: You should always call the method |
Available methods
onDocumentReady(callback)
Allows to create a function that will be called when every record is loaded in the campaign builder.
updateRecords(records)
Update records values
updateFieldFromRecord(record, fieldpath, value)
Note |
---|
If you update a field that is involved in the amount calculation process, the field value can be changed after this operation because amount calculation process has highest priority over custom javascript methods. |
Set a specific value directly in the record object based on the field path.
getFieldFromRecord(record, fieldpath)
Set a specific value directly in the record object based on the field path.
setFieldError(recordId, fieldpath, message)
Set a custom message for an specific field based on the fieldpath and the recordId.
async query({objectName,fields,limit,offset,conditions = [],orderByField,orderByDirection}
Run a SOQL query by parameters.
hasChildRecord(record)
Return true if the record has sub items otherwise return false
getChildRecords(record)
Return all the child record (sub items) of a record, if the record doesn’t have child records it will return an empty array
isRelatedField(record, fieldpath)
Return true if the field has isRelated
true
getRecordsFromRelatedFields(record, fieldpath)
Return all records of a related field inside a children record, if the fields inside are not a related field it will return an empty array
getRecordsFromRelatedField(record, fieldpath)
Return all the records of a related field if it’s not a related field it will return an empty array
getAllRelatedFieldsFromRecord(record)
Return an array with all the fields that has isRelated
true in the record
setFieldAsRequired(recordId, field, value)
Will set the indicated field as required or not depending on the value (true or false)
setFieldAsReadOnly(recordId, field, value)
Will set the indicated field as read only or not depending on the value (true or false)
setFieldAsHidden(recordId, field, value)
Will set the indicated field as hidden or not depending on the value (true or false)
...
...
Use your custom script
Once you have created your static resource js, you can upload it to your org via Setup > Static Resources > New.
...