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
albonillalbonill 

Need help with trigger

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 an attachment present in the Agreement

 

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;
        }

    }
}

 

 

If it works for you , mark this as resolved.

 

Thanks ,

bForce

All Answers

Marty Y. ChangMarty Y. Chang

Hello, Alex,

 

This sounds like pretty serious development you want done.  You'll probably get better help by securing a small budget and posting the job on a site like Elance.

jungleeejungleee

Hi Alex,

 

trigger deleteTest on Opportunity (before delete) {

    Set<id> agreeAttachment = new set<id>();

    for(Attachment a : [select id, name, parentid from Attachment]){
        agreeAttachment.add(a.ParentId);
    }

    for (Opportunity opp : trigger.old){
        //Assuming that the name of the loop-up fields on the Opportunity are as echosign_dev1__SIGN_Agreement__c, Sales_Order__c and Sales_Invoice__c
        if(opp.echosign_dev1__SIGN_Agreement__c != null || opp.Sales_Order__c != null || opp.Sales_Invoice__c!=null ||  agreeAttachment.contains(opp.echosign_dev1__SIGN_Agreement__c)){
            opp.addError('enter your error message here!!');
        }
    }
}

Hope this helps!

Regards
Sam

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;
        }

    }
}

 

 

If it works for you , mark this as resolved.

 

Thanks ,

bForce

This was selected as the best answer
b-Forceb-Force

Above script will work for bulk as well.

 

Thanks,

bForce

albonillalbonill

Hello bForce,

 

I really appreciate your help.  There was only a minor problem.  I had to change Opportunity__c to echosign_dev1__Opportunity__c since that is how the Opportunity is referenced within the Agreement. 

 

The trigger saved just fine so now I just have to test things out.  Two thumbs up!!!

 

Regards,

 

Alex

b-Forceb-Force

It willwork perfectly for bulk data as well

 

Thanks,

bForce