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
Atomic_ForceAtomic_Force 

Help with Trigger - need to update multiple records after inserting into custom object

Really need some help!  I created a Trigger that inserts records into a custom object.  The after insert is working.  However, the trigger just keeps inserting records.  Need help with the after update part.  How do I use Old.map New.Map in this trigger?  OppShipMonthAssociation__c is a junction object.  Ship_Month__c is a custom object.  Code is below:

 

trigger insertOppShipMoAssoc on Opportunity (after insert, after update) {
	List<OppShipMonthAssociation__c> osmas = new List<OppShipMonthAssociation__c>();
    for (Opportunity newOpportunity: Trigger.New) {
    	for (integer i = 0; i < newOpportunity.Number_of_Ship_Months__c; i++ ) {
            if(newOpportunity.Book_Ship_in_QTR__c == 'Yes' && newOpportunity.ForecastCategoryName == 'Commit') {
                Osmas.add(new OppShipMonthAssociation__c(
                        Name = newOpportunity.Id,
                    	Opportunity__c = newOpportunity.Id,
                		Ship_Month__c = ));
                
                
            }
		}
		 insert Osmas;
	}
}

 

Best Answer chosen by Admin (Salesforce Developers) 
Srinu@dreamsSrinu@dreams

Use If(Trigger.isupdate) and put relevent condition in case of update

 

Thanks,

Srini

All Answers

Peter OpheimPeter Opheim

From your code it looks like you want to insert a certain number of opportunity ship month records when a new opportunity is inserted, based on the Number_of_Ship_Months__c value.  Are you also saying that if the user were to change the value in Number_of_Ship_Months__c, the code should then update the number of ship month records based on the new value?

 

Would you need to keep any existing opportunity ship month records that would still be applicable after the Number_of_Ship_Months__c value is changed?

 

Thanks,

 

Peter Opheim

Senior Software Developer

Sereno Software LLC

www.serenosoftware.com

Atomic_ForceAtomic_Force

Number_of_Ship_Months__c is a formula field on Opportunity object..  It uses a Case formula that grabs the numeric value of the month in CloseDate field on Opportunity and then gives a numeric result for each month. Decided to implement using a case formula to get around needing to query FiscalYearSetting in Trigger or Class.  Formula and list of additional business logic below:

 

Number_of_Ship_Months__c

 

CASE(MONTH( CloseDate ), 
1, 3, 
2, 2, 
3,1, 
4, 3, 
5, 2, 
6, 1, 
7, 3, 
8, 2, 
9, 1, 
10, 3, 
11, 2, 
12, 1, 
0)

 

 

The business logic I am trying to implement is listed below:

 

1.  Opportunity Created or Updated

2.  If after insert, then need to insert multiple records into a custom junction object which has master-detail relationship with both the Opportunity object and  custom a custom Ship_month__c object.

3.  Using for Loop, create no. of rows/records equal to numeric value in Opportunity.Number_of_Ship_Months__c field.  I will need to create some additional conditional statements or loops to handle increment and decrement part.

 

4.  I need help with the after update.  Trigger is inserting records everytime hit edit and save button.  Need trigger to look at Old.map for after update.

 

Appreciate the help.

Srinu@dreamsSrinu@dreams

Use If(Trigger.isupdate) and put relevent condition in case of update

 

Thanks,

Srini

This was selected as the best answer
Atomic_ForceAtomic_Force

Thanks