This repository has been archived by the owner on Feb 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
updateCaseSalesForce.json
30 lines (30 loc) · 2.9 KB
/
updateCaseSalesForce.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
{
"name": "updateCaseSalesForce",
"version": "0.0.20",
"type": "actuator",
"script": "const jsforce = require('jsforce')\nconst pify = require('pify')\nconst Waylay = require('@waylay/client')\nconst _ = require('lodash')\n\nasync function main () {\n const caseObject = cleanObject({\n Id: options.requiredProperties.Id,\n Subject: options.requiredProperties.Subject,\n Description: options.requiredProperties.Description,\n Status: options.requiredProperties.Status,\n Priority: options.requiredProperties.Priority,\n AssetId: options.requiredProperties.AssetId,\n AccountId: options.requiredProperties.AccountId,\n Reason: options.requiredProperties.Reason,\n OwnerId: options.requiredProperties.OwnerId,\n Product__c: options.requiredProperties.Product__c\n })\n\n const comment = options.requiredProperties.Comment\n\n const conn = await getSalesforceConnection()\n\n const SfCaseComment = pify(conn.sobject('CaseComment'))\n const SfCase = pify(conn.sobject('Case'))\n\n let res\n\n if (comment) {\n console.log('add only comment')\n res = await SfCaseComment.insert({ CommentBody: comment, ParentId: caseObject.Id })\n } else {\n res = await SfCase.update(caseObject)\n }\n\n send(null, { message: JSON.stringify(res) })\n}\n\nasync function getSalesforceConnection () {\n try {\n const waylayClient = new Waylay({\n token: options.globalSettings.waylayToken\n })\n\n const { access_token: accessToken } = await waylayClient.hoard.getTokens('salesforce', { scope: 'organisation' })\n\n if (!accessToken) {\n throw new Error('No user or token provided via Hoard service')\n }\n\n const jsclient = new jsforce.Connection({\n instanceUrl: 'https://eu26.salesforce.com',\n accessToken\n })\n\n return jsclient\n } catch (error) {\n throw new Error(`error: ${error} \\n possible solution: Connect Salesforce through the connections`)\n }\n}\n\n/**\n * remove empty values in object before sending to salesforce\n */\nfunction cleanObject (object) {\n return _.reduce(object, (acc, v, k) => {\n if (v) acc[k] = v\n return acc\n }, {})\n}\n\nmain().catch(send)\n",
"metadata": {
"author": "",
"iconURL": "https://static.waylay.io/plugs/icons/salesforce-update-case.png",
"description": "Add case in SalesForce, all fields are optional except Id.\nIf accountId is not provided, it will be using the account\nof the person to who belongs the API token.\nIf AssetId it will take waylay resource as AssetId.\nIf comment field is not empty, it will only update the comment field of the case.",
"rawData": [],
"requiredProperties": [
"Subject",
"Status",
"Priority",
"AssetId",
"AccountId",
"Reason",
"OwnerId",
"Product__c",
"Id",
"Comment"
],
"supportedStates": []
},
"dependencies": {
"jsforce": "1.10.0",
"pify": "5.0.0",
"@waylay/client": "2.20.0"
}
}