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
Chris FisherChris Fisher 

String to Date Time Format?

Hi All,

I having some trouble taking 15 days off of a datetime field. 

    var day = prompt("Enter Callback day", "01");   
        var month = prompt("Enter Callback Month", "11");
        var year = prompt("Enter Callback Year", "2015");   
        var time = prompt("Enter Callback Time", "09:00");
        try
            {
            var taskToCreate = new sforce.SObject("Task");
              var datetime = year+"-"+month+"-"+day+"T"+time+":00Z";
            datetime - 15;
            taskToCreate.OwnerId = "{!$User.Id}";
            taskToCreate.WhoId = "{!Lead.Id}";
            taskToCreate.Subject = "Call";
            taskToCreate.ActivityDate = new Date();
            taskToCreate.Priority = "Normal";
            taskToCreate.Status = "Not Started";
            taskToCreate.IsReminderSet = true;
            taskToCreate.ReminderDateTime = mydatetime;
            result = sforce.connection.create([taskToCreate]);
            if(result[0].success == "true")
                {
                location.reload();
                }
            else
                {
                alert(
                "An Error has Occurred. Error: \r\n" +
                result[0].errors.message
                );
                }
            }
        catch(e)
            {
            alert(
            "An Un-expected Error has Occurred. Error: \r\n" +
                e
            );
            }

Basically its a customer button that will create a task, that will link to a lead, but we want the tast to pop up 15 days before the date entered. 

I think the problem is because "datetime" is a string and not a date time field. But how do i get it to be recognised as a datetime?

Thanks
JeffreyStevensJeffreyStevens
Set your reminderDateTime field using this...

newInstance(year, month, day, hour, minute, second) Constructs a Datetime from Integer representations of the specified year, month (1=Jan), day, hour, minute, and second in the local time zone.

 taskToCreate.ReminderDateTime = DateTime.newInstance(year,Month,Day,Hour,Minute,Second);


 
Chris FisherChris Fisher
Ok great, so in newInstance(year, month, day, hour, minute, second) i can write as newInstance(2015, 1 , 25, 9, 9, 9) 

Could i also write as: newinstance(2016,01,01,09:00)


 
JeffreyStevensJeffreyStevens
Yep on the first statement.

Nope on the second.

All 6 parameters are integer. From page 1927 of the Apex Developers Guide
Chris FisherChris Fisher
New code as below

    var day = prompt("Enter Callback day", "01");   
        var month = prompt("Enter Callback Month", "11");
        var year = prompt("Enter Callback Year", "2015");   
        var time = prompt("Enter Callback Time", "09:00");
        var hour = time.substring(0,2);
        var minute = time.substring(3,5);
        var second = "00"
        try
            {
            var taskToCreate = new sforce.SObject("Task");
              var datetime = datetime.newinstance(year,month,day,hour,minute,second);
            datetime - 15;


Which i think was smashingly, but error generated "Cannot Read Property 'newinstance' of undefined" I had Something similar early when i was using 

var date = new date()   This gives me the error "Date is not defined" 

Is there something stopping dates being used on custom buttons? 
JeffreyStevensJeffreyStevens
Oh - you're doing this in .js - I was thinking you were in APEX.

Sorry - can't help much then.