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
BenPBenP 

Before delete, better error message

I've been searching around, but have yet to find the solution.  I have the trigger below that prevents the case from being deleted if the work order count field is > 0.  The problem is that it shows a somewhat harsh system error message vs. a nice popup window that the user can click ok on.  Any suggestions?

 

trigger BeforeDeleteCase on Case (before delete) {

for (Case cs : trigger.old)
    {
        
        if (cs.Work_order_count__c > 0)
        //if the work order count is more than zero
            {
            	cs.addError('Can delete this case, A work order for this case currently exists');
            }  
           
}
}

 

Validation Errors While Saving Record(s)
There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Can delete this case, A work order for this case currently exists".

Click here to return to the previous page. 

 

Best Answer chosen by Admin (Salesforce Developers) 
SwarnasankhaSwarnasankha

Hi Ben,

 

When you delete a case, the system redirects you to another URL and therefore the error message that you are trying to display with the trigger will always be displayed in the way that it is being shown currently.

 

If the main purpose here is to show the error message via the UI, then you can create a custom Delete button to manage the requirement. You can use the following steps:

1. Disable the trigger that you have coded

2. Create a custom button with the Display Type as Detail Page Button and the Content Source as OnClick JavaScript

3. Add the following code:

 

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

var queryResult = sforce.connection.query( 
"select Id, Work_order_count__c from Case where id = '{!Case.Id}'", 
{onSuccess: processResultForDelete, onFailure: showFailureMessage}); 

function processResultForDelete(queryResult) 
{ 
	var workOrderCnt = queryResult.records.Work_order_count__c;
	
	if(workOrderCnt != null && workOrderCnt > 0)
	{
		window.alert("You cannot delete the case because there are one or more work order(s) associated with the case.");
	}
	else
	{
		var x = window.confirm("Are you sure?");
		if(x)
		{
			var deleteURL = "{!URLFOR($Action.Case.Delete, Case.Id, null, true)}";
			window.location.replace(deleteURL);
		}
	}
}

function showFailureMessage(error) 
{ 
	alert('Please contact your system administrator and provide the following error message : ' + error);
}

4. Add the custom Delete button the page layouts.

 

I hope this helps. In case you have any queries, feel free to ask.

All Answers

SwarnasankhaSwarnasankha

Hi Ben,

 

When you delete a case, the system redirects you to another URL and therefore the error message that you are trying to display with the trigger will always be displayed in the way that it is being shown currently.

 

If the main purpose here is to show the error message via the UI, then you can create a custom Delete button to manage the requirement. You can use the following steps:

1. Disable the trigger that you have coded

2. Create a custom button with the Display Type as Detail Page Button and the Content Source as OnClick JavaScript

3. Add the following code:

 

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

var queryResult = sforce.connection.query( 
"select Id, Work_order_count__c from Case where id = '{!Case.Id}'", 
{onSuccess: processResultForDelete, onFailure: showFailureMessage}); 

function processResultForDelete(queryResult) 
{ 
	var workOrderCnt = queryResult.records.Work_order_count__c;
	
	if(workOrderCnt != null && workOrderCnt > 0)
	{
		window.alert("You cannot delete the case because there are one or more work order(s) associated with the case.");
	}
	else
	{
		var x = window.confirm("Are you sure?");
		if(x)
		{
			var deleteURL = "{!URLFOR($Action.Case.Delete, Case.Id, null, true)}";
			window.location.replace(deleteURL);
		}
	}
}

function showFailureMessage(error) 
{ 
	alert('Please contact your system administrator and provide the following error message : ' + error);
}

4. Add the custom Delete button the page layouts.

 

I hope this helps. In case you have any queries, feel free to ask.

This was selected as the best answer
BenPBenP
I was afraid you would say that. Great response though, very helpful.

I'll probably do as you stated and might also make another button to allow me to delete the record no matter what.