Learn Salesforce Developer from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Trigger Sceniors

Suresh Kumar Tavva
15/12/2016 0 0

Trigger Scenarios:- (Prepared By Suresh Kumar tvs)

Scenario one:-  (All Opportunity Triggers)

//Trigger to display error message when stage name is change from 'Order closed' to other values

Part one 1 (While trigger.old) :- 

if(Trigger.isupdate && Trigger.isBefore){

          for(Opportunity oldOpportunity:trigger.old){

             if(oldOpportunity.StageName == 'Order Closed'){

                  trigger.new[0].addError('An Opportunity cannot be edited once the opportunity stage is Order Closed');



Part 2(While Trigger.new):-

if(Trigger.isupdate && Trigger.isBefore){

set<id> opplistid=new set<id>();

                for(Opportunity newopportunity:Trigger.new){



                list<Quote> Qlist = [select OpportunityId,Customization_Payment_Criteria__c,Approval_Status_Quote__c from Quote where OpportunityId=:opplistid];

                for(Opportunity newopportunity: Trigger.new){

                 Opportunity old=trigger.oldmap.get(newopportunity.Id);

                    if(newopportunity.Record_Type_Name__c=='Watches' || newopportunity.Record_Type_Name__c=='Jewellery'||newopportunity.Record_Type_Name__c=='Lifestyle'){

                        if((old.StageName != newopportunity.StageName)){

                            for(Quote qq:Qlist){

                                if(qq.OpportunityId==newopportunity.id && qq.Customization_Payment_Criteria__c =='Free Of Cost' && qq.Approval_Status_Quote__c!='Approved'){

                                    newopportunity.addError('cannot change Stage as Quote Customization Payment Criteria is Free of cost and quote is not approved');







Scenario Two:-

//Trigger on checking attachment size and displaying error for different Record type

if(Trigger.isAfter && Trigger.isupdate){

        set<id> oppId=new set<id>();

        for(Opportunity newopportunity:Trigger.new){



        Integer Vattachmentcount = 0;       

        Integer Invoicecount = 0; 

            List<Attachment> ca = [Select id,Name from Attachment where  ParentId=:oppId limit 99];

            for(Integer i=0;i<ca.size();i++){

                if(ca[i].Name.indexOfIgnoreCase('validated')>=0 ){



                if(ca[i].Name.indexOfIgnoreCase('invoice')>=0 ){




        for(Opportunity newOpportunity:Trigger.new){

            if(newOpportunity.Record_Type_Name__c == 'Jewellery' && newOppor

tunity.Customization_Status__c == 'Validated' && (newOpportunity.StageName == 'Proforma Invoice'||newOpportunity.StageName == 'Received PO/Order Won' ||newOpportunity.StageName == 'Payment/Delivery' ||newOpportunity.StageName == 'Order Closed' )&& Vattachmentcount == 0){

                newOpportunity.addError('Please upload the appropriate file with the keyword "validated" included within the file name');


            if((newOpportunity.Record_Type_Name__c == 'Lifestyle' || newOpportunity.Record_Type_Name__c == 'Watches') && newOpportunity.Customization_Status__c == 'Validated' && (newOpportunity.StageName == 'Proforma Invoice'||newOpportunity.StageName == 'Received PO/Order Won' ||newOpportunity.StageName == 'Payment/Delivery' ||newOpportunity.StageName == 'Order Closed' ) && Vattachmentcount == 0){

                newOpportunity.addError('Please upload the appropriate file with the keyword "validated" included within the file name');


            if(newOpportunity.Delivery_Status__c == 'Completed' && newOpportunity.StageName == 'Payment/Delivery' && Invoicecount == 0){

                newOpportunity.addError('Please upload the appropriate file with the keyword "Invoice" included within the file name');





0 Dislike
Follow 0

Please Enter a comment


Other Lessons for You

Trigger Best Practices
Triggers - Best Practices in Salesforce 1. Future methods, SOQL and DML: Avoid writing Future methods, SOQL and DML inside the "For" loop.2. Bulkify the trigger: Start developing the logic...

Rajiv Kumar Singh

0 1

When To Use Before Triggers And When To Use After Triggers?
1. Before Triggers: To perform the validations we should use before triggers. If you are updating any field on the same object on which you are writing the trigger and no need to explicitly include the...

Salesforce Job based training
Hi All, Who are genuinely searching for a Salesforce job-based training? , That Job-based training will be provided here. 1.We will provide Salesforce Best Realtime Training 2.We will provide...

Assigning One permission to multiple users automatically without any manual work.
Hi Friends,I recently came across below new things I did in my project. Hope it helps you too.Description :How to Assign one permission set to multiple Users ? For example: If I want to assign ONE permission...

Difference Between Insert/Update And Database.Insert/ Database.Update
insert/update Database.insert/Database.update Assume that you are inserting 100 records. If any one of the record fail due to error then entire operation will fail. None of the records will be saved...

Looking for Salesforce Developer Classes?

The best tutors for Salesforce Developer Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Salesforce Developer with the Best Tutors

The best Tutors for Salesforce Developer Classes are on UrbanPro

Book a Free Demo

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more