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
Amiya DasAmiya Das 

javasscript custom buttons

I have a requirement to create a custom button for cancelling a request. Have already implemented this functionality using javascript custom button for which, on clicking the cancel button, the ticket(Custom object) status changes from new to cancelled. But for other status picklist values other than new, it will throw a pop that only new tickets can be cancelled. This has been done(code is below). Now an additional requirement is that only a partilcular user should be able to cancel the ticket for all status picklist values(including new picklist value). As of now a user is only able to cancel a ticket in new status only.
Any help would be appreciated. Thanks in advance. Any ideas what modifications to be done..

Code is :-

{!REQUIRESCRIPT("/soap/ajax/39.0/connection.js")}{!REQUIRESCRIPT("/soap/ajax/39.0/apex.js")}
 
var trs = new sforce.SObject("Ticket_Request__c");
trs.Id = '{!Ticket_Request__c.Id}';
if('{!Ticket_Request__c.Status__c}' == 'New') {
if(confirm('Are you Sure ? Once the Ticket Request is cancelled , it cannot be resubmitted')){
window.location.reload();
trs.Status__c = 'Cancelled - Unsubmitted';
}
else{
}
 
}
else if('{!Ticket_Request__c.Status__c}' == 'Cancelled - Unsubmitted'){
alert('Ticket request is already cancelled');
 
}
else if('{!Ticket_Request__c.Status__c}' == 'Cancelled - Unsubmitted'){
AND(
ISCHANGED(Ticket_Request__c.Status__c ),
ISPICKVAL(PRIORVALUE(Ticket_Request__c.Status__c ), "Cancelled - Unsubmitted"),
NOT(ISPICKVAL( Ticket_Request__c.Status__c , "Cancelled - Unsubmitted"))
)
}
else{
alert('Only newly created ticket requests can be cancelled');
 
}
var result = sforce.connection.update([trs]);
LBKLBK
I am not very clear about your requirement of a "only a partilcular user should be able to cancel the ticket for all status picklist values"

I have added a condition in such a way that other than "New" tickets can be cancelled only by a System Administrator profile.

You can customize it the way you need it.

If you want only the "System Administrator" (or any other profile) to be able to any tickets (including new), remove the first IF condition in the code below.
{!REQUIRESCRIPT("/soap/ajax/39.0/connection.js")}{!REQUIRESCRIPT("/soap/ajax/39.0/apex.js")}

var trs = new sforce.SObject("Ticket_Request__c");
trs.Id = '{!Ticket_Request__c.Id}';
if('{!Ticket_Request__c.Status__c}' == 'New') {
	if(confirm('Are you Sure ? Once the Ticket Request is cancelled , it cannot be resubmitted')){
		trs.Status__c = 'Cancelled - Unsubmitted';
	}
}
else if('{!Ticket_Request__c.Status__c}' == 'Cancelled - Unsubmitted'){
	alert('Ticket request is already cancelled');
}
else if('{!$Profile.Name}' == 'System Administrator'){
	if(confirm('Are you Sure ? Once the Ticket Request is cancelled , it cannot be resubmitted')){
		//window.location.reload();
		trs.Status__c = 'Cancelled - Unsubmitted';
	}
}
else{
	alert('Only newly created ticket requests can be cancelled');
}
var result = sforce.connection.update([trs]);
window.location.reload();
Let me know if this helps.