0

enter image description here


I created a custom object budget which will store company budget for every month. I created opportunity related list into budget and I want to show all opportunities into that related list according to close date. Let say if any opportunity close date is Jan so it should be show under Jan budget. How can I achieve this do I need to create trigger to pull all opportunities into budget related list.

  • Are you creating a budget for every month? – Salesforcesmarty Jan 27 '16 at 6:52
  • Yes. Every month and opportunities who have same month close date should be show under that month budget. – Russel baker 1 Jan 27 '16 at 7:00
  • I did and opportunity related list come under budget but the problem is I want to show all opportunity whos closed date are in jan comes under jan budget and fen come under feb budget. Do I need any kind of trigger to pull opportunities under related list. @Bennie – Russel baker 1 Jan 27 '16 at 7:33
2

I believe you will need a trigger on update for opportunities whereby you will check the current month (i.e. Jan, Feb...) and based on the given month you will link the opportunities to that given budget. Obviously you will need a relationship between budget object and opportunity object.

So you will have something like this:

Trigger:

trigger OpportunityTrigger on Opportunity (after update) {
    OpportunityTriggerHandler.assignToBudget(Trigger.new);
}

Handler:

public class OpportunityTriggerHandler {
    public static void assignToBudget(List<Opportunity> opportunities) {
        List<Opportunity> updateOpportunities = new List<Opportunity>();
        for (Opportunity opportunity : opportunities) {
            //Check if opportunity is closed
            if (opportunity.StageName == 'Closed Won' || opportunity.StageName == 'Closed Lost') {
                //Check month on close date
                Date closeDate = opportunity.CloseDate;
                if (closeDate.month() == 1) {   // 1 is January, 2 is February etc
                    //Link opportunity to budget via field
                    Budget__c budget = [SELECT Id, Budget_Name__c FROM Budget__c WHERE Budget_Name__c =: 'Jan'];
                    if (budget != null) {
                        opportunity.Budget__c = budget.Id;
                        updateOpportunities.add(opportunity);
                    }
                }
            }
        }
        //Update opportunities
        update updateOpportunities;
    }
}
  • Hi,this code is working but I have to put budget Month(look field ) into opportunity. so that is showing under budget related list. – Russel baker 1 Jan 27 '16 at 9:25
  • @Russelbaker1 awesome, please mark as correct answer if it helped – Nelson Chisoko Jan 27 '16 at 9:53
  • You got me wrong, This code is not working as expected. I have created relation ship between opportunity and budget. So I have to manually update budget field on opportunity so it shows under budget. @Nelson Chisoko – Russel baker 1 Jan 27 '16 at 10:02
  • @Russelbaker1 so are you saying you will effectively do this manually on opportunities? Anyways the trigger is meant to do the update for you when the opportunity is closed. Hence the after update. Anyways if you have found a better solution then awesome :) – Nelson Chisoko Jan 27 '16 at 10:06
  • You got me wrong Nelson. I want it for open opportunities and closed won not closed lost. basically I want to use it as forecasting. So every month budget have related opportunity so we can forecast opportunity amount. I don't want to use standard forecasting of Salesforce. – Russel baker 1 Jan 27 '16 at 10:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.