My code:
trigger EsquemaComercializacion on Unidad_de_explotacion__c (before update) {
Set<String> set_UE = new Set<String>();
for(Unidad_de_explotacion__c u:trigger.new){
set_UE.add(u.Id);
}
List<Inventario__c> lstInventarios = new list<Inventario__c>();
Inventario__c modificaInventario;
if(trigger.isBefore && trigger.isUpdate){
Map<Id,Inventario__c> map_Inventarios = new Map<Id,Inventario__c>([SELECT Id, Estatus_de_ubicacion__c,Fecha_de_pago__c,Unidad_de_explotacion__c,
Unidad_de_explotacion__r.Esquema_de_Comercializaci_n__c,
Unidad_de_explotacion__r.Fecha_Solicitud_de_Reembolso_Garantia__c,
Unidad_de_explotacion__r.Paquete_en_Proceso_de_cierre__c,
Unidad_de_explotacion__r.Esquema_de_venta__c,
Unidad_de_explotacion__r.Esquema_de_Adquisicion__c,
Esquema_de_Adquisicion__c,
Metodo_de_Pago__c
FROM Inventario__c
WHERE Unidad_de_explotacion__c =:set_UE]);
List<Inventario__c> lstInv = [SELECT Id, Estatus_de_ubicacion__c,Fecha_de_pago__c, Unidad_de_explotacion__c,
Unidad_de_explotacion__r.Fecha_Solicitud_de_Reembolso_Garantia__c,
Unidad_de_explotacion__r.Esquema_de_Comercializaci_n__c,
Unidad_de_explotacion__r.Paquete_en_Proceso_de_cierre__c,
Unidad_de_explotacion__r.Esquema_de_venta__c,
Unidad_de_explotacion__r.Esquema_de_Adquisicion__c,
Esquema_de_Adquisicion__c,
Metodo_de_Pago__c
FROM Inventario__c WHERE Unidad_de_explotacion__c =:set_UE];
for(integer x=0;x<Trigger.new.size();x++){
if(trigger.old[x].Paquete_en_Proceso_de_cierre__c == false && trigger.new[x].Paquete_en_Proceso_de_cierre__c == true){
for(Inventario__c inv : lstInv){
modificaInventario = map_Inventarios.get(inv.Id);
IF(inv.Estatus_de_ubicacion__c == 'Vendido'){
modificaInventario.Esquema_de_comercializacion__c = inv.Unidad_de_explotacion__r.Esquema_de_venta__c;
modificaInventario.Esquema_de_Adquisicion__c = inv.Unidad_de_explotacion__r.Esquema_de_Adquisicion__c;
}else{
modificaInventario.Esquema_de_comercializacion__c = 'Tradicional';
modificaInventario.Fecha_de_pago__c = inv.Unidad_de_explotacion__r.Fecha_Solicitud_de_Reembolso_Garantia__c;
modificaInventario.Esquema_de_Adquisicion__c = 'Tradicional';
}
lstInventarios.add(modificaInventario);
}
}
if(trigger.old[x].Solicitud_de_Reembolso_Garantia__c == false && trigger.new[x].Solicitud_de_Reembolso_Garantia__c == true){
for(Inventario__c inv : lstInv){
modificaInventario = map_Inventarios.get(inv.Id);
if(trigger.new[x].Se_Aplico_Garant_a__c == true){
if(modificaInventario.Metodo_de_Pago__c == ''){
modificaInventario.Metodo_de_Pago__c = 'Pagado con Garantía';
}
}else{
if(modificaInventario.Metodo_de_Pago__c == ''){
modificaInventario.Metodo_de_Pago__c = 'Pagado con DEPOREF';
}
}
lstInventarios.add(modificaInventario);
}
}
}
try{
if(lstInventarios != null){
Update lstInventarios;
}
}catch(Exception ex){
if('System.DmlException'==ex.getTypeName()){
trigger.new[0].addError(ex.getDmlMessage(0));
}else{
trigger.new[0].addError(ex);
}
}
}
}
Hi Cesar,
Salesforce has a timeout limit for transactions based on CPU usage.
If transactions consume too much CPU time, we'll shut them down as a long-running transaction and this is the reason you're getting this error as
you are trying to process a large amount of data at a time.
Please check and make sure trigger is not called infinitely. Also, try to follow best practices in your code.
Helpful links are provided below:
https://developer.salesforce.com/forums/?id=9060G000000UXORQA4
https://help.salesforce.com/articleView?id=000232681&language=en_US&type=1
https://force201.wordpress.com/2014/11/09/fixing-a-common-cause-of-system-limitexception-apex-cpu-time-limit-exceeded/
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha