• coddy nic
  • 5 Points
  • Member since 2020

  • Chatter
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
  • 1
I have a new trigger that creates a custom object record when an opportunity is created or updated.

Everything works but I need help with one thing. I only want it to run when a specific opportunity type is created or when that type has its "cloe date" field updated.

Here is my code:
trigger createOq on Opportunity (after insert, after update) {

    List <Open_Quarter__c> createOpenQuarter = new List <Open_Quarter__c> (); 
     for(Opportunity opportunityList : trigger.New){
       Open_Quarter__c oq = new Open_Quarter__C();

        oq.Amount_Per_Quarter_oq__c = opportunityList.Amount_per_Quarter_op__c; 
        oq.Close_Date_oq__c = opportunityList.CloseDate;
        oq.Name = opportunityList.Quarter_Created_op__c;
        oq.Opportunity_Name_oq__c = opportunityList.Id;
        oq.Quarters_Spanned_oq__c = opportunityList.Quarters_Spanned_op__c;

    try {
            insert createOpenQuarter;
    catch (system.Dmlexception e) {
        system.debug (e);

How do I limit this so it only runs when a "Sales" opp is created or when an existing Sales Opp has the "Close Date" field updated?

New to coding and apex so any help is greatly appreciated.

Best regards,