• SOPORTE TI
  • NEWBIE
  • 10 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 3
    Replies
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.
 
How can i do this with any object?  VFPage?  Triggers?   ApexClass?
Please, it's a critical problem for my partners!!!

User-added image

User-added image
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.
 
How can i do this with any object?  VFPage?  Triggers?   ApexClass?
Please, it's a critical problem for my partners!!!

User-added image

User-added image