+ Start a Discussion

Test Class Assist Please on working Trigger

Someone was kind enough to help me work through to get the following trigger, now I need some help building test coverage so I can deploy. thanks.



trigger NextTaskInfo on Task (after insert, after update) {

  if(Trigger.new.size() == 1 ) {

    Task tk = Trigger.New[0];
    String str = tk.whatid;
    if(str != null && str.substring(0,3)== '006')

         Opportunity opp = [select OwnerId,NextTaskSubject__c,Next_Task_Date__c from Opportunity where Id = :tk.WhatId ];

        List<Task> tskMin = [Select ActivityDate,Subject From Task where whatid=:tk.whatid and  what.type = 'Opportunity' and isClosed  = false order By ActivityDate limit 1];  

        if (tskMin.size()>0) {
        else {
        update opp;







Below is your test class code. It will give you 100% test coverage.


private class TestClassTaskTrigger {

    static testMethod void testDiscussionBoardTrigger() {
        Opportunity testOpp = new Opportunity( Name ='TestOpp', StageName ='Prospecting', CloseDate = System.today()+60);
        insert testOpp;
        System.assert(testOpp != null);
        Task testTask = new Task (WhatId = testOpp.Id,Subject = 'Other', Status ='In Progress');
        insert testTask;
        System.assert(testTask != null);
        Task edTask = [Select t.WhatId, t.Subject, t.Status From Task t where t.Id=:testTask.Id];
        edTask.Status = 'Completed';
        update edTask;


thanks, this seems to be working, but I cannot deploy b/c I have two existing triggers in production that i lack test coverage for.  

I appreciate the help. I am an admin who trying to expand my powers to include developer/coding... 



trigger AccountTeamCreator on Account (after insert) {
     AccountTeamMember[] newmembers = new AccountTeamMember[]{};  //list of new team members to add
     AccountShare[] newShare = new AccountShare[]{};  //list of new shares to add
     User currentuser =  [select id,name,department from user where id=:UserInfo.getUserId() LIMIT 1];  //get the user running the trigger, anyone that changes the Account will added to the account team
    ID uid=currentuser.id;
    String dept=currentuser.Department;
     for(Account a:trigger.new){
          AccountTeamMember Teammemberad=new AccountTeamMember();
          Teammemberad.TeamMemberRole = dept;
     Database.SaveResult[] lsr = Database.insert(newmembers,false);//insert any valid members then add their share entry if they were successfully added
     Integer newcnt=0;
     for(Database.SaveResult sr:lsr){
        Database.Error emsg =sr.getErrors()[0];
        system.debug('\n\nERROR ADDING TEAM MEMBER:'+emsg);
        newShare.add(new AccountShare(UserOrGroupId=newmembers[newcnt].UserId, AccountId=newmembers[newcnt].Accountid, AccountAccessLevel='Full',OpportunityAccessLevel='Read'));
     Database.SaveResult[] lsr0 =Database.insert(newShare,false); //insert the new shares
     Integer newcnt0=0;
     for(Database.SaveResult sr0:lsr0){
         Database.Error emsg0=sr0.getErrors()[0];
         system.debug('\n\nERROR ADDING SHARING:'+newShare[newcnt0]+'::'+emsg0);



trigger convertdoctor on Custom_Object__c (after insert) {
    for(Integer g=0; g<Trigger.new.size(); g++)


        string docname = Trigger.new[g].OwnerId;
        string lname = Trigger.new[g].Id;


        PM_Visit__c a = new PM_Visit__c(PM__c= docname, Custom_Object__c = lname);

        Database.SaveResult[] lsr = Database.insert(new PM_Visit__c[]{a},false);

        // Iterate through the Save Results 

        for(Database.SaveResult sr:lsr){

           if(!sr.isSuccess())Database.Error err = sr.getErrors()[0];