I have a custom setting to numerate the opps by year.
When an opp is insert, it fires a trigger that call to the method 'renombrarOppEmpresa'.
I look for the record of the custom setting per year, I look for its value in the field Numero__c and I use it in the name of the opportunity. Then I increase the value of the Numero__c field of the corresponding custom setting record by 1.
public static void renombrarOppEmpresa(List<Opportunity> listaOpps){
system.debug('Clase OportunityMethods, renombrarIdOppEmpresa');
Set<AutoNumberOppCorporativo__c> setAutoNumberParaModificar = new Set<AutoNumberOppCorporativo__c>();
String num = '';
String idOpp = '';
//Integer year;
String year = '';
for(Opportunity opp: listaOpps){
//year = opp.CloseDate.year();
year = String.valueOf(system.now().year());
//system.debug('year = ' + year);
system.debug('year = ' + year);
num = String.valueOf((Integer)MAPAUTONUM.get(year).Numero__c);
system.debug('num = ' + num);
while(num.length() < 4){
num = '0' + num;
}
idOpp = 'ID-' + num + '-' + year;
system.debug('idOpp = ' + idOpp);
system.debug('num = ' + num);
system.debug('opp.Name = ' + opp.Name);
system.debug('opp.Id_de_OportunidadBIS__c = ' + opp.Id_de_OportunidadBIS__c);
system.debug('opp.Nombre_del_Evento__c = ' + opp.Nombre_del_Evento__c);
opp.Id_de_OportunidadBIS__c = idOpp;
opp.Nombre_del_Evento__c = opp.Name;
opp.Name = opp.Id_de_OportunidadBIS__c + ' | ' + opp.Nombre_del_Evento__c;
MAPAUTONUM.get(opp.Year_Oportunidad__c).Numero__c += 1;
setAutoNumberParaModificar.add(MAPAUTONUM.get(year));
}
if(!setAutoNumberParaModificar.isEmpty()){
upsert new List<AutoNumberOppCorporativo__c>(setAutoNumberParaModificar) Name;
}
}
At first I thought it was working fine, but I have noticed that the value is being repeated in several opportunity records.
Does it occur to you that it may be failing?
ID-{0000}-{yyyy}
as the format.