0

I am having difficulty writing tests for the apex class. This is my first time working with apex classes and I don't understand how to send to @InvocableVariable. I've read answers to similar questions, but I still can't solve this problem. Thank you for your help! Apex class:

public class NWT_Calc_Charge_Ressource {

// Engine
@InvocableMethod
public static List <Results> execute (List<Requests> requestList) 
{

    List<NWT_NOWRH_JOUR__c> inputJourCollection = requestList[0].inputJourCollection;       
    for(NWT_NOWRH_JOUR__c OneDay : inputJourCollection)
    {

        NWT_NOWRH_JOUR__c[] CurrentJour = [SELECT User_ID__c, Collaborateur__c FROM NWT_NOWRH_JOUR__c WHERE Id =: OneDay.Id];

        List<String> caseNouveau = new List<String>();
        Integer nmbrPoints=0;
        integer a=0;
        List<Case> cases = [SELECT Points_de_traitement__c FROM Case WHERE OwnerId =: CurrentJour[0].User_ID__c];
        for(Case caseN : cases)
        {
            a=integer.ValueOf(caseN.Points_de_traitement__c);          
            nmbrPoints=nmbrPoints+a;
            system.Debug(nmbrPoints);
        }
        Integer nmbrCaseNouveau = [SELECT count() FROM Case WHERE (Status ='Nouveau' OR Status='En cours') AND OwnerId =: CurrentJour[0].User_ID__c];
        Integer nmbrCaseNiveau5 = [SELECT count() FROM Case WHERE (Status ='Nouveau' OR Status='En cours') AND Priority ='5 - Critique (Production)' AND OwnerId =: CurrentJour[0].User_ID__c];
        Integer nmbrCaseNiveau4 = [SELECT count() FROM Case WHERE (Status ='Nouveau' OR Status='En cours') AND Priority ='4 - Critique' AND OwnerId =: CurrentJour[0].User_ID__c];
        Integer nmbrCaseSlaOut = [SELECT count() FROM Case WHERE (Status ='Nouveau' OR Status='En cours') AND (Priority ='3 - Urgente' OR Priority ='2 - Normale' OR Priority ='1 - Basse') AND SLA_Resolution_Out__c = true AND OwnerId =: CurrentJour[0].User_ID__c];
        Integer nmbrCaseTotal = [SELECT count() FROM Case WHERE OwnerId =: CurrentJour[0].User_ID__c];

        NWT_NOWRH_RESSOURCE__c myRessource = [SELECT Nouveau_en_cours_Tickets__c, Points_de_traitements_Tickets__c, SLA_Out_Tickets__c, Total_Tickets__c, X4_Critiques_Tickets__c, X5_Critiques_Prod_Tickets__c FROM NWT_NOWRH_RESSOURCE__c WHERE Collaborateur__c =: CurrentJour[0].User_ID__c];

        myRessource.Points_de_traitements_Tickets__c = nmbrPoints; 
        myRessource.Nouveau_en_cours_Tickets__c = nmbrCaseNouveau;
        myRessource.X5_Critiques_Prod_Tickets__c = nmbrCaseNiveau5;
        myRessource.X4_Critiques_Tickets__c = nmbrCaseNiveau4;
        myRessource.SLA_Out_Tickets__c = nmbrCaseSlaOut;
        myRessource.Total_Tickets__c = nmbrCaseTotal;

        update myRessource;

    }
    SObject outputMember = inputJourCollection[0];
    Results response = new Results();
    response.outputMember = outputMember;
    List<Results> responseWrapper= new List<Results>();
    responseWrapper.add(response);
    return responseWrapper;         
}

//Input all flow vars
public class Requests 
{
  @InvocableVariable(required=true)
  public List<SObject> inputJourCollection;
}    
//output all flow vars
public class Results 
{     
  @InvocableVariable
  public SObject outputMember;
}
}

Apex test :

@isTest
class NWT_Calc_Charge_RessourceTest 
{
        @isTest static void setup() 
        {
            UserRole r = new UserRole(DeveloperName = 'yon', Name = 'yon');
            insert r;
            User u = new User(
                ProfileId = [SELECT Id FROM Profile WHERE Name = 'System Administrator'].Id,
                LastName = 'last',
                Email = 'puser001@amamama.com',
                Username = 'puser000@amamama.com' + System.currentTimeMillis(),
                CompanyName = 'TEST',
                Title = 'title',
                Alias = 'alias',
                TimeZoneSidKey = 'America/Los_Angeles',
                EmailEncodingKey = 'UTF-8',
                LanguageLocaleKey = 'en_US',
                LocaleSidKey = 'en_US',
                UserRoleId = r.Id
            );
            insert u;
            User u2 = new User(
                ProfileId = [SELECT Id FROM Profile WHERE Name = 'System Administrator'].Id,
                LastName = 'last',
                Email = 'puser002@amamama.com',
                Username = 'puser000@amamama.com' + System.currentTimeMillis(),
                CompanyName = 'TEST',
                Title = 'title',
                Alias = 'alias',
                TimeZoneSidKey = 'America/Los_Angeles',
                EmailEncodingKey = 'UTF-8',
                LanguageLocaleKey = 'en_US',
                LocaleSidKey = 'en_US',
                UserRoleId = r.Id
            );
            insert u2;
            System.runAs(u){
                Account acc = new Account();
                acc.Name = 'Test Account';
                acc.ShippingLatitude = 75.46;
                acc.ShippingLongitude = 45.46;
                //acc.AccountStatus__c = 'test';
                insert acc;
            }
            System.runAs(u){
                Contact con = new Contact();
                con.LastName = 'Contact1';
                con.AccountId = [SELECT Id FROM Account WHERE Name = 'Test Account'].Id;
                insert con;
            }
            System.runAs(u){
                Case t1 = new Case();
                t1.Subject = 'case1';
                t1.Status = 'En cours';
                t1.Origin = 'Web';
                t1.Priority ='1 - Basse';
                t1.Description = 'descr';
                t1.AccountId = [SELECT Id FROM Account WHERE Name = 'Test Account'].Id;
                t1.ContactId = [SELECT Id FROM Contact WHERE Name = 'Contact1'].Id;
                t1.Nature__c = 'Incident';
                t1.Type__c = 'Messagerie';
                t1.Sous_Type__c = 'Authentification';
                insert t1;
            }
            System.runAs(u){
                Case t2 = new Case();
                t2.Subject = 'case2';
                t2.Status = 'En cours';
                t2.Origin = 'Web';
                t2.Priority ='1 - Basse';
                t2.Description = 'descr';
                t2.AccountId = [SELECT Id FROM Account WHERE Name = 'Test Account'].Id;
                t2.ContactId = [SELECT Id FROM Contact WHERE Name = 'Contact1'].Id;
                t2.Nature__c = 'Incident';
                t2.Type__c = 'Messagerie';
                t2.Sous_Type__c = 'Authentification';
                insert t2;
            }
                System.runAs(u){
                NWT_NOWRH_RESSOURCE__c res1 = new NWT_NOWRH_RESSOURCE__c();
                res1.Name ='res1';
                Decimal heures = 39;
                res1.Heures_de_travail_Semaine__c = heures;  
                res1.OwnerId =  [SELECT Id FROM User WHERE email = 'puser001@amamama.com'].Id;
                insert res1;
            }
                System.runAs(u){
                NWT_NOWRH_JOUR__c nj1 = new NWT_NOWRH_JOUR__c();
                Date d = Date.today();
                nj1.Date__c = d;
                nj1.Collaborateur__c = [SELECT Id FROM NWT_NOWRH_RESSOURCE__c WHERE Name = 'res1'].Id;
                insert nj1;
            }



    }  
    @isTest static void callRequest()
    {

        Test.startTest();
        NWT_NOWRH_JOUR__c[] Jours = [SELECT User_ID__c, Collaborateur__c FROM NWT_NOWRH_JOUR__c];

        List<NWT_Calc_Charge_Ressource.Requests> Testingthis = new List<NWT_Calc_Charge_Ressource.Requests>();
        NWT_Calc_Charge_Ressource.Requests RequestTest  = new NWT_Calc_Charge_Ressource.Requests();

        RequestTest.inputJourCollection = Jours;
        Testingthis.add(RequestTest);


        //System.assertEquals(sObject.);

        Test.stopTest();
    }
}
2
  • Once you have requests list, call the method like this: List<Results> resultList = NWT_Calc_Charge_Ressource.execute(Testingthis); Jun 11, 2020 at 12:16
  • @salesforceDeveloper - this should be the answer
    – cropredy
    Jun 12, 2020 at 1:30

1 Answer 1

0

In your test class after you have a request list call your Method for its code coverage.

List<Results> resultList = NWT_Calc_Charge_Ressource.execute(Testingthis);

Assuming Testingthis is your request list which you need to pass in your method

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.