0

I have a List of User who are enable to have ForecastingQuota:

List<User> resultsusers = new List<User>();
    resultsusers = [SELECT Id FROM User WHERE IsActive = TRUE AND ForecastEnabled = TRUE LIMIT 999];

And a List of the ForecastingQuota:

List<ForecastingQuota> resultsquotas = new List<ForecastingQuota>();
    resultsquotas = [SELECT Id,QuotaAmount,QuotaOwnerId FROM ForecastingQuota ORDER BY StartDate ASC];

I have make a Map of the User Id and his ForecastingQuotaAmount :

Map<Id, ForecastingQuota> fqsByOwnerId = new Map<Id, ForecastingQuota>();
        for (ForecastingQuota fq : resultsquotas)
        {
            fqsByOwnerId.put(fq.QuotaOwnerId, fq);
        }


        // new map of quotas keyed by all user ids
    Map<Id, ForecastingQuota> allUserFqsByOwnerId = new Map<Id, ForecastingQuota>();
        for (User u : resultsusers)
        {
              allUserFqsByOwnerId.put(u.id, fqsByOwnerId.get(u.id));
        }

But this Map show only User who have already a QuotaAmount, and not also the User who haven't a Quota, but I want the both in the same List.

0

I think the easiest method would be to add a Ternary statement, which acts as a shortcut for an if-else statement. The statement checks if the key already exists in the map using the containsKey map method, and either adds the data already in the map, or provides empty data.

The statement:

fqsByOwnerId.containsKey(u.id) ? fqsByOwnerId.get(u.id) : new ForecastingQuota()

Ends up looking something like this when fully expanded:

if ( fqsByOwnerId.containsKey(u.id) )
{ 
    fqsByOwnerId.get(u.id); 
} 
else 
{ 
    new ForecastingQuota();
}

You can see how using this in your code would allow you to add an empty ForecastingQuota without modifying your loop.

for (User u : resultsusers)
{
    allUserFqsByOwnerId.put(u.id, fqsByOwnerId.containsKey(u.id) ? fqsByOwnerId.get(u.id) : new ForecastingQuota());
}
  • Thanks that's work perfectly, but the name of the user does not appear when the user does not have a quota. – Skeo Oct 27 '16 at 7:47

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.