0

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?

enter image description here

2
  • 1
    Do you reset the number to 1 every new year? If not, then you should be able to use the standard auto number data type. Use ID-{0000}-{yyyy} as the format. – Hengky Ilawan Jul 9 '20 at 12:20
  • 1
    this is likely a concurrency issue with multiple transactions executing the same trigger - you'll need to implement a Mutex on the process that controls custom setting fetch – cropredy Jul 10 '20 at 16:02
0

I think I found the issue.

The problema was that: MAPAUTONUM.get(opp.Year_Oportunidad__c).Numero__c += 1;

When actually I need to do this: MAPAUTONUM.get(year).Numero__c += 1;

2
  • Concurrency with multiple transactions would still be an issue though (as commented by @cropredy) – Hengky Ilawan Jul 11 '20 at 23:37
  • @HengkyIlawan how can I do that? I don't what is a mutex – Davidenserio Jul 15 '20 at 14:43

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.