I have a opp which contain a task. My scenario is when task is closed on opp update with new task from a lookup field temp on opp(this lookup to another opp) . I have wrote a test class when task is getting closed create new task if any task exist with the lookup opp create the task on the opp else dont do any thing. I created opp with task and close the task by status = completed but even then it is not working. Please guide me the best way to get the solution.

static testmethod void test_createTaskOnUpdate1(){

    account acct = new account(Name = 'sha');
    insert acct;

    String strRecordTypeId = [Select Id From RecordType Where SobjectType = 'Opportunity' and Name = 'Template'].Id;

    Opportunity opp = new Opportunity(accountid=acct.id,Name = 'TRN-0100438691', StageName = 'Cancelled',RecordTypeid=strRecordTypeId, CloseDate=date.today());
    insert opp;

    Task tk=new Task(whatid=opp.id, Subject='Send Quote',Ownerid=UserInfo.getUserId(),Task_Order__c=1,Due_Date_Offset__c=11,Status='In Progress',Priority='High');
    insert tk;

    Opportunity opp1 = new Opportunity(CloseDate = Date.today(), accountid=acct.id,Name = 'test', StageName = 'In Progress', RecordTypeid=strRecordTypeId,Template__c=opp.id);
    insert opp1;

    List<task> task_lst = new  List<task>();
    task_lst = [SELECT id,Task_Order__c, Status  FROM task WHERE whatid=:opp1.id];                     
    task_lst[0].Status = 'Completed';
    upsert task_lst;
    system.debug(' checking a task 1 '+task_lst );

    List<task> task_lst2 = new  List<task>();
    task_lst2 = [SELECT id,Task_Order__c, Status  FROM task WHERE whatid=:opp1.id];
    system.debug(' checking task 2 '+task_lst2 ); 
    system.assertEquals(task_lst2.size(), 0);


the issue is the when i closed(Status = 'Completed') the task even then i SOQL the task i am getting the records in the task list

List<task> task_lst2 = new  List<task>();
task_lst2 = [SELECT id,Task_Order__c, Status  FROM task WHERE whatid=:opp1.id];

In this task list.


Edited Answer

It appears as though the isClosedfield on Task isn't being set to true. It's possible that Status = Completed isn't what actually closes a Task in your org.

Referring to the Object Reference for Salesforce and Force.com, for Task.Status:

Under "Description" it says
Each predefined Status field implies a value for the IsClosed flag. To obtain picklist values, you can query the TaskStatus object.

I'll add that your code also doesn't appear to be looking for the isClosed field which may be adding to some of the issues you're having. Closing a Task doesn't delete it from the database. So, if your org is indeed using Status = Completed as the criteria, since you're not querying for isClosed = false then perhaps that could be why you're not detecting the Task as being closed in your confirmation query to make your assertions. However, that's not how I'd expect it to work if that were the case.

  • IsClosed is set automatically by the system, based on the Status for the task. You cannot set IsClosed manually. – sfdcfox Jun 19 '14 at 19:30
  • He may need to query the TaskStatus object to find out what value actually closes a Task in his org then for an Opportunity. – crmprogdev Jun 19 '14 at 19:43
  • thanks i have solved the issue same as you has suggested. – Anu Jun 20 '14 at 13:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.