Skip to content

Instantly share code, notes, and snippets.

@sjurgis
Last active January 4, 2022 20:05
Show Gist options
  • Save sjurgis/f10f3f22db83c36ec28eef75b1c2f20b to your computer and use it in GitHub Desktop.
Save sjurgis/f10f3f22db83c36ec28eef75b1c2f20b to your computer and use it in GitHub Desktop.
a sample Salesforce org service using sfdx-node
const sfdx = require('sfdx-node/parallel')
const fs = require('fs')
const temp = require('temp');
temp.track();
const defaultUsers = [
'Integration User',
'User User',
'Security User'
]
module.exports = {
async getLoginUrl(isQa) {
let params = {urlonly: true}
if (isQa) {
params.targetusername = 'qa-user'
}
return sfdx.org.open(params)
.then(org => {
return org.url
})
},
query(query) {
return sfdx.data.soqlQuery({query: query})
.then(result => result && result.records)
},
import(sobjects) {
const filePath = temp.openSync({suffix: '.json'}).path
fs.writeFileSync(filePath, JSON.stringify({"records": sobjects}))
return sfdx.data.treeImport({
sobjecttreefiles: filePath,
_rejectOnError: true
}).map(i => ({...i, Id: i.id}))
},
insertRandomLeads(count, company, fields) {
const sobjects = new Array(count).fill({}).map((i, n) => {
return {
...fields,
attributes: {
"type": "Lead",
"referenceId": "Lead" + n
},
LastName: 'RandomName ' + n,
Company: company
}
})
return this.import(sobjects)
},
async insertGroups(count, name, groupMemberNames) {
await this.executeApex(`
Group[] groups = new Group[]{};
for(Integer i = 0; i < ${count}; i++){
Group test = new Group();
test.Name = '${name}'+i;
groups.add(test);
}
insert groups;
User[] users = [select id from user WHERE IsActive = true
AND (name = 'User User' or name ='Integration User' or name = 'Security User')];
GroupMember[] members = new GroupMember[]{};
for(Group i: groups){
for(User j: users){
members.add(new GroupMember(
GroupId = i.Id,
UserOrGroupId = j.Id
));
}
}
insert members;
`)
return this.query(`
SELECT
CreatedById,
CreatedDate,
DeveloperName,
DoesIncludeBosses,
DoesSendEmailToMembers,
Email,
Id,
LastModifiedById,
LastModifiedDate,
Name,
OwnerId,
RelatedId,
SystemModstamp,
Type
FROM Group
WHERE Name LIKE '${name}%'
`)
},
insertQueues(count, name) {
const sobjects = new Array(count).fill({}).map((i, n) => {
return {
attributes: {
"type": "Group",
"referenceId": "Queue" + n
},
Name: name,
Type: 'Queue'
}
})
return this.import(sobjects)
},
deleteByClause(sObjectType, whereClause) {
return sfdx.data.recordDelete({
sObjectType: sObjectType,
where: whereClause
});
},
delete(objectsOrIds) {
return this.executeApex(`
Database.delete(new List<Id> {'${objectsOrIds.map(i => i.Id || i).join('\',\'')}'});
`)
},
executeApex(apexBody) {
const filePath = temp.openSync({suffix: '.apex'}).path
fs.writeFileSync(filePath, apexBody)
return sfdx.apex.execute({
apexcodefile: filePath,
loglevel: 'error',
_rejectOnError: true
})
},
executeApexFile(filename) {
return sfdx.apex.execute({
apexcodefile: filename,
loglevel: 'error',
_rejectOnError: true
})
.then(this.parsePolicyLog)
},
parsePolicyLog(result) {
if (result) {
return JSON.parse(result.logs.match(/(?<=RETURN_VALUE_).*?(?=_RETURN_VALUE)/gs)[1])
}
},
getDefaultUsers() {
return this.query(`
SELECT
Id,
Username,
Name
FROM
User
WHERE
Name = '${defaultUsers.join('\' OR Name=\'')}'
`)
},
getSampleAccounts() {
return this.executeApex(`
Account[] accounts = [SELECT Id FROM Account LIMIT 2];
System.debug(LoggingLevel.ERROR, 'RETURN_VALUE_' + JSON.serialize(accounts) + '_RETURN_VALUE');
`)
.then(this.parsePolicyLog)
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment