如何测试Salesforce产品部署的contentdocumentlink触发器

如何测试Salesforce产品部署的contentdocumentlink触发器,salesforce,apex,salesforce-lightning,salesforce-service-cloud,salesforce-communities,Salesforce,Apex,Salesforce Lightning,Salesforce Service Cloud,Salesforce Communities,我正在尝试部署一个触发器来刺激salesforce。我希望有人能帮我举一个测试这个触发器的例子 这是我的扳机。它的目的是,当一个新的contentNote(或任何内容类型)通过process builder产生附带影响时,更新bool字段 trigger NewNote on ContentDocumentLink (before insert) { Set<Id> setParentId = new Set<Id>(); List<Client_Relations

我正在尝试部署一个触发器来刺激salesforce。我希望有人能帮我举一个测试这个触发器的例子

这是我的扳机。它的目的是,当一个新的contentNote(或任何内容类型)通过process builder产生附带影响时,更新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;
} 
在ContentDocumentLink上触发新注释(插入前){
Set setParentId=new Set();
List crlst=新列表();
for(ContentDocumentLink cdl:trigger.new){
setParentId.add(cdl.LinkedEntityId);
}
crlst=[从客户关系中选择Id,newNote_uuuuuuuc,其中Id位于:setParentId];
用于(客户关系:crlst)
{
e、 newNote__c=真;
}
更新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;
} 
在ContentDocumentLink上触发新注释(插入前){
List crlst=新列表();
for(ContentDocumentLink cdl:trigger.new){
if(cdl.LinkedEntityId.getSObjectType().GetDescripte().getName()=='Client\u Relationships\uuu c'){
crlst.add(
新客户关系(
Id=cdl.LinkedEntityId,
newNote__c=真
)
);
}
}
更新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
        ]);
    }
}
@IsTest
私有类ContentDocumentLinkTriggerTest{
@测试设置
静态孔隙测试(){
插入新内容版本(
Title='Test_Document.txt',
VersionData=Blob.valueOf('这是我的文件体'),
共享隐私='N',
SharingOption='A',
原点='H',
PathOnClient='/Test_Document.txt'
);
列表关系=新列表();
对于(整数i=0;i<300;i++){
关系。添加(
新客户关系(
//添加必需的字段名和值
)
);
}
插入关系;
}
静态测试方法void testInsertTrigger(){
//准备数据
列表内容版本=新列表([
从ContentVersion中选择Id、ContentDocumentId
]);
System.assertNotEquals(0,contentVersions.size(),“应检索ContentVersion记录”);
列表关系=getAllClientRelationships();
System.assertNotEquals(0,relationships.size(),“应已检索到客户端关系记录”);
List documentLinks=新列表();
对于(整数i=0;i<252;i++){
documentLinks.add(
新ContentDocumentLink(
ContentDocumentId=contentVersions[0]。ContentDocumentId,
LinkedEntityId=关系[i].Id,
ShareType='I'
)
);
}
//测试功能
Test.startTest();
插入文档链接;
Test.stopTest();
//断言预期结果
List relationshipsAfterProcessing=getAllClientRelationships();
for(客户关系:关系shipsafterprocessing){
assert(relationship.newNote__________c,'newNote_____c字段值应为true');
}
}
私有静态列表getAllClientRelationships(){
返回新列表([
从客户关系中选择Id、新注释
]);
}
}

对于设置测试数据,使用一个实用程序类来集中创建格式良好的记录是很有帮助的。当代码库变大并且验证规则影响在许多测试类中插入新数据时,这非常有用。使用集中式方法,插入的数据只需更改一次。

非常感谢您的帮助!我在触发器上遇到了问题,我试图绕过这个问题,并用其他方法对对象进行类型检查,但我没有找到解决方法。对于原始行,我得到的错误是“比较参数必须是兼容的类型:Schema.SObjectType,String”。然后我在测试中发现了三个错误。其中两个是“不能从静态上下文引用非静态方法:List ContentDocumentLinkTriggerTest.getAllClientRelationships()”,另一个是“从客户端关系到Id的非法分配”,我希望你能帮我解决这个问题?!我更新了代码。当我在电脑前的时候,我必须再修正一个。触发器中的比较现在也应该修正了。