+ Start a Discussion
albonillalbonill 

Need help creatng a trigger to avoid deletion of Opportunities

Hello,

 

I need assistance in the creation of a trigger that will not allow the deletion of an Opportunity under the following circumstances:

 

  • If there is an Agreement Name (Name) (echosign_dev1__SIGN_Agreement__c is object)
  • Or if there is a Sales Order No (Name) (Sales_Order__c is object)
  • Or if there is an Sales Invoice No (Name) (Sales_Invoice__c is the object)
  • Or if there is a Google doc, note or attachment present in the Opp

Thank you for your help.

 

Regards,

Alex

 

Best Answer chosen by Admin (Salesforce Developers) 
b-Forceb-Force

Apex script

trigger validateChilds on Opportunity (before delete) 
{
    Set<Id> stId = new Set<Id>();
    List<echosign_dev1__SIGN_Agreement__c> lstRe =[Select Id, Opportunity__c from echosign_dev1__SIGN_Agreement__c where opportunity__c in : Trigger.oldMap.KeySet() ];
    for(echosign_dev1__SIGN_Agreement__c objp : lstRe)
    {
        stId.add(objp.opportunity__c);
    }

    Set<Id> stIdS = new Set<Id>();
    List<Sales_Order__c> lstReS =[Select Id, Opportunity__c from Sales_Order__c where opportunity__c in : Trigger.oldMap.KeySet() ];
    for(Sales_Order__c objp : lstReS)
    {
        stIdS.add(objp.opportunity__c);
    }

    Set<Id> stIdSI = new Set<Id>();
    List<Sales_Invoice__c> lstReI =[Select Id, Opportunity__c from Sales_Invoice__c where opportunity__c in : Trigger.oldMap.KeySet() ];
    for(Sales_Invoice__c objp : lstReI)
    {
        stIdSI.add(objp.opportunity__c);
    }
    
    Set<Id> stIdAtt = new Set<Id>();
    List<Attachment> lstAtt =[Select Id, ParentId from Attachment where ParentId in : Trigger.oldMap.KeySet() ];
    for(Attachment objp : lstAtt)
    {
        stIdSI.add(objp.ParentId);
    }
  
    for(Opportunity objA : Trigger.old)
    {
        if(stId.contains(objA.Id))
        {
     	   objA.addError('you can not delete this, As there are AGreements available .');
           return;
        }

	if(stIdS.contains(objA.Id))
        {
     	   objA.addError('you can not delete this, As there are some Sales Order available .');
           return;
        }

	if(stIdSI.contains(objA.Id))
        {
     	   objA.addError('you can not delete this, As there are some sales INvoice available');
           return;
        }

	if(stIdAtt.contains(objA.Id))
        {
     	   objA.addError('you can not delete this, As there are some Google docs are attached to it ');
           return;
        }

    }
}

 

 

Let me know if you need any more assistance.

 

Thanks,

bForce

All Answers

b-Forceb-Force

DOes below mentioned objects are in Opportunity related list ?

 

 

For Attachment and notes : what you want to check ......  means Name of attachment/note contain google Doc ?

 

Clarify above issues, I will help you to write trigger 

 

Thanks,

bForce

albonillalbonill

Hello b-Force,

 

Yes, all of the mentioned objects are in the Opportunity related list section.  For the Google Docs section, I guess it would be sufficient to check if there is an attachment present (don't worry about notes or Google Docs since we only use attachments) in order for deletion to be denied.  Thanks for your help.

 

Regards,

Alex

b-Forceb-Force

Apex script

trigger validateChilds on Opportunity (before delete) 
{
    Set<Id> stId = new Set<Id>();
    List<echosign_dev1__SIGN_Agreement__c> lstRe =[Select Id, Opportunity__c from echosign_dev1__SIGN_Agreement__c where opportunity__c in : Trigger.oldMap.KeySet() ];
    for(echosign_dev1__SIGN_Agreement__c objp : lstRe)
    {
        stId.add(objp.opportunity__c);
    }

    Set<Id> stIdS = new Set<Id>();
    List<Sales_Order__c> lstReS =[Select Id, Opportunity__c from Sales_Order__c where opportunity__c in : Trigger.oldMap.KeySet() ];
    for(Sales_Order__c objp : lstReS)
    {
        stIdS.add(objp.opportunity__c);
    }

    Set<Id> stIdSI = new Set<Id>();
    List<Sales_Invoice__c> lstReI =[Select Id, Opportunity__c from Sales_Invoice__c where opportunity__c in : Trigger.oldMap.KeySet() ];
    for(Sales_Invoice__c objp : lstReI)
    {
        stIdSI.add(objp.opportunity__c);
    }
    
    Set<Id> stIdAtt = new Set<Id>();
    List<Attachment> lstAtt =[Select Id, ParentId from Attachment where ParentId in : Trigger.oldMap.KeySet() ];
    for(Attachment objp : lstAtt)
    {
        stIdSI.add(objp.ParentId);
    }
  
    for(Opportunity objA : Trigger.old)
    {
        if(stId.contains(objA.Id))
        {
     	   objA.addError('you can not delete this, As there are AGreements available .');
           return;
        }

	if(stIdS.contains(objA.Id))
        {
     	   objA.addError('you can not delete this, As there are some Sales Order available .');
           return;
        }

	if(stIdSI.contains(objA.Id))
        {
     	   objA.addError('you can not delete this, As there are some sales INvoice available');
           return;
        }

	if(stIdAtt.contains(objA.Id))
        {
     	   objA.addError('you can not delete this, As there are some Google docs are attached to it ');
           return;
        }

    }
}

 

 

Let me know if you need any more assistance.

 

Thanks,

bForce

This was selected as the best answer