0

I need to make an apex class that needs to upsert hierarchically three times the same object. Currently, the only way I found to do this was this way:

private void upsertLevels()
{
    for(List<Transfer> lstTransfers : mapNiveisTransferencias.values())
    {
        List<Transferencia__c> lstTransferencias = new List<Transferencia__c>();

        for(Transfer iTransfer : lstTransfers)
        {
            Transferencia__c transferencia = convertTransferToTransferencia(iTransfer);

            if(String.isNotBlank(iTransfer.nivelSuperior))
            {
                transferencia.NivelSuperior__c = mapFoundTransfers.get(iTransfer.nivelSuperior).Id;
            }

            lstTransferencias.add(transferencia);
        }

        this.lstUpsertLevel.addAll(Database.upsert(lstTransferencias, Transferencia__c.Fields.CodTransferencia__c, false));

        for(Transferencia__c transferencia : lstTransferencias)
        {
            mapFoundTransfers.put(transferencia.CodTransferencia__c, transferencia);
        }
    }
}

The mapNiveisTransferencias.values () value may return a maximum of 3 items, so the loop would be looped a maximum of 3 times as well.   Keeping my Database.upsert within this loop can create performance issues even though it has delimited the number of iterations?

  • assuming this is a "service" provided to "clients" it may be called within a larger context more than once. If so, you may need to rethink your approach and possibly delegate the work to asynchronous tasks – cropredy Dec 3 '19 at 20:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.