salesforce - 如何测试 Salesforce Prod 部署的 contentdocumentlink 触发器
问题描述
我正在尝试部署触发器来刺激销售人员。我希望有人可以帮助我提供一个测试这个触发器的例子。
这是我的触发器。它的目的是在新的 contentNote(或任何内容类型)通过流程构建器产生附带影响时更新 bool 字段。
trigger NewNote on ContentDocumentLink (before insert) {
Set<Id> setParentId = new Set<Id>();
List<Client_Relationships__c> crlst = new List<Client_Relationships__c>();
for (ContentDocumentLink cdl : trigger.new ) {
setParentId.add(cdl.LinkedEntityId);
}
crlst = [select Id , newNote__c from Client_Relationships__c where Id IN :setParentId];
For(Client_Relationships__c e : crlst)
{
e.newNote__c = True;
}
update crlst;
}
解决方案
您编写的触发器可以通过省略 SOQL 查询来提高效率,如下所示:
trigger NewNote on ContentDocumentLink (before insert) {
List<Client_Relationships__c> crlst = new List<Client_Relationships__c>();
for (ContentDocumentLink cdl : trigger.new ) {
if(cdl.LinkedEntityId.getSObjectType().getDescribe().getName() == 'Client_Relationships__c'){
crlst.add(
new Client_Relationships__c(
Id = cdl.LinkedEntityId,
newNote__c = true
)
);
}
}
update crlst;
}
最佳实践是将您的代码添加到处理程序或实用程序类,并且每个对象只有一个触发器。如果您采用这种做法,可以将此触发器的名称更改为“ContentDocumentLinkTrigger”。
该触发器的测试类如下。我无法测试编译,因为我没有相同的自定义对象。
@IsTest
private class ContentDocumentLinkTriggerTest {
@TestSetup
static void setupTest() {
insert new ContentVersion(
Title = 'Test_Document.txt',
VersionData = Blob.valueOf('This is my file body.'),
SharingPrivacy = 'N',
SharingOption = 'A',
Origin = 'H',
PathOnClient = '/Test_Document.txt'
);
List<Client_Relationships__c> relationships = new List<Client_Relationships__c>();
for(Integer i = 0; i < 300; i++){
relationships.add(
new Client_Relationships__c(
//add required field names and values
)
);
}
insert relationships;
}
static testMethod void testInsertTrigger() {
//prepare data
List<ContentVersion> contentVersions = new List<ContentVersion>([
SELECT Id, ContentDocumentId FROM ContentVersion
]);
System.assertNotEquals(0, contentVersions.size(), 'ContentVersion records should have been retrieved');
List<Client_Relationships__c> relationships = getAllClientRelationships();
System.assertNotEquals(0, relationships.size(), 'Client Relationship records should have been retrieved.');
List<ContentDocumentLink> documentLinks = new List<ContentDocumentLink>();
for(Integer i = 0; i < 252; i++){
documentLinks.add(
new ContentDocumentLink(
ContentDocumentId = contentVersions[0].ContentDocumentId,
LinkedEntityId = relationships[i].Id,
ShareType = 'I'
)
);
}
//test functionality
Test.startTest();
insert documentLinks;
Test.stopTest();
//assert expected results
List<Client_Relationships__c> relationshipsAfterProcessing = getAllClientRelationships();
for(Client_Relationships__c relationship : relationshipsAfterProcessing){
System.assert(relationship.newNote__c, 'The newNote__c field value should be true.');
}
}
private static List<Client_Relationships__c> getAllClientRelationships(){
return new List<Client_Relationships__c>([
SELECT Id, newNote__c FROM Client_Relationship__c
]);
}
}
对于设置测试数据,有一个实用程序类来集中创建格式良好的记录是很有帮助的。当您的代码库变得很大并且验证规则会影响在许多测试类中插入新数据时,这非常有用。使用集中式方法,插入的数据只需更改一次。
推荐阅读
- c# - 如何计算数组中数字的频率?
- kotlin - 在 Kotlin 中何时使用参数与何时使用成员变量?
- laravel - 如何在 laravel 5.7 中使用 Crinsane Laravel 购物车?
- amazon-web-services - AWS Waiter TasksStopped 失败:taskId 长度应为以下之一
- haxe - 导入我的开发 haxelib 时无法解析符号
- javascript - 点击时减去选项本身的值
- ruby-on-rails - Heroku:PG :: ConnectionBad:无法连接到服务器
- java - spring 如何使用@Resource 注册一个bean
- javascript - 如何将 javascript 提要数据传输到 php 变量?
- javascript - [React Native]:找不到变量:DeviceEventEmitter