+ Start a Discussion
dbrunsdbruns 

Newbie Code Issue - Prevent Delete of a Closed Won Opportunity

Hello,

 

I wrote the following trigger to prevent deletion of a Closed Won Opportunity:

 

 


trigger CannotDeleteClosedWon on Opportunity (before delete) 

{

    if(system.Trigger.isDelete)

    {

    for (Opportunity Opps : trigger.new)

         if (Opps.StageName == 'Closed Won')

            {

            Opps.addError('Cannot delete a Closed Won Opportunity');

            }

    }




}

 

The problem, I keep getting sent to a page with the following message:

 

CannotDeleteClosedWon: execution of BeforeDelete

caused by: System.NullPointerException: Attempt to de-reference a null object

Trigger.CannotDeleteClosedWon: line 5, column 29

 

Any suggestions?

Best Answer chosen by Admin (Salesforce Developers) 
NaishadhNaishadh
Use trigger.old
Message Edited by Naishadh on 11-20-2009 04:34 AM

All Answers

NaishadhNaishadh
Use trigger.old
Message Edited by Naishadh on 11-20-2009 04:34 AM
This was selected as the best answer
dbrunsdbruns
Thank you!
Colby JuarezColby Juarez
Good day I was working with this trigger and ran into an issue myself. I would like to acomplish the same goal to not allow deletion one a opportuinty is at a cerain stae type of Close/Won
 
trigger CannotDeleteClosedWon on Opportunity (before insert) 
{
if(system.Trigger.isDelete)
 {for (Opportunity Opps : trigger.old)
	if (Opps.Type == 'Close/Won')
		{
		Opps.addError('Cannot delete a Closed Won Opportunity');
        }
}

geting this error
unexpected syntax: 'mismatched input '<EOF>' expecting RCURLY'