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
Oliver Freeman 9Oliver Freeman 9 

OnClick JS Button - IF Statement

Hi,

I have a requirement to create an OnClick JavaScript button which will essentially check whether the value of a field (Total Number of Contract Loans on Case) is greater than 1, if it is greater than 1, display an error message (alert), telling the user that there is more than one contract loan record raised against the case.
If the value is not greater than 1 (else), tick a checkbox field on the case (Replacement_Device_Issued__c).

I've tried the below script, but I'm getting a "Unexpected End of Script" error when trying to use it.
 
if('{Case.Total_Number_of_Contract_Loans_on_Case__c}' >1)
{
alert( "More than one contract loan." ))
}
else if('{Case.Total_Number_of_Contract_Loans_on_Case__c}' ==1)
{
var newRecords = []; 
var c = new sforce.SObject(Case); 
c.id ="{Case.Id}"; 
c.Replacement_Device_Issued__c = true;
newRecords.push(c); 
result = sforce.connection.update(newRecords); 
window.location.reload();
}

Any help greatly appreciated!

Thanks,
Oli
Akhil ReddyAkhil Reddy
if('{Case.Total_Number_of_Contract_Loans_on_Case__c}' >1){\
	alert( "More than one contract loan." );
}
else if('{Case.Total_Number_of_Contract_Loans_on_Case__c}' ==1) {
	var newRecords = []; 
	var c = new sforce.SObject("Case"); 
	c.id ='{Case.Id}'; 
	c.Replacement_Device_Issued__c = true;
	newRecords.push(c); 
	result = sforce.connection.create(newRecords); 
	window.location.reload();
}

Hope this Works!!
karthikeyan perumalkarthikeyan perumal
Hello, 

Try below code in your JS button  it will update your checkbox field based on your condition.
 
{!REQUIRESCRIPT("/soap/ajax/31.0/connection.js")} 
if({!Case.Total_Number_of_Contract_Loans_on_Case__c > 1)
{
alert( "More than one contract loan." )
}
else if({!Case.Total_Number_of_Contract_Loans_on_Case__c == 1)
{
var newRecords = []; 
var c = new sforce.SObject('case'); 
c.id ='{!Case.Id}'; 
c.Replacement_Device_Issued__c ='True';
newRecords.push(c); 
var result = sforce.connection.update([c]); 
window.location.reload();
}

Hope this will help you. 

mark it solved if its works for you. 

Thanks
karthik