1

I'm starting with code and test classes. I have a class that reject an approval process for an account. I start this class by a process builder.

This is the code:

public class ALI_RechazoPAprobacion_Expediente {
    @InvocableMethod
    public static void RechazarExpediente(List<Id> ExpId){
        try{        
            list<Account> AccList = [SELECT id FROM account WHERE id in:ExpId];
            Approval.ProcessWorkitemRequest[] prWkItems = New Approval.ProcessWorkItemRequest[]{};

            //Comprueba que el expediente está bloqueado
            //if (Approval.isLocked(AccList[0].Id) == true){

                //Reject the record
                ProcessInstance[] pi = [Select ID, Status, TargetObject.Name, (SELECT Id, ActorId, ProcessInstanceId FROM Workitems),
                    (SELECT Id, StepStatus, Comments FROM Steps) From ProcessInstance 
                    Where TargetObjectID IN :AccList AND Status = 'Pending'];

                for(ProcessInstance instance : pi){

                    for(ProcessInstanceWorkItem workItem : instance.WorkItems){

                        Approval.ProcessWorkitemRequest prWkItem = new Approval.ProcessWorkitemRequest();                   
                        prWkItem.setWorkItemID(workItem.id);
                        prWkItem.setComments('Proceso de aprobación cancelado por superar el límite de tiempo sin contestación');
                        prWkItem.setAction('Reject');
                        prWkItems.add(prWkItem);                     
                    }
                }  

                if(!prWkItems.isEmpty()){
                    Approval.ProcessResult[] appResult = Approval.process(prWkItems);
                }
            //}    
        }
        catch(Exception ex){
            System.debug('ERROR EXCEPTION' + ex);           
        }
    }
}

The class works perfectly, but I'm having some troubles to make its test class.

This is the code

/*@isTest static void ExpedienteCompleto(){
        Profile p = [SELECT Id FROM Profile WHERE Name='Territorial']; 

        //Crea el usuario y lo inserta
        User user1 = new User(Alias = 'standt', Email='standarduser@testorg.com', 
                              EmailEncodingKey='UTF-8', LastName='Testing', 
                              LanguageLocaleKey='en_US', 
                              LocaleSidKey='en_US', ProfileId = p.Id, 
                              TimeZoneSidKey='America/Los_Angeles', 
                              UserName='standarduser@aliseda.com');
        insert user1;
        if(user1 != null){
            System.debug('Usuario insertado: '+ user1.LastName);
        }
        //Crea la cuenta y la inserta
        Account acc = new Account(Name='Test1 PA', 
                                  Director_Territorial__c=user1.Id);
        Insert acc;
        if(acc != null){
            System.debug('Cuenta insertada: '+ acc.Name);
        }

        //Añade el Id de la cuenta a una lista de Ids que se pasan al método
        List<Id> ListAcc = new List<Id>();
        ListAcc.add(acc.Id);

        // Create an approval request for the account
        Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
        req1.setComments('Submitting request for approval.');
        req1.setObjectId(acc.id);
        req1.setNextApproverIds(ListAcc);

        // Submit on behalf of a specific submitter
        req1.setSubmitterId(user1.Id); 

        // Submit the record to specific process and skip the criteria evaluation
        req1.setProcessDefinitionNameOrId('Asignacion_Ingenieria');
        req1.setSkipEntryCriteria(true);    

        ALI_RechazoPAprobacion_Expediente.RechazarExpediente(listAcc);
    }

I'm able to cover the 50% of the class:

enter image description here

My test code can't access to the processInstanceWorkItem. I'm trying with some examples that I found here but I am failing.

Can anyone give me a clue or something? because i'm really stuck.

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.