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
SIB AdminSIB Admin 

How to refer task inside Account's child object rather than task under account

Hi,

We have Activities(Related List) under Account and we have  a child object called "LED Lighting Opportunity" and activities (Related List) under "LED Lighting Opportunity".I want to write a webservice method that creates activities under "LED Lighting Opportunity" rather than activities under account level.Here is the existing code.How do I update it to refer activities under LED Lighting Opportunity?

//Webservice for quickly logging a voicemail for sales reps.
  ////////////////////////////////////////////////////////////
  webservice static void logACall(ID contactId, ID userId, Integer days, String callResult){
    
    //Get a list of the contact itself, the account of the contact, and all open tasks on the contact.
    List<Contact> contact = [SELECT Id, AccountId FROM Contact WHERE Id = :contactId];
    List<Account> account = [SELECT Id FROM Account WHERE Id = :contact[0].AccountId];
    List<Task> openTask = [SELECT Id, OwnerId, Who__c, Type, Subject, ActivityDate, Priority, Status, Description, WhatId, WhoId From Task Where WhoId = :contactId AND Status = 'Not Started'];
    

    //If there is at least one open task...
    if (openTask.size() > 0) {
      //Create an exact copy of the task that will be postponed (days) number of days later.
      Task followUpTask = openTask[0].clone(false, false, false, false);
      followUpTask.ActivityDate = followUpTaskDate(days);
      
      //Close out the open task and set the subject to 'Call No Answer Left VM'.
      openTask[0].Subject = callResult;
      openTask[0].Status = 'Completed';
      
      insert followUpTask;
      update openTask[0];
      
    }
    
    //There is no open task so we need to create a followUpTask from scratch and then clone a completedTask.
    else {
      Task followUpTask = new Task(OwnerId = userId, Type = '@Call', Subject = '@Call', ActivityDate = followUpTaskDate(days), Priority = 'Normal', Status = 'Not Started', WhoId = contact[0].Id, WhatId = account[0].Id);
      Task completedTask = followUpTask.clone(false, false, false, false);
      
      //Closed out the completedTask and set the subject to 'Call No Answer Left VM'.
      completedTask.Subject = callResult;
      completedTask.ActivityDate = system.today();
      completedTask.Status = 'Completed';
      
      insert followUpTask;
      insert completedTask;
    }
  }
  
  //Private class method for calculating the follow task date in business days.
  private static Date followUpTaskDate(Integer d){
    //Set the date of the follow task to (days) number of days later.
    //Get a date and a datetime for (days) number of days later.
    Date taskDate = system.today().addDays(d);
    datetime dt = system.today().addDays(d + 1);

    //If the date falls on a Saturday, add 2 days to the date.
    if (dt.format('EEEE') == 'Saturday'){
      taskDate = taskDate.addDays(2);
    }
    //If the date falls on a Sunday, add 1 day to the date.
    else if (dt.format('EEEE') == 'Sunday'){
      taskDate = taskDate.addDays(1);
    }
    return taskDate;
  }
}