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
Steve Connelly 5Steve Connelly 5 

Could use some basic coding help

Good morning all,

I am trying to build a trigger that will create a related custom object  record when an Opportyunity is created or updated.

This is my code so far:
trigger createOpenQuarter on Opportunity (after insert,after update) {
     
    List <Open_Quarter__c> createOpenQuarter = new List <Open_Quarter__c> ();
        
    
        Open_Quarter__c oq = new Open_Quarter__c ();
        Quarter_Number__c qn = new Quarter_Number__c ();
    	Opportunity o = new Opportunity ();
    
         
        oq.Amount_Per_Quarter__c = o.Amount_per_Quarter__c; 
        oq.Close_Date__c = o.CloseDate;
        oq.Name = o.Quarter_Created__c;
        oq.Opportunity_Name__c = o.Name;
        oq.Quarters_Spanned__c = o.Quarters_Spanned__c;
         
        createOpenQuarter.add(oq);
         
    try {
        insert createOpenQuarter;
    } catch (system.Dmlexception e) {
        system.debug (e);
    }
}

I am trying to get this a step at a time. So I just want the first step to work.

The trigger is active in my sandbox so i was hoping that when I created a new Opp or updated an existing one that the trigger would create the custom record but nothing happens.

Can you let me know what I am missing or have done wrong? Once this part works i will work on the next step. Just trying to learn as i go.

I am brand new to coding so i appreciate your patience and any help i can get. I do not get any errors with the obove code, but nothing happens either.

Best regards,
Steve
Best Answer chosen by Steve Connelly 5
Khushmeet KaurKhushmeet Kaur

Hi Steve,

As your using an instance variable for the opportunity that will not contain any value. You might be getting the expectation that the required fields are missing, please refer to the code that might help you.
 
trigger createOpenQuarter 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__c = opportunityList.Amount_per_Quarter__c; 
        oq.Close_Date__c = opportunityList.CloseDate;
        oq.Name = opportunityList.Quarter_Created__c;
        oq.Opportunity_Name__c = opportunityList.Name;
        oq.Quarters_Spanned__c = opportunityList.Quarters_Spanned__c;
        createOpenQuarter.add(oq);

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


 

All Answers

Khushmeet KaurKhushmeet Kaur

Hi Steve,

As your using an instance variable for the opportunity that will not contain any value. You might be getting the expectation that the required fields are missing, please refer to the code that might help you.
 
trigger createOpenQuarter 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__c = opportunityList.Amount_per_Quarter__c; 
        oq.Close_Date__c = opportunityList.CloseDate;
        oq.Name = opportunityList.Quarter_Created__c;
        oq.Opportunity_Name__c = opportunityList.Name;
        oq.Quarters_Spanned__c = opportunityList.Quarters_Spanned__c;
        createOpenQuarter.add(oq);

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


 
This was selected as the best answer
Steve Connelly 5Steve Connelly 5
OK, I got a few errors at first biut sorted them out. I had fields with the same name in multiple objects and the trigger did not seem to like that so I made a few changes and now this is working. Thanks much.

One more question, How can I limit this to the creation of a particular Opp record type and to when the close date field is updated?

Thanks much,
Steve
Khushmeet KaurKhushmeet Kaur
Hi Steve,

Glad to help you.
trigger createOpenQuarter on Opportunity (after insert,after update) {

    List <Open_Quarter__c> createOpenQuarter = new List <Open_Quarter__c> ();
      

     for(Opportunity opportunityList : trigger.New){
        
      If(opportunityList.RecordTypeId == ' ' && CloseDate != NULL){

       Open_Quarter__c oq = new Open_Quarter__C();

        oq.Amount_Per_Quarter__c = opportunityList.Amount_per_Quarter__c; 
        oq.Close_Date__c = opportunityList.CloseDate;
        oq.Name = opportunityList.Quarter_Created__c;
        oq.Opportunity_Name__c = opportunityList.Name;
        oq.Quarters_Spanned__c = opportunityList.Quarters_Spanned__c;
        createOpenQuarter.add(oq);
      }
     }
 
    try {
            insert createOpenQuarter;
    } 
    catch (system.Dmlexception e) {
        system.debug (e);
    }
}
Please add the following if condition and specify your RecordTypeId.

 
Steve Connelly 5Steve Connelly 5
Sorry, but I do not see an if condition.
Steve
Khushmeet KaurKhushmeet Kaur
Hi Steve,

The if condition is at line number 8, After the for loop

 If(opportunityList.RecordTypeId == ' ' && CloseDate != NULL){

    // condition block
}

Thanks.
Steve Connelly 5Steve Connelly 5
Ahh, I see it. I added this and the part to limit it to the record type works. The second part was supposed to limit on existing opps to only when the Close Date field is changed. It still runs for all changes which is not what I was looking for.

This is the same problem I have been stuck on for a bit. Everything I tried either broke the trigger of failed to limit it to updates to the close date field only.

That is what threw me about the NULL bit. For opps, this is a required field, it can't be null. So sales reps have to enter one when the opp is created. Because of that, the Close Date field oftern gets changed after that fact. When that happens, i need the trigger to fire again. but only for changes to that one field.

Any suggestions?
Steve
Khushmeet KaurKhushmeet Kaur
Hi Steve,

As per my understanding, you only fire the trigger when a specific field is updated. You can compare the trigger.old values with the trigger.new values.

Please refer to the link as to compare :
http://www.sfdcpoint.com/salesforce/compare-old-and-new-field-values-in-trigger/

Thanks
 
Steve Connelly 5Steve Connelly 5
Just to make sure I am clrear here. The trigger should fire under two circumstances:

Any time a new Sales Opp is created

Any time the close date is changed for an existing Sales Opp.

Can that be done in a single if statement?

Steve
Khushmeet KaurKhushmeet Kaur
Hi Steve,

Yes as it can be done. For Example
if(trigger.isNew){
}
if(trigger.isUpdated){
    if(// Compare the trigger.old closed date as well as the sales rep as to confirm your scenario)
     {
        // Pass the trigger.new to the handler
     }
}


Thanks