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
SOPORTE TISOPORTE TI 

Curious problem - Trigger and Apex Task.

I have a curious problem with a trigger and a Apex task.
This trigger, Schedule an Apex Task to execute immediately.
Until now, this trigger doesn't show any problems, this Apex Task is executed immediately (at least, in less than one minute).

But, between 15% or 20% of all the cases, SalesForce put "in queued" this task!. And i don't know why, but this Apex Task runs in 5, 10 or 15 minutes o more! (the time execution is random), and i need this Apex task works immediately.

Mi question is: How can i programming in a better way this trigger?
it's a Trigger problem? It's a SalesForce Problem?
 
public with sharing class ProcesoConsultaPrecios {
  
  public static Boolean allow = true;

@Future(CallOut=true)
public static void consultar(Set<Id> itemIds) {
  OpportunityLineItem[] olis = [SELECT Id, Opportunity.CurrencyIsoCode, PriceBookEntry.Product2.Status_Precio_con_Descuento__c, PriceBookEntry.Product2.Codigo_Sustituto__c, Codigo_de_Unidad_de_Medida_para_Venta__c, Opportunity.Codigo_de_Unidad__c, Opportunity.Codigo_Tienda__c, PriceBookEntry.Product2.ProductCode, Opportunity.Codigo_Empresa__c, Opportunity.Account.Codigo_Cliente__c, PriceBookEntry.Product2.CurrencyIsoCode, Opportunity.Codigo_de_Almacen__c FROM OpportunityLineItem WHERE Id IN :itemIds AND PriceBookEntry.Product2.ProductCode NOT IN (NULL, '')];
  WsConsultaPrecios.WSCRMMCONSSoap service = new WsConsultaPrecios.WSCRMMCONSSoap();
  service.endpoint_x = '';
  service.timeout_x = 120000;
  WsConsultaPrecios.ArrayOfTMITEM_CRMMRequest request = new WsConsultaPrecios.ArrayOfTMITEM_CRMMRequest();
  List<WsConsultaPrecios.TMITEM_CRMMRequest> items = new List<WsConsultaPrecios.TMITEM_CRMMRequest>();
  request.TMITEM_CRMMRequest = items;
  for (OpportunityLineItem oli : olis) {
    WsConsultaPrecios.TMITEM_CRMMRequest item = new WsConsultaPrecios.TMITEM_CRMMRequest();
    String codigoAlmacen = oli.Opportunity.Codigo_de_Almacen__c;
    item.CO_ALMA = codigoAlmacen == null ? '' : (codigoAlmacen.length() < 3 ? codigoAlmacen : codigoAlmacen.substring(0,3));
    items.add(item);
  }
  
  if (Test.isRunningTest())
  Test.setMock(WebServiceMock.class, new FN_CONS_PRODMock());
  WsConsultaPrecios.ArrayOfTMITEM_CRMM response = service.fn_CONS_PROD(request);
  System.assertNotEquals(null, response.TMITEM_CRMM, 'No hubo respuesta del servicio');
  System.assertEquals(response.TMITEM_CRMM.size(), olis.size(), 'El arreglo de salida del servicio no tiene el mismo largo que el de entrada.');
  for (Integer i=0; i < response.TMITEM_CRMM.size(); i++) {
    WsConsultaPrecios.TMITEM_CRMM item = response.TMITEM_CRMM[i];
        OpportunityLineItem oli = olis[i];
        if (oli != null) {
                oli.Precio_Real_OFISMART__c = item.IM_PREC;
        oli.UnitPrice = item.IM_PREC;
        oli.Sincronizado_ERP_Ofismart__c = true;
        }
  }
      if (olis.size() > 0) {
        allow = false;
  update olis;
  allow = true;
      }

      }
  
  
}

Note: Sorry for my english... it's a little lame. xD

Thanks for you help.
 
Best Answer chosen by SOPORTE TI
Pankaj_GanwaniPankaj_Ganwani
Hi,

The jobs are queued because batch is of asunchronous type unlike triggers. ie. it executes when the resources are available. The job remains in a queue status unless it gets enough resources to be executed.

All Answers

Pankaj_GanwaniPankaj_Ganwani
Hi,

The jobs are queued because batch is of asunchronous type unlike triggers. ie. it executes when the resources are available. The job remains in a queue status unless it gets enough resources to be executed.
This was selected as the best answer
SOPORTE TISOPORTE TI
Thanks Pankaj
So there is no way to guarantee a automatic response (less 1 minute) in SalesForce? It's almost random the execution time?

 
Pankaj_GanwaniPankaj_Ganwani
Yes, The execution time for batch processes, future methods and call outs are random in salesforce since these are of asynchronous nature.