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
SarbashishSarbashish 

Unable to create New Task using S-Control

I am trying to create a New Task and assign it to a particular account (account id: 0015000000GadFpAAJ).
 
I get to see the alert ID is Null. The task does not get created. I am trying to do this and I have no idea why it is failing. Please find the code below:
----------------------------------------------------------------------------------------
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title></title>
<script type="text/javascript" src="/js/functions.js"></script>
<script src="/soap/ajax/8.0/connection.js"></script>
 <script>
    function setupPage() {
  var user = sforce.connection.getUserInfo().userFullName;
  alert("Logged in User: "+user);
  SaveTHISSCalendar();
    }
 //create THISS Calendar Data
 function SaveTHISSCalendar(){
  try{ 
        alert(new Date());
  var TaskObj = new sforce.SObject("Task");
  TaskObj.WhatId = "0015000000GadFpAAJ"
  TaskObj.AccountId = "0015000000GadFpAAJ";
  TaskObj.ActivityDate = new Date();
  TaskObj.Description = "Thank you order confirmation to Client + Welcome Kit";
  TaskObj.Status = "Not Started";
  TaskObj.OwnerId = "00550000000wJyuAAE";
  TaskObj.CreatedById = "00550000000wJyuAAE";
  TaskObj.Priority = "Normal";
  TaskObj.Subject = "Send Welcome Kit";
  var result = sforce.connection.create([TaskObj]);
  alert("New Task record created with id " + result[0].id);
  }catch(e){
  alert(e.faultstring);
  }
/*  
  if (result[0].getBoolean("success")) {
   //log("new Task created with id " + result[0].id);
   alert("New Task record created with id " + result[0].id);
   //TaskObj.Id = result[0].id;
  } else {
   //throw ("failed to create Task Record" + result[0]);
   alert("Failed to create New Task Record" + result[0]);
  }
*/
 }
</script>

</head>
<body onload="setupPage();">
</body>
</html>
cheenathcheenath
Not clear what error message you are getting.

Try this:

var result = sforce.connection.create([TaskObj]);
alert(result);

I think the server is returning some error back. If you alert result you
will be able to see it.






SarbashishSarbashish

I have found the problem. The issue is that you cannot set the accountid at the time of creating the Task record. So all you need to do is create the Task record without the accountid. Then in the next block you have to set the whatid which is the accountid and Salesforce internally will set the accountid. Unfortunately there is no exception thrown in the catch block so you don't have the debugging facility. So the revised code will be something like this:-

 function SaveTHISSCalendar(){
  try{ 
        //alert(new Date());
  var TaskObj = new sforce.SObject("Task");
  //TaskObj.What = "Account";
  //TaskObj.WhatId = "0015000000GadFpAAJ";
  //TaskObj.AccountId = "0015000000GadFpAAJ";
  TaskObj.ActivityDate = new Date();
  TaskObj.Description = "Thank you order confirmation to Client";
  TaskObj.Status = "Not Started";
  TaskObj.OwnerId = "00550000000yeTrAAI";
  TaskObj.Priority = "Normal";
  TaskObj.Subject = "Send Welcome Kit";
  var result = sforce.connection.create([TaskObj]);
  alert("New Task record created with id " + result[0].id);
  // Update the Newly Created Event
  TaskObj.id = result[0].id
  //TaskObj.AccountId = "0015000000GadFpAAJ";
  TaskObj.WhatId = "0015000000GadFpAAJ";
  result = sforce.connection.update([TaskObj]);
  alert(result[0].id);
  alert("tried to update the record");
  }catch(e){
  alert(e.faultstring);
  }

 

SuperfellSuperfell
You can set the WhatId during creation, you don't need to do the create/update approach. Create an Update are bulk calls, so they don't throw exceptions, but return per row results that will include an error message and status code if that particular row fails. see the docs for SaveResult.
SarbashishSarbashish
Thank you for that. It worked.:)
 
Cheers,
 
Sarbashish Bhattacharjee