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
Saad Ahmad 33Saad Ahmad 33 

Validation Rule to Require Attachment when Opp Reaches Probability 100%

Hi, 

I need to create a trigger or validation rule that requires a user to attach a document/insertion order to an opportunity when it is changed to 100% or closed won.

Please advise. 

  
Best Answer chosen by Saad Ahmad 33
Akhil AnilAkhil Anil
Hi Saad,

Are you checking in the Production environment ?

If yes, you cannot create or edit Apex classes or triggers in a production organization.

To use Apex classes or triggers in a production organization, you need to write them in a Sandbox or free Developer Edition organization--along with the required automated tests to meet or exceed code coverage requirements--and then use one of our deployment tools (the Force.com IDE or the Force.com Migration Tool for Ant) to push your classes and triggers to production.

The reason for this is that all Apex code must be covered by automated tests in order to be deployed to production.  If you tried to write a trigger in production, you wouldn't have a test so it wouldn't save.  If you tried to write the test first, the test would fail because your trigger wasn't working, so you couldn't save that either.  So instead, you do all of your development work in a non-production organization, where these rules are not enforced, and then you can deploy your code and tests to your production organization to make their functionality available to end users.

All Answers

Akhil AnilAkhil Anil
Hi Saad,

Your trigger would be simply this
 
trigger ClosedOpportunityTrigger on Opportunity (before update) {
    
    
    for(Opportunity o:Trigger.New) {
        if(o.Probability == 1) {
            
            Attachment a = new Attachment();
            try {
               a = [Select Id, Name from Attachment where ParentId =:o.Id];
            }
            catch(Exception e) {
               a = null;
            }
            
            if (a == null)
               o.addError('Add an attachment before you close the Opportunity');
        }
    }
    

}

Kindly test it and let me know how it goes !
Saad Ahmad 33Saad Ahmad 33
Thanks for sharing! I don't see the option to create a new trigger in my SF instance. I see APEX author permission set checked off on my profile. How can I fix this? 
Akhil AnilAkhil Anil
Hi Saad,

Are you checking in the Production environment ?

If yes, you cannot create or edit Apex classes or triggers in a production organization.

To use Apex classes or triggers in a production organization, you need to write them in a Sandbox or free Developer Edition organization--along with the required automated tests to meet or exceed code coverage requirements--and then use one of our deployment tools (the Force.com IDE or the Force.com Migration Tool for Ant) to push your classes and triggers to production.

The reason for this is that all Apex code must be covered by automated tests in order to be deployed to production.  If you tried to write a trigger in production, you wouldn't have a test so it wouldn't save.  If you tried to write the test first, the test would fail because your trigger wasn't working, so you couldn't save that either.  So instead, you do all of your development work in a non-production organization, where these rules are not enforced, and then you can deploy your code and tests to your production organization to make their functionality available to end users.
This was selected as the best answer
Saad Ahmad 33Saad Ahmad 33
Thank you. 
Saad Ahmad 33Saad Ahmad 33
Hey Akhil, 

How do I execute this code now or test it? First time running this, any help would be appreciated. 

Thanks
Akhil AnilAkhil Anil
Hi Saad,

You will have put this code in your sandbox environment first and test it there. Once you are confident that everything looks fine, you can deploy the code to your production using a changeset or a deployment tool like ANT. I would strongly recommend to pass this on to your development team if you are doing this for the first time.
Umadevi SUmadevi S
Hi Akhil,

Can you help me on the Class for the above trigger.