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 ConnellySteve Connelly 

Need to create new custom object records from Opportunity record plus fields from another custom object.

Working in Lightning....

I am new to APEX. I nee dto build a trigger to create a custom object record from an Opportunity when a new Opp is created or the Close date changes.

I need to use fields from the Opp and from another custom object in the new record.

Can anyone point me to an article or some basic code to get me started?

There is more to this but I just want to get to whewre I can create the new record first. Sort of a learn by doing process.

Any help or suggestions is greatly appreciated.

Best regards,
Steve
Best Answer chosen by Steve Connelly
AbhishekAbhishek (Salesforce Developers) 
Hi Steve,

Maybe the below blogs might help you check it once,

https://success.salesforce.com/apex/answers?id=90630000000DLraAAG

https://success.salesforce.com/answers?id=9063A000000lDTUQA2

Thanks.

All Answers

AbhishekAbhishek (Salesforce Developers) 
Hi Steve,

Maybe the below blogs might help you check it once,

https://success.salesforce.com/apex/answers?id=90630000000DLraAAG

https://success.salesforce.com/answers?id=9063A000000lDTUQA2

Thanks.
This was selected as the best answer
Steve ConnellySteve Connelly
Thanks Abhishek, but these won't help me.

Probably my fault for not going into greater detail. 

I won't be able to use a Process Builder, Workflow or flow for this, i will need a trigger.


The end result I need is to create a new record for each quarter an Opportunity is scheduled to be open based upon the Created date and the Close date.

I have a custom object (Quarter Number) that has a record for each quarter for a number of years (Q1-2019, Q2-2019, Q3-2019, Q4-2019, Q1-2020, Q2-2020, and so fortht. These records also have an index number (1,2, 3, 4, etc).

I have created formula fields on the Opp that show the quarter that the opp was created based upon the Created date and the number of quarters that the Opp spans as well as the average Amount for each quarter.

Then I have a last custom object (Open Quarter) that is the record i want to create which will show the quarter number and the avg amount for each quarter plus a few other things. I need to create one of these records for each quarter the Opp spans. So if the Opp was created on February 14th 2019 and has a close date of June 3rd 2020 then it spans 6 quarters and I will need 6 records, like this: Q1-2019, Q2-2019, Q3-2019, Q4-2019, Q1-2020, Q2-2020. This will allow me to create some particular forcasting reports.

The trigger needs to do the following:
  • Fire any time a new Sales Opp is opened or when the Close date changes for an existing Sales Opp (using Process Builder)
  • Pull in the Opp ID and a few other fields including the three I describe above
  • Search for the Quarter Number record that matches the Quarter number in the Opp
  • Capture the number and index from above
  • Create a new Open Quarter record based upon the quarter that the Opp was created (based upon formula field)
  • Loop the process to create records for each quarter the Opp spans. Iterate the loop to create the same number of records as the "Quarters spanned field in the Opp.
  • In the event that the close date changes, i want the trigger to fire and delete any existing Open Quarter records and create new ones based upon the new close date

I did not go into all the detail as I just wanted to take this a step at a time and learn as i go. But as you can see above, I think that a trigger is the way that I have to go. I am fairly adept with Lightning Flow Builder but could not figure out a way to loop this process within a flow. This is why I am working on a trigger.

So I had planned to first build the part where the trigger grabs the Opp ID and required fields (when the trigger fires) and then build the part where it creates a new record with that data.

Then I was going to work on the loop by matching the index record  based upon the starting quarter and then iterate the loop based upon the quarters spanned filed.

And the then bat clean-up on other details like removing existing records if the close date changes, etc.

Now if my thinking about how to approach the peices are all wrong, I am open to suggestions. As I mentioned, I am crand new to coding and so I understand that what I have come up with may not be the best way.

But in the end, this is the project and I am pretty sure that a trigger is the best way to go.

And so I am looking for existing pieces that I can use as guides to build what I need.

Any other suggestions or thoughts?
Best regards,
 
Steve ConnellySteve Connelly
Here is my code so far. There are no error messages but it won't execute in the sandbox:
trigger createOq on Opportunity (after insert, after update) { 
    
    // trigger
    List <Open_Quarter__c> createOpenQuarter = new List <Open_Quarter__c>();
    List <Quarter_Number__c> useQuarterNumber = new List <Quarter_Number__c>();
    
    // for loop
    for (Opportunity opportunityList : Trigger.new) {
        
        // record type conditional check
        if (opportunityList.RecordTypeId == '01241000001USq9AAG') {
            
            // rest of the conditional checks
            if (Trigger.isInsert || (Trigger.isUpdate && opportunityList.CloseDate != Trigger.oldMap.get(opportunityList.Id).CloseDate)) {
                
                              
                Open_Quarter__c oq           = new Open_Quarter__C();
                Quarter_Number__c qn		 = new Quarter_Number__c();
                oq.Amount_Per_Quarter_oq__c  = opportunityList.Amount_per_Quarter_op__c;
                oq.Close_Date_oq__c          = opportunityList.CloseDate;
                oq.Name                      = qn.Quarter_Number_qn__c;
                oq.Opportunity_Name_oq__c    = opportunityList.Id;
                oq.Quarters_Spanned_oq__c    = opportunityList.Quarters_Spanned_op__c;
               
                decimal counter = qn.Index_Number__c;
                
                if(opportunityList.Quarter_Created_op__c == qn.Quarter_Number_qn__c)
                
                    While (counter < qn.Index_Number__c + opportunityList.Quarters_Spanned_op__c + 1)
                {    
                // while loop
                createOpenQuarter.add(oq);
                    counter++;
                } // end while loop
                
                try {
                    insert createOpenQuarter;
                }
                catch (system.Dmlexception e) {
                    system.debug (e);
                }
                
            } // end rest of the conditional checks
        } // end record type conditional check
    } // end for loop
} // end trigger

I do get this error in the sandbox:
Sandbox

Apex script unhandled trigger exception by user/organization: 00541000002c4kS/00D0j000000DCsc Source organization: 00D4100000137pQ (null)
createOq: execution of AfterUpdate

caused by: System.NullPointerException: Attempt to de-reference a null object

Trigger.createOq: line 26, column 1