1

I have a requirement where, the client needs to plan a budget on Month/Country basis.

  • First select the % of budget that the Month Gets. Lets call it x.
  • Then select the the % of x each country gets for that month.

Here is a picture showing the requirement. Image showing the requirement, all % are drop down list with multiple of 5

What would be the best approach to achieve this? The month and country are custom objects and Country is the child object of Month. The month and country object are created at the same time, judging from the start and end date of a Campaign and all data should be committed to database on 'Save'.

I don't want the exact code, but any pseudo code, or suggestion would be highly appreciated.

Many thanks.

  • Does the number of countries vary by month? Is the Country object truly a child of Month or is there a join object in the middle? – user735 Feb 4 '15 at 23:08
  • No, the number of countries does not vary by month, they remain same. Yes, Country is truly a child object, there is no junction object in the middle. – ankittaneja Feb 5 '15 at 17:01
1

I'd do everything but the sums with VF/Apex. You can create a selectOption array for the percentages (if that is OK with business). On all of the selectOptions add an onchange javascript event that will the sum the column. Once you click submit, validate the sums with JS if you need to, then fire an actionFunction which submits the records to the controller where you insert them.

On the controller side, make a wrapper class that holds the country and the list of months.

When the wrapper class is returned, loop through and pull the countries out, while creating a map of the country name to the months.

Insert the countries.

Loop through the countries and add the new Id to the months for the country.

Insert the months.

There are many ways to do it, but this seems the most straight forward.

JS Example for sums:
var janTotal = 0;
function sumJan()
{
  janTotal = 0;
  for(var i = 0; i < {!countries.size}; i++)
    janTotal += document.getElementById("page:form:block:"+i);
}


APEX Wrapper Class Example:
public class CountryMonthWrapper
{
  public Country__c country {get;set;}
  public Month__c[] months {get;set;}
  public CountryMonthWrapper(Country__c c,Month__c[] m)
  {
    this.country = c;
    this.months = m;
  }
}

Page Method Maps Looping Example
Country__c[] countries = new Country__c[]{};
Map<String,List<Month__c>> countryToMonths = new Map<String,List<Month__c>>();
Month__c[] allMonths = new Month__c[]{};
for(CountryMonthWrapper cmw:cmws)
{
  countries.add(cmw.country);
  countryToMonths.put(cmw.country.name,cmw.months);
}
insert countries;

for(Country__c c:countries)
{
  for(Month__c m:countryToMonths.get(c.name))
  {
    m.Country__c = c.Id;
    allMonths.add(m);
  }
}
insert months;
  • Thanks for the reply, but u missed an important information: Country is the child object of the month, so I cannot insert countries before months. The other problem, there is a percentage row, just below month names which accepts percentage for the month and in the other places, its the percentage of the above selected value. – ankittaneja Feb 4 '15 at 21:44
  • I guess that makes sense, you're planning on using a rollup to get the totals inside of salesforce. In that case, you can flip all of the apex code to accommodate the opposite. As far as the JS part of the solution, you have to find the logical argument to create these functions. If a single cell is updated, how does that affect the sums happening on the page. Take this answer and add it to the function. You can do near anything with JS. – Pete O'Connell Feb 5 '15 at 22:08

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.