【问题标题】:How to write Test class to test a field on an attachment or file insert?如何编写测试类来测试附件或文件插入上的字段?
【发布时间】:2020-05-02 04:50:21
【问题描述】:

下面是触发器更新附件或文件插入的字段,有人可以为这个触发器建议一个测试类吗?

请有人帮忙为下面的触发器编写一个@isTest 类来实现 100% 的代码完成

trigger ContentDocumentLinkTrigger on ContentDocumentLink (after insert) {
if(trigger.isAfter) {
    if(trigger.isInsert) {
        ContentDocumentLinkTriggerHandler.onAfterInsert(trigger.new);
    }
}
}

这是课程

            public  class ContentDocumentLinkTriggerHandler {

            public static void onAfterInsert(list<ContentDocumentLink> lstCntLinks) {
            set<Id> setTaskIds = new set<Id>();
            list<task> lstTask=new list<task>();
            Id recordTypeId = Schema.SObjectType.Task.getRecordTypeInfosByName().get('ERT_Task_Record_Type').getRecordTypeId();

            for(ContentDocumentLink clIterator : lstCntLinks) {  
            setTaskIds.add(clIterator.LinkedEntityId);//Set all task id            
            }

            if(!setTaskIds.isEmpty()) {
                    lstTask= [SELECT Id, Name,Attachment_Indicator__c  FROM task WHERE Id IN :setTaskIds and recordtypeid=: recordTypeId ]; //Get all the task 
                }

            for(task varTsk:  lstTask){
            varTsk.Attachment_Indicator__c=true;//Set Attachment Indicator Flag true

            }
            if(lstTask.size()>0){
            update lstTask; //update task
             }
            }
            }

提前致谢。 卡罗琳

【问题讨论】:

    标签: salesforce salesforce-lightning


    【解决方案1】:

    我们并不是真正的代码编写服务,您可能想聘请一些顾问,甚至在https://appexchange.salesforce.com/developers 上发布工作(应该由 SF 自己审核,个人没有使用过 appexchange 的那部分)。如果你有特定的问题,写了一些代码,遇到了特定的问题,那就不同了......

    这应该可以让您了解如何攻击它。没有承诺它甚至会编译。它不是像素完美的,没有负面测试用例(链接到不同记录类型的任务不应设置标志等)

    但这只是一个开始。

    @isTest
    public with sharing class ContentDocumentLinkTriggerTest {
        @isTest
        public static void testSettingTaskFlag(){
            Task t = new Task(
                Subject = 'unit test here we go',
                RecordTypeId = Schema.SObjectType.Task.getRecordTypeInfosByName().get('ERT_Task_Record_Type').getRecordTypeId()
            );
            insert t;
    
            ContentVersion cv = new ContentVersion(
                Title = 'Some document',
                PathOnClient = 'some document.txt',
                VersionData = Blob.valueOf('Lorem ipsum dolor sit amet...')
            );
            insert cv;
    
            ContentDocument cd = [SELECT Id FROM ContentDocument WHERE LatestPublishedVersionId = :cv.Id];
            ContentDocumentLink cdl = new ContentDocumentLink(
                ContentDocumentId = cd.Id,
                LinkedEntityId = t.Id
            );
    
            Test.startTest();
            insert cdl;
            Test.stopTest();
    
            Task newTask = [SELECT Attachment_Indicator__c FROM Task WHERE Id = :t.Id];
            System.assert(newTask.Attachment_Indicator__c);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-02-24
      • 2010-10-31
      • 1970-01-01
      • 1970-01-01
      • 2011-04-25
      • 1970-01-01
      • 2020-11-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多