Share Your Experience With Others

Apex Trigger Use Case For Interview Practice

On Account object create two custom fields

  • Present prods(Number)
  • Won Opportunities(Number)

In Opportunity object there are 3 values in stage picklist values who sets the opportunity as closed won.

  • Closed Won
  • Got Payment
  • Got Partial Payment

Create a trigger on Opportunity object on after update event,which perform following operations:
Scenario 1: When Opportunity stage is set to won

  • Get list of all opportunity line items of that opportunity, and search for specific product(for example: TestProduct)
  • If the product is found in that opportunity, then perform following task:
  • Add the quantity of the product in the related account object field(present prods).
    Increment the Account field(won opportunities) by 1.

Scenario 2:When Opportunity stage is not set to won

  • Get list of all opportunity line items of that opportunity, and search for specific product(for example: TestProduct)
  • If the product is found in that opportunity, then perform following task:
  • Substract the quantity of the product in the related account object field(present prods).
  • Decrement the Account field(won opportunities) by 1.

Scenario 3:When Opportunity stage is changes in between Closed Won, Got Payment and Got Partial Payment.

  • Don’t perform any update in this case.

Create Trigger on Account Object:

  • On Account object if someone modify the data on(Present prods, Won Opportunities) field then no changes will updated on the record.

Also create test class for both the triggers and code should be bulkified.

I am also developing this one you can also practice.

Leave a comment