• SalesForce 13
  • NEWBIE
  • 10 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 2
    Replies
I have this validation rule in status, where it has 3 values (analysis, process, completed), all the profiles can assign these 3 statuses, but once it is in the completed value you must activate the rule where only the administrator can change that value 
I have it like this:

AND(
          ISPICKVAL(Status__c, "completed"), 
          RecordType.Name = "Sales", 
          NOT( ISNEW()), 
          $Profile.Name <> "System Administrator"
)

Could you help me?
I have a problem with the limit of queries in my trigger this error marks me: " System.LimitException: Too many query rows: 50001" when testing the test class, I already made a batch because according to that I could overcome the error that I sent when wanting to implement it in production, but keep sending me the error , any help or guide?

Trigger:
trigger TotalOficina on Account (after delete,after update, after insert, after undelete) {
List<String> listNumOficina = new List<String>();

	if(trigger.isInsert) {
		for(Account itemAccount : Trigger.New) {
			if(itemAccount.Unidad_Ejecutora__c != null && itemAccount.Unidad_Ejecutora__c.trim() != '') {
				if (itemAccount.Suma_Total_Captaci_n__c != 0) {
				listNumOficina.add(itemAccount.Unidad_Ejecutora__c);
				}
			}
		}
			if (listNumOficina.size() > 0) {
			Database.executeBatch(new cBatchTotal(listNumOficina));
			}
	}
		else if(trigger.isDelete) {
			for(Account itemAccount : Trigger.Old) {
				if(itemAccount.Unidad_Ejecutora__c != null && itemAccount.Unidad_Ejecutora__c.trim() != '') {
					if (itemAccount.Suma_Total_Captaci_n__c != 0) {
					listNumOficina.add(itemAccount.Unidad_Ejecutora__c);
					}
				}
			}
					if (listNumOficina.size() > 0) {
					Database.executeBatch(new cBatchTotal(listNumOficina));
					}
		}
		else if(trigger.isUnDelete) {
			for(Account itemAccount : Trigger.New) {
				if(itemAccount.Unidad_Ejecutora__c != null && itemAccount.Unidad_Ejecutora__c.trim() != '') {
					if (itemAccount.Suma_Total_Captaci_n__c != 0) {
					listNumOficina.add(itemAccount.Unidad_Ejecutora__c);
					}
				}
			}
					if (listNumOficina.size() > 0) {
					Database.executeBatch(new cBatchTotal(listNumOficina));
					}				
		}
		else if(trigger.isUpdate) {
		System.debug('CEAM CANTIDAD TRIGGER: ' + Trigger.New.size());
			for(Account itemAccount : Trigger.New) {
				if(itemAccount.Unidad_Ejecutora__c != null && itemAccount.Unidad_Ejecutora__c.trim() != '') {
					if (itemAccount.Suma_Total_Captaci_n__c != 0) {
					listNumOficina.add(itemAccount.Unidad_Ejecutora__c);
					}
				}
			}
					if (listNumOficina.size() > 0) {
					Database.executeBatch(new cBatchTotal(listNumOficina));
						}
		}
}

Batch
global class cBatchTotal implements Database.Batchable<sObject> {
	List<String> listNumOficina = new List<String>();
	Map<String, Oficina__c> oficinaMap = new Map<String, Oficina__c>();

	global cBatchTotal(List<String> listNumOficina) {
		this.listNumOficina = listNumOficina;
	}

	global Database.QueryLocator start(Database.BatchableContext BC) {
		return DataBase.getQueryLocator([SELECT Id, UEJ__c FROM Oficina__c WHERE UEJ__c IN :this.listNumOficina]);
    }

    global void execute(Database.BatchableContext BC, List<Oficina__c> listOficina)
    {
		for(Oficina__c oOficina : listOficina) {
			oficinaMap.put(oOficina.UEJ__c, new Oficina__c(Id = oOficina.Id, UEJ__c = oOficina.UEJ__c, Total_Captacion_Oficina__c = 0));
		}

		for(Account cuenta : [SELECT Unidad_Ejecutora__c, Suma_Total_Captaci_n__c FROM Account WHERE Unidad_Ejecutora__c IN :this.listNumOficina]) {
			if (cuenta.Suma_Total_Captaci_n__c != null && cuenta.Suma_Total_Captaci_n__c != 0) {
				oficinaMap.get(cuenta.Unidad_Ejecutora__c).Total_Captacion_Oficina__c += cuenta.Suma_Total_Captaci_n__c;
			}
		}
		Database.update(oficinaMap.values());
    }

    global void finish(Database.BatchableContext BC) {

    }
}

And my @Test:
@isTest(SeeAllData=true)
private class TestctrlTotalOficina {
    Private static testMethod void insertAccountTest() {
        Test.startTest();
		//To Insert Account
		 
        Account oCuenta = new Account();
        oCuenta.Name = 'Prueba Nombre';
        oCuenta.Unidad_Ejecutora__c = '205';
		try {
			insert oCuenta;
		} catch(Exception e) {
			e.getCause();
		}
		Test.stopTest();
    }

	Private static testMethod void updateAccountTest() {
        // To Update Account
		///////////////////////////////////////////
        List<Account> acctList = [SELECT Name, Unidad_Ejecutora__c FROM Account];
			//for(Account acct :acctList){
   			 //acct.Name = 'dasdds';
			//}
		update acctList;
///////////////////////////////////////////
        
        Account oCuenta = new Account();
		//oCuenta.Id = '0014P000029eezDQAQ';
        oCuenta.Name = 'Prueba Nombre';
        oCuenta.Unidad_Ejecutora__c = '205';
		insert oCuenta;
		oCuenta.Name = 'Otra Prueba';
      //  oCuenta.Suma_Total_Captaci_n__c = 0;
		Test.startTest();
        try {
			update oCuenta;
		} catch (Exception e) {
			e.getCause();
		}
		Test.stopTest();
    }
    	Private static testMethod void deleteAccountTest() {
        // To Delete Account
		Account oCuenta = new Account();
        oCuenta.Name = 'Prueba Nombre';
        oCuenta.Unidad_Ejecutora__c = '205';
		insert oCuenta;
        oCuenta = [SELECT Id, Name, Unidad_Ejecutora__c FROM Account WHERE Id =:oCuenta.Id];
		Test.startTest();
        try {
			Delete oCuenta;
		} catch (Exception e) {
			e.getCause();
		}
		Test.stopTest();
    }

	Private static testMethod void undeleteAccountTest() {
		// To UnDelete Account
        Account oCuenta = new Account(Name = 'Prueba', Unidad_Ejecutora__c = '205');
		insert oCuenta;
		delete oCuenta;
		Account[] aCuenta = [SELECT Id, Name, Unidad_Ejecutora__c FROM Account WHERE Name = 'Prueba' ALL ROWS];
		try {
			undelete aCuenta;
		} catch (Exception e) {
			e.getCause();
		}
    }
}



 
I have this validation rule in status, where it has 3 values (analysis, process, completed), all the profiles can assign these 3 statuses, but once it is in the completed value you must activate the rule where only the administrator can change that value 
I have it like this:

AND(
          ISPICKVAL(Status__c, "completed"), 
          RecordType.Name = "Sales", 
          NOT( ISNEW()), 
          $Profile.Name <> "System Administrator"
)

Could you help me?
I have a problem with the limit of queries in my trigger this error marks me: " System.LimitException: Too many query rows: 50001" when testing the test class, I already made a batch because according to that I could overcome the error that I sent when wanting to implement it in production, but keep sending me the error , any help or guide?

Trigger:
trigger TotalOficina on Account (after delete,after update, after insert, after undelete) {
List<String> listNumOficina = new List<String>();

	if(trigger.isInsert) {
		for(Account itemAccount : Trigger.New) {
			if(itemAccount.Unidad_Ejecutora__c != null && itemAccount.Unidad_Ejecutora__c.trim() != '') {
				if (itemAccount.Suma_Total_Captaci_n__c != 0) {
				listNumOficina.add(itemAccount.Unidad_Ejecutora__c);
				}
			}
		}
			if (listNumOficina.size() > 0) {
			Database.executeBatch(new cBatchTotal(listNumOficina));
			}
	}
		else if(trigger.isDelete) {
			for(Account itemAccount : Trigger.Old) {
				if(itemAccount.Unidad_Ejecutora__c != null && itemAccount.Unidad_Ejecutora__c.trim() != '') {
					if (itemAccount.Suma_Total_Captaci_n__c != 0) {
					listNumOficina.add(itemAccount.Unidad_Ejecutora__c);
					}
				}
			}
					if (listNumOficina.size() > 0) {
					Database.executeBatch(new cBatchTotal(listNumOficina));
					}
		}
		else if(trigger.isUnDelete) {
			for(Account itemAccount : Trigger.New) {
				if(itemAccount.Unidad_Ejecutora__c != null && itemAccount.Unidad_Ejecutora__c.trim() != '') {
					if (itemAccount.Suma_Total_Captaci_n__c != 0) {
					listNumOficina.add(itemAccount.Unidad_Ejecutora__c);
					}
				}
			}
					if (listNumOficina.size() > 0) {
					Database.executeBatch(new cBatchTotal(listNumOficina));
					}				
		}
		else if(trigger.isUpdate) {
		System.debug('CEAM CANTIDAD TRIGGER: ' + Trigger.New.size());
			for(Account itemAccount : Trigger.New) {
				if(itemAccount.Unidad_Ejecutora__c != null && itemAccount.Unidad_Ejecutora__c.trim() != '') {
					if (itemAccount.Suma_Total_Captaci_n__c != 0) {
					listNumOficina.add(itemAccount.Unidad_Ejecutora__c);
					}
				}
			}
					if (listNumOficina.size() > 0) {
					Database.executeBatch(new cBatchTotal(listNumOficina));
						}
		}
}

Batch
global class cBatchTotal implements Database.Batchable<sObject> {
	List<String> listNumOficina = new List<String>();
	Map<String, Oficina__c> oficinaMap = new Map<String, Oficina__c>();

	global cBatchTotal(List<String> listNumOficina) {
		this.listNumOficina = listNumOficina;
	}

	global Database.QueryLocator start(Database.BatchableContext BC) {
		return DataBase.getQueryLocator([SELECT Id, UEJ__c FROM Oficina__c WHERE UEJ__c IN :this.listNumOficina]);
    }

    global void execute(Database.BatchableContext BC, List<Oficina__c> listOficina)
    {
		for(Oficina__c oOficina : listOficina) {
			oficinaMap.put(oOficina.UEJ__c, new Oficina__c(Id = oOficina.Id, UEJ__c = oOficina.UEJ__c, Total_Captacion_Oficina__c = 0));
		}

		for(Account cuenta : [SELECT Unidad_Ejecutora__c, Suma_Total_Captaci_n__c FROM Account WHERE Unidad_Ejecutora__c IN :this.listNumOficina]) {
			if (cuenta.Suma_Total_Captaci_n__c != null && cuenta.Suma_Total_Captaci_n__c != 0) {
				oficinaMap.get(cuenta.Unidad_Ejecutora__c).Total_Captacion_Oficina__c += cuenta.Suma_Total_Captaci_n__c;
			}
		}
		Database.update(oficinaMap.values());
    }

    global void finish(Database.BatchableContext BC) {

    }
}

And my @Test:
@isTest(SeeAllData=true)
private class TestctrlTotalOficina {
    Private static testMethod void insertAccountTest() {
        Test.startTest();
		//To Insert Account
		 
        Account oCuenta = new Account();
        oCuenta.Name = 'Prueba Nombre';
        oCuenta.Unidad_Ejecutora__c = '205';
		try {
			insert oCuenta;
		} catch(Exception e) {
			e.getCause();
		}
		Test.stopTest();
    }

	Private static testMethod void updateAccountTest() {
        // To Update Account
		///////////////////////////////////////////
        List<Account> acctList = [SELECT Name, Unidad_Ejecutora__c FROM Account];
			//for(Account acct :acctList){
   			 //acct.Name = 'dasdds';
			//}
		update acctList;
///////////////////////////////////////////
        
        Account oCuenta = new Account();
		//oCuenta.Id = '0014P000029eezDQAQ';
        oCuenta.Name = 'Prueba Nombre';
        oCuenta.Unidad_Ejecutora__c = '205';
		insert oCuenta;
		oCuenta.Name = 'Otra Prueba';
      //  oCuenta.Suma_Total_Captaci_n__c = 0;
		Test.startTest();
        try {
			update oCuenta;
		} catch (Exception e) {
			e.getCause();
		}
		Test.stopTest();
    }
    	Private static testMethod void deleteAccountTest() {
        // To Delete Account
		Account oCuenta = new Account();
        oCuenta.Name = 'Prueba Nombre';
        oCuenta.Unidad_Ejecutora__c = '205';
		insert oCuenta;
        oCuenta = [SELECT Id, Name, Unidad_Ejecutora__c FROM Account WHERE Id =:oCuenta.Id];
		Test.startTest();
        try {
			Delete oCuenta;
		} catch (Exception e) {
			e.getCause();
		}
		Test.stopTest();
    }

	Private static testMethod void undeleteAccountTest() {
		// To UnDelete Account
        Account oCuenta = new Account(Name = 'Prueba', Unidad_Ejecutora__c = '205');
		insert oCuenta;
		delete oCuenta;
		Account[] aCuenta = [SELECT Id, Name, Unidad_Ejecutora__c FROM Account WHERE Name = 'Prueba' ALL ROWS];
		try {
			undelete aCuenta;
		} catch (Exception e) {
			e.getCause();
		}
    }
}