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
Vandana Rattan 4Vandana Rattan 4 

Javascript button Timezone issue

I have created a Javascriptbutton on Cases to create Tasks for users after a number of days (entered in prompt box). It is working fine for users in Sydney timezone. But is givivng issues for users in Adelaide. I am unable to figure out the cause. Any hints would be appreciated.
 
{!REQUIRESCRIPT("/soap/ajax/33.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/33.0/apex.js")} 

try{ 
var numDays = prompt("Please enter number of days after which you want to contact", "0"); 

var exstTask = sforce.connection.query("SELECT Id, Status from Task where whoId='{!Case.ContactId}' and WhatId = '{!Case.Id}' and subject like '%Contact Attempt%'"); 

var tskRec = exstTask.getArray("records"); 


for(var i=0; i< tskRec.length; i++){ 

var taskToClose = new sforce.SObject("Task"); 
taskToClose.Id = tskRec[i].Id; 

taskToClose.Status = 'Compeleted'; 
var resultUpdate = 
sforce.connection.update([taskToClose]); 
if(resultUpdate [0].success != "true"){ 
//alert('An error occurred while Closing task ' + resultUpdate[0].errors.message); 
} 
} 

alert('Please do not click any other button. Action In Progress...'); 

var taskToCreate = new sforce.SObject("Task"); 

var recType = sforce.connection.query("SELECT Id from RecordType where name='General' and sObjectType = 'Task'"); 
var records = recType.getArray("records"); 
taskToCreate.RecordTypeId = records[0].Id; 


taskToCreate.OwnerId = "{!$User.Id}"; 

taskToCreate.WhatId = "{!Case.Id}"; 

taskToCreate.WhoId = "{!Case.ContactId}"; 
var num = tskRec.length+1; 
taskToCreate.Subject = "Contact Attempt " + num.toString(); 

//taskToCreate.ActivityDate = new Date(); 
var date1 = new Date(); 
alert('offset>>>' + date1.getTimezoneOffset()); 

date1.setDate(date1.getDate()+parseInt(numDays)); 

var date2 =((date1.getMonth()+1)+"/"+(date1.getDate())+"/"+date1.getFullYear()); 

taskToCreate.ActivityDate = new Date(date2); 
taskToCreate.Type = "Call Attempt"; 

taskToCreate.Priority = "Normal"; 
taskToCreate.Status = "Not Started"; 

taskToCreate.IsReminderSet = "true"; 
taskToCreate.ReminderDateTime = new Date(date2); 

alert('task created>>>>>' + taskToCreate); 
result = sforce.connection.create([taskToCreate]); 


if(result[0].success == "true"){ 
alert('Task has been created successfully.'); 
var caseToUpdate = new sforce.SObject("Case"); 
caseToUpdate.Id = "{!Case.Id}" 
caseToUpdate.Contact_Attempt__c = num; 
caseToUpdate.Last_Contact_Date__c = new Date(); 
var resultUpdate = 
sforce.connection.update([caseToUpdate]); 
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 
); 
}

User-added image
Vandana Rattan 4Vandana Rattan 4
For anyone who alos faces similar kind of issue I used toISOString() function to resolve the problem. I had to manipulate my dates accorting to Australian Timezone after that.