function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
vinni1vinni1 

test coverage Help

Hi All,

 

  Req: if we update task field  under lead , Lead date field get update

 

   Trig::  It works perfectly

 

      trigger TaskAfterInsertUpdate on Task (after update, after insert){
    list<Lead> leads = new list<Lead>();
    list<id> alliIDs = new list<id>();
    datetime myDateTime = datetime.now();
    
    Id taskRecordTypeId = Schema.SObjectType.Task.getRecordTypeInfosByName().get('Old One').getRecordTypeId();

    for(Task sTask : trigger.new)
    {
        if (sTask.WhoId <> NULL && sTask.RecordTypeid == taskRecordTypeId && string.valueOf(sTask.WhoId).startsWith('00Q'))
        {
            alliIDs.add(sTask.Whoid);
        }
    }

    Map<Id,Lead> leadmap = new Map<Id, Lead>([select Id,BU__c,Last_Date__c,CreatedDate,LeadSource,Country,Status,DonotCall,Mobile__c ,Phone__c from Lead where Id in : alliIDs]);
    Map<Id,Lead> allMap = new Map<Id, Lead>();

    for(Lead lead : leadmap.Values()){
        idcreateddateMap.put(lead.Id,lead);
    }

    for(Task tsk :Trigger.new){
        if (tsk.WhoId <> NULL && tsk.RecordTypeid == taskRecordTypeId && string.valueOf(tsk.WhoId).startsWith('00Q')){
            
                    if(allMap .get(tsk.WhoId).CreatedDate.Date().daysBetween(myDateTime.Date())>10& allMap .get(tsk.WhoId).Status <> 'Open' && allMap .get(tsk.WhoId).LeadSource == 'All Data' &&(tsk.Outcome__c == 'Back Me' || tsk.Outcome__c=='ywefuw' || tsk.Outcome__c=='My all' || tsk.Outcome__c == 'Get one') && tsk.Type == 'Out'){
                        idcreateddateMap.get(tsk.WhoId).Last_Date__c = NULL;   

                    }
                    
                    leads.add(idcreateddateMap.get(t.WhoId));

              
        }
    }
    update leads;
}

 

 

Test Class::  but it's not cover fully

 

@isTest
public class TestTaskAfterInsertUpdate
{
  static testMethod void TestTaskAfterInsertUpdate()
  {
     task t = new task(Subject='Call',Type='Outbound Call',Status='Not Started',Priority='Normal');
     insert t;
  }
  }
 

 

Best Answer chosen by Admin (Salesforce Developers) 
asish1989asish1989

Try this test class

@isTest
public class TestTaskAfterInsertUpdate
{
  static testMethod void TestTaskAfterInsertUpdate()
  {  
      Lead ld = new Lead(Name = 'test' , Status = 'Working - Contacted',Company = 'testcompany');
	  insert ld;
      RecordType rt = [SELECT id,Name 
                             FROM RecordType 
                             WHERE SobjectType='Task ' AND Name='Old One '];
     task t = new task(Subject='Call',Type='Outbound Call',Status='Not Started',
	                     Priority='Normal',recordTypeId = rt.id , WhoId = ld.id);
     insert t;
  }
  }

 For record type coverage go through this post of mine.

http://salesforceworld4u.blogspot.in/search/label/Record%20type%20for%20creating%20test%20class%20data%20in%20Apex%20Salesforce