• Vagner Andrade
  • NEWBIE
  • 10 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
Hi,

I need to create a custom field on opportunity that shows the amount of all opportunities with the same CloseDate month and year.

I've tried a trigger, but i'm geting this error "SELF_REFERENCE_FROM_TRIGGER". I can't update the field of the record that fires the trigger.

How can I reach this? 

Trigger:
 
trigger calculateTotalAmount on Opportunity(after update, after insert) {

    double totalAmount;
    date firstDayReference;
    date lastDayReference;
    Integer daysInMonth;

    If(Trigger.IsInsert || Trigger.IsUpdate) {
        For(Opportunity opp: Trigger.New) {

            daysInMonth = Date.daysInMonth(opp.CloseDate.year(), opp.CloseDate.month());
            firstDayReference = date.newinstance(opp.CloseDate.year(), opp.CloseDate.month(), 1);
            lastDayReference = date.newinstance(opp.CloseDate.year(), opp.CloseDate.month(), daysInMonth);

            List < Opportunity > Oppor = [SELECT ID, Name, CloseDate, Amount
                                            FROM Opportunity
                                           WHERE CloseDate >=: firstDayReference AND CloseDate <=: lastDayReference AND StageName = 'Closed Won'
                                         ];


            For(Opportunity o: Oppor) {

                totalAmount = totalAmount + o.Amount;
            }


            For(Opportunity o: Oppor) {
                o.total_amount__c = totalAmount;

            }
            
            update Oppor;

        }
    }
}


 
Hi,

I need to create a custom field on opportunity that shows the amount of all opportunities with the same CloseDate month and year.

I've tried a trigger, but i'm geting this error "SELF_REFERENCE_FROM_TRIGGER". I can't update the field of the record that fires the trigger.

How can I reach this? 

Trigger:
 
trigger calculateTotalAmount on Opportunity(after update, after insert) {

    double totalAmount;
    date firstDayReference;
    date lastDayReference;
    Integer daysInMonth;

    If(Trigger.IsInsert || Trigger.IsUpdate) {
        For(Opportunity opp: Trigger.New) {

            daysInMonth = Date.daysInMonth(opp.CloseDate.year(), opp.CloseDate.month());
            firstDayReference = date.newinstance(opp.CloseDate.year(), opp.CloseDate.month(), 1);
            lastDayReference = date.newinstance(opp.CloseDate.year(), opp.CloseDate.month(), daysInMonth);

            List < Opportunity > Oppor = [SELECT ID, Name, CloseDate, Amount
                                            FROM Opportunity
                                           WHERE CloseDate >=: firstDayReference AND CloseDate <=: lastDayReference AND StageName = 'Closed Won'
                                         ];


            For(Opportunity o: Oppor) {

                totalAmount = totalAmount + o.Amount;
            }


            For(Opportunity o: Oppor) {
                o.total_amount__c = totalAmount;

            }
            
            update Oppor;

        }
    }
}