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
DTAPADMINDTAPADMIN 

Task is not being created

My code seem to execute and the success message is displayed on the screen but the task is not being created.  Please look at the code below and give any input as to why this might not create the new task.

Code:
var queryResult = sforce.connection.query("Select CreatedDate, Project_Manger__c, Name, Implementation_Rep__r.Name, Implementation_Rep__c, Id, Go_Live_Planned_Date__c, Technology_Rep__r.Name, Technology_Rep__c From SFDC_Projects__c WHERE Name = '{!SFDC_Projects__c.Name}'");

var records = queryResult.getArray('records');

var myTask1 = new sforce.SObject("Task");
myTask1.WhatId = records[0].Id;
myTask1.OwnerId = records[0].Technology_Rep__r.Name;
myTask1.Status = 'Not Started';
myTask1.Priority = 'Normal';
myTask1.Subject = 'Create Customer DB from Base';
myTask1.IsReminderSet = true;
sforce.connection.create([myTask1], callback);
}

 

London BenLondon Ben
I know it's rather a boring answer - but have you tried the old fashioned 'try/catch error' thing? 
Code:
try {    
    var queryResult = sforce.connection.query("Select CreatedDate, Project_Manger__c, Name, Implementation_Rep__r.Name, Implementation_Rep__c, Id, Go_Live_Planned_Date__c, Technology_Rep__r.Name, Technology_Rep__c From SFDC_Projects__c WHERE Name = '{!SFDC_Projects__c.Name}'");
    var records = queryResult.getArray('records');
    var myTask1 = new sforce.SObject("Task");
    myTask1.WhatId = records[0].Id;
    myTask1.OwnerId = records[0].Technology_Rep__r.Name;
    myTask1.Status = 'Not Started';
    myTask1.Priority = 'Normal';
    myTask1.Subject = 'Create Customer DB from Base';
    myTask1.IsReminderSet = true;
    sforce.connection.create([myTask1], callback);
  }

catch (error)
  {
    alert(error.faultstring);
  }

DTAPADMINDTAPADMIN
I added that code but it doesn't through any errors.  It just displays the "Tasks Successfully Created" in the new window. 
Is there anything special I have to do to use the sforce.connection.create vs. .update?
 
I have other s-controls using the sforce.connection.update and they work just fine.
London BenLondon Ben
hmm your code is very similar to other stuff I use
 
perhaps catch the 'result' from the creation call. - eg 
 
Code:
var result = sforce.connection.create([myTask1], callback);
if (result[0].getBoolean("success")) { 
alert("Task Created"); 
} 
else 
{ 
alert("Error: " + result[0]); 
} 

 
DTAPADMINDTAPADMIN
Firebug console gave me an error of:
 
result has no properties 
> if (result[0].getBoolean("success")) {
DTAPADMINDTAPADMIN
Okay,
 
I took out the callback function and then got the error message: Fields: 'OwnerId, message:'Assigned To Id; id value of incorrect type: Ryan Sturm', StatusCode: 'Malformed_Id' id:null, success: 'false',}
 
I am going to change my code a little bit to see if I can fix the Id issue.
DTAPADMINDTAPADMIN
Is there a way in javascript/Ajax to include a variable in a SOQL statement?
I am setting a variable to the Name I get in my first query and then trying to get the User ID from the User object  by doing the following.
 
Code:
var queryResult = sforce.connection.query("Select CreatedDate, Project_Manger__c, Name, Implementation_Rep__r.Name, Implementation_Rep__c, Id, Go_Live_Planned_Date__c, Technology_Rep__r.Name, Technology_Rep__c From SFDC_Projects__c WHERE Name = '{!SFDC_Projects__c.Name}'");

var records = queryResult.getArray('records');
var usrName = records[0].Technology_Rep__r.Name;
var usrId = sforce.connection.query("Select Id From User WHERE Name = usrName");
var myTask1 = new sforce.SObject("Task");
myTask1.WhatId = records[0].Id;
myTask1.OwnerId = usrId;
myTask1.Status = 'Not Started';
myTask1.Priority = 'Normal';
myTask1.Subject = 'Create Customer DB from Base';
myTask1.IsReminderSet = true;
var result = sforce.connection.create([myTask1]);
if (result[0].getBoolean("success")) {
alert("Task Created");
}
else
{
alert("Error: " + result[0]);
}

 
London BenLondon Ben
yes in a word... you precede variables with a colon (:) it's called a bind. 
The Apex parser first evaluates the local variable in script context before executing the SOQL statement. Bind expressions can be used as:
• The filter literals in WHERE clauses
• The numeric value in LIMIT clauses
• The value of the IN or NOT IN operator in WHERE clauses, allowing filtering on a dynamic set of values. Note that this is
of particular use with a list of IDs or Strings, though it works with lists of any type.
Code:
For example:
Account A = new Account(name='xxx');
insert A;
Account B;
// A simple bind
B = [select id from account where id = :A.id];
// A bind with arithmetic
B = [select id from account
where name = :('x' + 'xx')];
String s = 'XXX';
// A bind with expressions
B = [select id from account
where name = :'XXXX'.substring(0,3)];

soo.....
Code:
var queryResult = sforce.connection.query("Select CreatedDate, Project_Manger__c, Name, Implementation_Rep__r.Name, Implementation_Rep__c, Id, Go_Live_Planned_Date__c, Technology_Rep__r.Name, Technology_Rep__c From SFDC_Projects__c WHERE Name = '{!SFDC_Projects__c.Name}'");

var records = queryResult.getArray('records');
var usrName = records[0].Technology_Rep__r.Name;
var usrId = sforce.connection.query("Select Id From User WHERE Name = :usrName");
var myTask1 = new sforce.SObject("Task");
myTask1.WhatId = records[0].Id;
myTask1.OwnerId = usrId;
myTask1.Status = 'Not Started';
myTask1.Priority = 'Normal';
myTask1.Subject = 'Create Customer DB from Base';
myTask1.IsReminderSet = true;
var result = sforce.connection.create([myTask1]);
if (result[0].getBoolean("success")) {
alert("Task Created");
}
else
{
alert("Error: " + result[0]);
}


 
by the way, I've not tested the rest of the code you supplied (or even read it)... but the variable thing will work ;)
cheenathcheenath
That is an example in Apex. In Ajax it looks like:

<html>
<head>
<script src="/soap/ajax/10.0/connection.js"></script>
<script src="/js/dojo/0.4.1/dojo.js"></script>

<script>
dojo.addOnLoad(init);

function init() {
var callback = {
onSuccess : displayResult,
onFailure : displayError
};
sforce.connection.query("SELECT Id FROM User where name = 'Manoj Cheenath'", callback);
}

function displayResult(result) {
var it = new sforce.QueryResultIterator(result);
var html = [];
while(it.hasNext()) {
var record = it.next();
html.push("Id = " + record.Id + "<br>");
html.push("<hr>");
html.push("<br>");
}

document.getElementById("output-div").innerHTML = html.join("");
}

function displayError(error) {
document.getElementById("output-div").innerHTML =
"oops something went wrong ... " + error;
}
</script>


</head>
<body>

<div id="output-div"></div>

</body>
</html>

cheenathcheenath
Or more specific:

var userName = "myuser";
sforce.connection.query("SELECT Id FROM User where name = '" + userNmae + "'", callback);


DTAPADMINDTAPADMIN
That was exactly what I was looking for.  Thank you.
 
On another note: do you know how to add 7 days to a date or datetime without APEX methods?
cheenathcheenath
You can get filed in AJAX Toolkit as javascript Date. See getDate() or getDateTime().
Once you get the javascript date you can use the standard javascript methods to
adjust the date. You can find example here:

http://www.w3schools.com/js/js_obj_date.asp

HTHs,




DTAPADMINDTAPADMIN
You have been very helpful thank you.