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
THUNDER CLOUDTHUNDER CLOUD 

How to undelete Account having open opportunity ?

If any of the opportunities associated with Account has stage value is not "closed lost" then the Account cannot be deleted. 

How to achieve this ?
Best Answer chosen by THUNDER CLOUD
Onesh ReddyOnesh Reddy
Hi Thunder,

Please find the below Trigger that meets your requirements.
trigger AccountTrigger on Account (before delete) {
   Map<id,List<Opportunity>> AccountMap=new Map<id,List<Opportunity>>();
   if(Trigger.isdelete)
       if(Trigger.isbefore)
   {
       for(Account Account:[select id,name,(select id,stagename,name from Opportunities) from account where id in:trigger.oldmap.keyset()])
       {
           AccountMap.put(Account.id, Account.Opportunities);
       }
       for(Account Account:trigger.old)
       {
           for(Opportunity Opportunity:AccountMap.get(Account.id))
           {
               if(Opportunity.StageName!='Closed lost')
               {
                   Account.addError('Cannot perform the action');
               }
           }
       }
   }
}

Kindly mark this solution as solved if it helps you.

Best Regards,
Onesh.K

All Answers

NagendraNagendra (Salesforce Developers) 
Hi Thunder Cloud,

You can achieve this functionality by writing a trigger.Please find the below code snippet.
//Trigger context variable for  isdelete   

            if(trigger.isdelete){

                //check condition that stagename as closed lost and userprofile is sys admin

                if(o.StageName =='ClosedLost'&& userprofile !='System Administrator'){

                //Through error while user not equal to system  admin try to delete or modify closed lost opportunity

                    o.adderror('Only Admin can delete once opportunity status is closed');

                }

Note : 'o' is the instance of the object that is been used in your trigger.
Note: As a best practice dont write multiple triggers on the same object, still if you require you can make use of trigger context variable.

Kindly mark this solution as solved if it helps you.

Best Regards,
Nagendra.P
Onesh ReddyOnesh Reddy
Hi Thunder,

Please find the below Trigger that meets your requirements.
trigger AccountTrigger on Account (before delete) {
   Map<id,List<Opportunity>> AccountMap=new Map<id,List<Opportunity>>();
   if(Trigger.isdelete)
       if(Trigger.isbefore)
   {
       for(Account Account:[select id,name,(select id,stagename,name from Opportunities) from account where id in:trigger.oldmap.keyset()])
       {
           AccountMap.put(Account.id, Account.Opportunities);
       }
       for(Account Account:trigger.old)
       {
           for(Opportunity Opportunity:AccountMap.get(Account.id))
           {
               if(Opportunity.StageName!='Closed lost')
               {
                   Account.addError('Cannot perform the action');
               }
           }
       }
   }
}

Kindly mark this solution as solved if it helps you.

Best Regards,
Onesh.K
This was selected as the best answer