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
RKRK 

Passing Date Parameter to Task Created vie SControl

Hey all,
I've recently created an SControl that will create follow-up tasks for my users based upon the last time a key contact has been created.  The basic premise is that anyone that is within the first tier of producers (i.e. their A producers) should be spoken to every 30 days.

So, based upon the "Last Activity" field and the tier field, I can rather easily figure out who should be targetted.

In the case of the A producers, they should be contacted "Today - 30 days".

I created an S Control to create the tasks, but I struggled on the formatting of the date to be passed to the Last Activity field - I know that it needs to be in the format of YYYY-MM-DD, but I was never able to get the date format in the format that would work.

As a work around, I have the SControl prompt me for the date, for which I'll type "2007-02-23".  But, I'd much rather have it just be automatic (check out the red highlighted section in the code below).

I'll run this manually on a somewhat regular basis.

Any thoughts?
As always, thanks for the input.
RK

<html>

<head>

<script src="/soap/ajax/9.0/connection.js"></script>

<script>

var d = new Date(); //will give today's date
document.write(d+"<br>"); //displays today's date
d.setDate(d.getDate()-30); //subtract 30 days from today's date.  Ideally, this is passed against the "Last Activity Date"
document.write(d); //display what that date is
//alert("//alertingdate");
//alert(d);
var taskType = '012300000000PvkAAE';
var followUpTaskType = 'Outbound Call';
var followUpTaskSubject = 'Top 50 advisor not contacted in 30 days';
var followUpResults = 'Follow-Up';
var whatTaskDate = prompt('What is the date?',' ');
var followUpDate = {!Today};
var followUpTaskNotes = 'Call to make contact with one of your top 50 advisors - no contact has been made in the last 30 days';
var followUpTaskStatus = 'Not Started';
var followUpSubType = 'Tier 1 - Regular Contact';




function createTask() {
var queryContact = sforce.connection.query("Select Id, OwnerId from Contact where X2006_Rank_Within_Region__c > 0 and X2006_Rank_Within_Region__c < 51 and RecordTypeId = '0123000000005yGAAQ' and Known_Terminated__c = FALSE and ((lastactivitydate = null) or (lastactivitydate < "+whatTaskDate+"))limit 150");

//alert(queryContact);


contactsToBeTasked = queryContact.getArray("records");
//alert ("contactsToBeTasked");
//alert (contactsToBeTasked);
//var contactsToBeTaskedArray = new Array();


var contactsToBeTaskedArray= new Array();
//alert("Array Created");
//alert(contactsToBeTaskedArray);

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

for (var i = 0; i < records.length; i++)
{
taskCreatedLineItem = contactsToBeTasked[i];
//alert(taskCreatedLineItem.get("Id"));
var neglectedContactTask = new sforce.SObject("Task");
//alert("DynaBean");
neglectedContactTask.set("RecordTypeId",taskType);
//alert("typeset");
neglectedContactTask.set("Type",followUpTaskType);
//alert ("Task Type Set");
neglectedContactTask.set("Subject",followUpTaskSubject);
//alert("SubSet");
neglectedContactTask.set("Status",followUpTaskStatus);
//alert("StatSet");
neglectedContactTask.set("Description",followUpTaskNotes);
//alert("CommSet");
neglectedContactTask.set("ActivityDate",new Date('{!Today}') );
//alert("dateset");
neglectedContactTask.set("OwnerId",taskCreatedLineItem .get("OwnerId"));
//alert("OwnerSet");
neglectedContactTask.set("WhoId",taskCreatedLineItem .get("Id"));
//alert("ConIDSet");
neglectedContactTask.set("Call_Sub_Type__c",followUpSubType );
//alert("SubType");
contactsToBeTaskedArray.push(neglectedContactTask);
//alert("Pushed");
}
var saveArrayResults = sforce.connection.create(contactsToBeTaskedArray);

}

</script>

</head>

<body onload="createTask();">

</body>

</html>
Ron HessRon Hess
you should be able to use the new feature

Date Literals


so, something like this
SELECT Id FROM Account WHERE CreatedDate = LAST_N_DAYS:365


note the equals operator used here

you would use something like this

lastactivitydate = LAST_N_DAYS:30



RKRK
Thank you so much, Ron, for the reply.  It worked like a charm.

For those of you who want to improve upon my poor attempt at code for your own use, please feel free to use the foundation below.

Also, for those of you who are code challenged like myself and would like a repository of code examples like this, please vote on my idea below at the Idea Exchange.

http://ideas.salesforce.com/article/show/57416/A_UserSubmitted_CodeSControlEnhancement_Sample_Library

______________________________________________________________________________________

<html>

<head>

<script src="/soap/ajax/9.0/connection.js"></script>

<script>

var taskType = '012300000000PvkAAE';
var followUpTaskType = 'Outbound Call';
var followUpTaskSubject = 'Top 50 advisor not contacted in 30 days';
var followUpResults = 'Follow-Up';
//var whatTaskDate = prompt('What is the date?',' ');
var followUpDate = {!Today};
var followUpTaskNotes = 'Call to make contact with one of your top 50 advisors - no contact has been made in the last 30 days';
var followUpTaskStatus = 'Not Started';
var followUpSubType = 'Tier 1 - Regular Contact';




function createTask() {
var queryContact = sforce.connection.query("Select Id, OwnerId from Contact where X2006_Rank_Within_Region__c > 0 and X2006_Rank_Within_Region__c < 51 and RecordTypeId = '0123000000005yGAAQ' and Known_Terminated__c = FALSE and ((lastactivitydate = null) or (lastactivitydate < LAST_N_DAYS:30))limit 175");

//alert(queryContact);


contactsToBeTasked = queryContact.getArray("records");
//alert ("contactsToBeTasked");
//alert (contactsToBeTasked);
//var contactsToBeTaskedArray = new Array();


var contactsToBeTaskedArray= new Array();
//alert("Array Created");
//alert(contactsToBeTaskedArray);

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

for (var i = 0; i < records.length; i++)
{
taskCreatedLineItem = contactsToBeTasked[i];
//alert(taskCreatedLineItem.get("Id"));
var neglectedContactTask = new sforce.SObject("Task");
//alert("DynaBean");
neglectedContactTask.set("RecordTypeId",taskType);
//alert("typeset");
neglectedContactTask.set("Type",followUpTaskType);
//alert ("Task Type Set");
neglectedContactTask.set("Subject",followUpTaskSubject);
//alert("SubSet");
neglectedContactTask.set("Status",followUpTaskStatus);
//alert("StatSet");
neglectedContactTask.set("Description",followUpTaskNotes);
//alert("CommSet");
neglectedContactTask.set("ActivityDate",new Date('{!Today}') );
//alert("dateset");
neglectedContactTask.set("OwnerId",taskCreatedLineItem .get("OwnerId"));
//alert("OwnerSet");
neglectedContactTask.set("WhoId",taskCreatedLineItem .get("Id"));
//alert("ConIDSet");
neglectedContactTask.set("Call_Sub_Type__c",followUpSubType );
//alert("SubType");
contactsToBeTaskedArray.push(neglectedContactTask);
//alert("Pushed");
}
var saveArrayResults = sforce.connection.create(contactsToBeTaskedArray);

}

</script>

</head>

<body onload="createTask();">

</body>

</html>