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
Vagner AndradeVagner Andrade 

How to create a roll-up field with criteria?

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;

        }
    }
}


 
Best Answer chosen by Vagner Andrade
Raj VakatiRaj Vakati
try this code

https://help.salesforce.com/articleView?id=000005278&type=1​​​​​​​

http://force-salesforce.blogspot.com/2012/06/controlling-recursive-triggers.html​​​​​​​
 
public class flag {
public static boolean firstrun = true;
}

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;

            }
            if(Oppor.size()>0 && flag.firstrun ){
			flag.firstrun = false;
            update Oppor;
			}

        }
    }
}


 

All Answers

Raj VakatiRaj Vakati
try this code

https://help.salesforce.com/articleView?id=000005278&type=1​​​​​​​

http://force-salesforce.blogspot.com/2012/06/controlling-recursive-triggers.html​​​​​​​
 
public class flag {
public static boolean firstrun = true;
}

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;

            }
            if(Oppor.size()>0 && flag.firstrun ){
			flag.firstrun = false;
            update Oppor;
			}

        }
    }
}


 
This was selected as the best answer
Vagner AndradeVagner Andrade
You nailed it, man! Thank you!