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
Sean O'ConnellSean O'Connell 

Help with Button Code

I am crearting a pretty simple button but I can not figure out how to get i to work correctly here is what I have


{!REQUIRESCRIPT("/soap/ajax/34.0/connection.js")} 

if ({!Inspection__c.Appointment_Date__c <= TODAY()})

{
alert("Its Past The Appointment Date and Can not be Canceled");
}

else{ 

var newRecords = []; 
var i = new sforce.SObject("Inspection__c");

i.id ="{!Inspection__c.Id}"; 
i.queue__c = 'Canceled';
i.ownerid = '00G37000000hecW' 
newRecords.push(i); 
result = sforce.connection.update(newRecords); 
window.location.reload();}


This Script works in every sceniario other then if Inspection__c.Appointment_Date__c  is blank.  I am getting an error

"A problem with the OnClick JavaScript for this button or link was encountered:
Unexpected token {"

What I want to happen is the field is empty/Null I want it to run the code not the alert.

Thanks in Advance

Sean
AnjithKumarAnjithKumar
Hello Sean,

I have update your code as per your requirement. 
{!REQUIRESCRIPT("/soap/ajax/34.0/connection.js")} 
var appointmentDate = '{!Inspection__c.Appointment_Date__c}';
if (appointmentDate!=undefined && appointmentDate!='' && {!Inspection__c.Appointment_Date__c <= TODAY()}){
	alert("Its Past The Appointment Date and Can not be Canceled");
}else{ 
	var newRecords = []; 
	var i = new sforce.SObject("Inspection__c");
	i.id ="{!Inspection__c.Id}"; 
	i.queue__c = 'Canceled';
	i.ownerid = '00G37000000hecW' 
	newRecords.push(i); 
	var result = sforce.connection.update(newRecords); 
	window.location.reload();
}

Hope it helps you.

Thanks,
Anjith kumar.