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
Max_gMax_g 

Trying to update or binsert quota records using trigger.

I am trying to update or insert forecastingquota records using the following code.  There are currently no rows in the object.  How do I process this when my initial selection returns no rows?

Currently I am only trying to insert 1 record per user which will be for January of the corrent year.  If I can get that working, then I can replicate for the other 11 months of the year.

Any suggestions will be greately appreciated.

  

trigger CreateQuota onStart_Budget__c (afterupdate) {

//Limit the size of list by using Sets which do not contain duplicate elements 

start_Budget__c sb = [select Update_Quota__c fromStart_Budget__climit 1];

system.debug('SB Update Quota = ' + sb.Update_Quota__c);

If (sb.Update_Quota__c ==true){

string YearOfToday = String.valueOf(Date.today().year());

string havequota ='YES';

 set<id>  Userids = newset<id>();  

for(Start_Budget__c sbp : trigger.new)

  { 

 list <User> p = [Select Id, Profile_Prefix__c, isactive from user where Profile_Prefix__c = 'Sale'and isactive = true];

 system.debug('P = ' + p);

 for (Integer i=0; i<p.size(); i++) {

if(p[i].Id != nullUserIds.add(p[i].Id); 

  }   

  }

//Map will contain one User Id to one sum value 

system.debug('Userids = ' + Userids);

system.debug('Userid size = ' + userids.size());

map<id,decimal> UserMap = newmap<id,decimal> ();

map<id,decimal> UserMap1 = newmap<id,decimal> ();

 

for(AggregateResult q : [select User__c,

sum(Jan_Amount__c) JAN,

sum(Feb_Amount__c)FEB,

sum(Mar_Amount__c)MAR,

sum(Apr_Amount__c)APR,

sum(May_Amount__c)MAY,

sum(June_Amount__c)JUNE,

sum(July_Amount__c)JULY,

sum(Aug_Amount__c)AUG,

sum(Sep_Amount__c)SEP,

sum(Oct_Amount__c)OCT,

sum(Nov_Amount__c)NOV,

sum(Dec_Amount__c)DEC

fromBudget__cwhere User__c IN :UserIds and text_year__c =: YearofToday groupbyrollup(User__c)])

{  

system.debug('Q = ' + q);

    UserMap.put((Id)q.get('User__c'),(decimal)q.get('JAN'));

  UserMap1.put((Id)q.get('User__C'),(decimal)q.get('FEB'));

 

 

}

  

system.debug('Have Quota Value = ' + havequota);

  List<ForecastingQuota> UsersToUpdate = new List<ForecastingQuota>();

for(ForecastingQuota o : [Select Id, QuotaOwnerId, StartDate,Quotaamount  fromForecastingQuotawhere QuotaOwnerId IN :UserIds])

  {  

system.debug('O initial = ' +o);

 if(o == null) havequota = 'NO';

 system.debug('Have Quota Value after selection = ' + havequota);

      Decimal PaymentSum = UserMap.get(o.Id);

         o.Quotaamount = 0;   

    system.debug('Quota Amount = ' + PaymentSum);  

         o.Quotaamount = PaymentSum;

     string QuotaDate ='01' + '01' + YearofToday;

        o.StartDate = date.valueOf(Quotadate);

     system.debug('qdate = ' + o.StartDate);   

     UsersToUpdate.add(o); 

  system.debug('Users to update = ' + UsersToUpdate);

  system.debug('Quota  = ' + o);   

  }

 update UsersToUpdate;

 

sb.Update_Quota__c =false;

system.debug('CreateQuota = ' + sb.Update_Quota__c);

update sb;

}

 

}

Best Answer chosen by Admin (Salesforce Developers) 
Max_gMax_g

Got the problem resolved.  Thanks for the help.

 

All Answers

Vinit_KumarVinit_Kumar

Make changes as below where you are updating 

 

if(UsersToUpdate.size()>0){

update UsersToUpdate;

}

Max_gMax_g

The problem is that I'm not getting my map value because there is no record from the forecastingquota object.  If there are no rows in the object, how do I get the values from the map to create the new rows?

 

Vinit_KumarVinit_Kumar

I don't see anywhere that you have created map for forecastingquota object.Am I missing somthing?

Max_gMax_g

I guess I'm not explaining very well what I am trying to accomplish.

I have a custom object that has the budget numbers for each account broken down by month and year to date. 

 

I am summing each of those months by account owner(User__c) so that I have the totals for each sales rep.

 

The first time I run this trigger for the year, there will be no rows in the quota object for any sales rep.  I need to be able to insert those records initially. 

 

If they update the budget after it is initially loaded, then I will run this trigger again and update the existing rows with the most current data.

 

My question is if I store the data in the usermap, how do I get it out  and populate the fields on the quota object to do an insert?

 

 

Vinit_KumarVinit_Kumar

Then,you should be using if and else condition if there is not values create a new record with all the values and then insert and in else condition you should be checking that if there is any record then update it with the correct id.

Max_gMax_g

I have a couple of other questions.

 

When I aggregate a set of totals, do they have to be placed in a usermap or can they be referenced immediately by the alias name?

 

If they are in a usermap how do you read them sequentially, or do you have to have an id to be able to retrieve the agregated value?

 

Max_gMax_g

Got the problem resolved.  Thanks for the help.

 

This was selected as the best answer