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
chubsubchubsub 

Before Update Trigger to sequence Opportunity Names

I'm scratching my head on this one.  What I'm trying to is update previous opportunity names in sequece by the date they were created and the account they were created for.  The trigger I am writing will update a field on the opportunity called Daily_Tracking_Number__c, but only if there is a date in the SOF_Requested_Date field.  

 

The daily tracking number field is then incorporated into a workflow that will update the opportunity name with the daily tracking number on the end "[OppName - Date - Daily Tracking Number".

 

For example:

Client A had 5 Opportunities created within the date range of 5/01/2010 and 6/01/2010.  Once these are updated, I'm trying to have the Daily_Tracking_Number__c field update in sequence by the CreatedDate.  Below is a chart of what I'm trying to explain:

 

Client A:

Opportunity created on 5/1 - Daily_Tracking_Number will = 1

Opportunity created on 5/4 - Daily_Tracking_Number will = 2

Opportunity created on 5/10 (but there is no date in the SOF Releasted Date - Daily_Tracking_Number will = 0

Opportunity created on 5/12 - Daily_Tracking_Number will = 3

 

Below is the code I have so far, but it does not work properly.  I'm not getting any error messages, but it's not updating the daily tracking number field:

 

Any ideas would be greatly appreciated!

 

Trigger:

 

trigger ManageOpportunitiesBeforeUpdate on Opportunity (before update) {   
    
        if(Trigger.isUpdate && Trigger.isBefore){

        ManageOpportunitiesBeforeUpdate.beforeUpdate(Trigger.New); 
    }   
} //end trigger

 

 

 

 

 

Class referenced by the trigger:

 

public static void beforeUpdate(list<Opportunity> optys)
{
set<Id> accids = new set<id>();
set<Date> createdDates = new set<Date>();

for (Opportunity o : optys)
{
if (o.SOF_Requested_Date__c == null)
{
accids.add(o.AccountId);
createdDates.add(o.CreatedDate.date());
}
}

//this will include the opps we need to count
Id thisaccid;
Date thisdate;
list<Opportunity> allopps = [select Id, AccountId, CreatedDate
from Opportunity
where Id in :accids
and CreatedDate in :createdDates
order by AccountId, CreatedDate];
map<Id, Integer> oppid2seqnummap = new map<Id, Integer>();

//reset placeholders: last Account, last Date, current counter - JS alerted 11/17 to check the size of the list before getting the value
if(allopps.size()>0){
thisaccid = allopps[0].AccountId;
thisdate = allopps[0].CreatedDate.date();
}
Integer counter = 0;


//keep track of all sequence numbers
for (Opportunity o : allopps)
{

//same account as the last one?
if (thisaccid == o.AccountID)
{
//same date as the last one?
if (thisdate == o.CreatedDate.date())
{
counter++;
}
else //new date
{
counter = 1;
}
}
else //new account
{
counter = 1;
}

//store counter for this Opp
oppid2seqnummap.put(o.Id, counter);

//reset to compare to next Opp
thisdate = o.CreatedDate.date();
thisaccid = o.AccountId;
}

//now go back through passed Opp list
for (Opportunity o :optys)
{
Integer seqnum = oppid2seqnummap.get(o.Id);
//TODO: populate Name field or store seq num somewhere

o.Daily_Tracking_Number__c = seqnum;

 


}

} //end before update


} //end class


 

aebenaeben

I haven't gone through the whole code. But, I see an obvious mistake.

 

In the query list<Opportunity> allopps = [select Id, AccountId, CreatedDate
from Opportunity
where Id in :accids
and CreatedDate in :createdDates
order by AccountId, CreatedDate];

 

you are trying to match opportunity Id to account ids. 

 

Thanks

chubsubchubsub

Thanks, good catch aeben.  I fixed that, but it's still not updating the daily tracking number like it should.  It remains null after I save the record.  I put the following code in the execute anonymous and it executed successfully:  I'm not sure where I'm going wrong.

 

Thanks for you help, it's greatly appreciated. 

 

public static void beforeUpdate(list<Opportunity> optys)
{
set<Id> accids = new set<id>();
set<Date> createdDates = new set<Date>();

for (Opportunity o : optys)
{
if (o.SOF_Requested_Date__c == null)
{
accids.add(o.AccountId);
createdDates.add(o.CreatedDate.date());
}
}

Id thisaccid;
Date thisdate;
list<Opportunity> allopps = [select Id, AccountId, CreatedDate
from Opportunity
where AccountId in :accids
and CreatedDate in :createdDates
order by AccountId, CreatedDate];
}

chubsubchubsub

Here are some other updates I made:

 

Line 124:      o.Daily_Tracking_Number__c = counter;

 

Line 85:  Integer counter = 1;

 

 

It will still let me save it, but it will always update the daily tracking number to the number I set in Line 85, which is one now.