+ Start a Discussion
Andrei HogasAndrei Hogas 

how could i write the test class the follow 2 methods.... i have no idea?! please help me


public class OpportunityHelper {
	

public static void CreateURL_SendComingSoonEmail(){
        List<Id> oppsToQuery = new List<Id>();
        for (Tabella_consultazioni__c tc : (List<Tabella_consultazioni__c>)trigger.new){
          //invio email solo se tipologia è uguale a Sessione
			  if(tc.Tipologia__c=='Sessione' ) {
                if (tc.Opportunita__c != NULL && 
                ((Tabella_consultazioni__c)trigger.oldMap.get(tc.Id)).Opportunita__c != tc.Opportunita__c){
                    oppsToQuery.add(tc.Opportunita__c);
                }
            }
        }
        if (oppsToQuery.isEmpty()){
            return;
        }
        List<Messaging.SingleEmailMessage> emailsToSend = new List<Messaging.SingleEmailMessage>();
        List<DatiVersamento__c> dvToInsert = new List<DatiVersamento__c>();
        List<Opportunity> oppsToUpdate = new List<Opportunity>();
        Map<Id, DatiVersamento__c> insertedDVs = new Map<Id, DatiVersamento__c>();
        EmailTemplate emailTemp = [SELECT htmlValue FROM EmailTemplate WHERE name = 'Filippo_ComingSoonTemplate'][0];
        Map<Id, Opportunity> oppQuery = new Map<Id, Opportunity>(
            						[SELECT Id, amount, URL_Dati_Anticipo__c,
                                     Account.FirstName, Account.LastName, Account.PersonEmail
                                     FROM Opportunity WHERE Id IN :oppsToQuery]);
        for (Opportunity opp : oppQuery.values()){
            dvToInsert.add(new DatiVersamento__c(Opportunity__c = opp.Id,
                                                Nome_Paziente__c = opp.Account.FirstName,
                                                Cognome_Paziente__c = opp.Account.LastName));
        }
        insert dvToInsert;
        Site_domain__c sd = Site_domain__c.getInstance();
        String url = sd.URL__c+'DatiVersamento';
        for (DatiVersamento__c dv : dvToInsert){
            insertedDVs.put(dv.Opportunity__c, dv);
        }
        for (Opportunity opp : oppQuery.values()){
            opp.URL_Dati_Anticipo__c = '<a href="'+url+'?Id='+insertedDVs.get(opp.Id).Id+'">'+url+'?Id='+insertedDVs.get(opp.Id).Id+'</a>';
            oppsToUpdate.add(opp);
        }
        update oppsToUpdate;
        for (Tabella_consultazioni__c tc : (List<Tabella_consultazioni__c>)trigger.new){
            if (tc.Opportunita__c != NULL && 
            ((Tabella_consultazioni__c)trigger.oldMap.get(tc.Id)).Opportunita__c != tc.Opportunita__c){
                Opportunity opp = oppQuery.get(tc.Opportunita__c);
                Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
                String body = emailtemp.htmlValue.replace('{!Opportunity.Account}', 
                                                          opp.Account.FirstName+' '+opp.Account.LastName);
                String subject = 'Acconto Sessione bSBS '+opp.Account.FirstName+' '+opp.Account.LastName;
                if (opp.URL_Dati_Anticipo__c == NULL || opp.Account.PersonEmail == NULL){
                    tc.addError('Errore: mancano i campi Email, Url o Amount nella opportunità scelta.');
                }
                body = body.replace('<![CDATA[','');
                body = body.replace(']]>','');
                body = body.replace('{!date}', tc.Data__c.day()+'/'+tc.Data__c.month()+'/'+tc.Data__c.year());
                body = body.replace('{!time}',tc.Ora_inizio_appoggio__c+'.'+tc.Minuto_inizio_appoggio__c);
                body = body.replace('{!IBAN}',IBAN);
                body = body.replace('{!SWIFT}',SWIFT);
                body = body.replace('{!BENEFICIARY}',BENEFICIARY);
                body = body.replace('{!BANK}',BANK);
                body = body.replace('{!subject}',subject);
                body = body.replace('{!amount}','600,00');
                body = body.replace('{!expireDate}',system.Date.today().addDays(2).day()+'/'+system.Date.today().addDays(2).month()+'/'+system.Date.today().addDays(2).year());
                body = body.replace('{!URLdatidacompilare}',opp.URL_Dati_Anticipo__c);
                email.setToAddresses(new List<String>{opp.Account.PersonEmail});
                email.setSubject(subject);
                email.setHtmlBody(body);
                if (opp.Account.PersonEmail == NULL) {
                    continue;
                } else {
                	emailsToSend.add(email);
                }
            }
            
        }
        Messaging.SendEmailResult[] results = Messaging.sendEmail(emailsToSend, TRUE);
        return;
    }
    
    
    public static void PopulateSMSField(){
        List<Id> oppIDsToQuery = new List<Id>();
		for (Tabella_Consultazioni__c tcs : (List<Tabella_Consultazioni__c>)trigger.new){
            	oppIDsToQuery.add(tcs.Opportunita__c);
        }
        Map<Id, Opportunity> oppsToUpdate = new Map<Id, Opportunity>([SELECT Id, OwnerId
                                                FROM Opportunity
                                                WHERE Id IN :oppIDsToQuery]);
        Map<Id, User> users = new Map<Id, User>([SELECT Id, FirstName, LastName
                                                FROM User]);
        Configurazione_SMS__c smsPC = Configurazione_SMS__c.getValues('SMS Prenotazione Consultazione');
        Configurazione_SMS__c smsPS = Configurazione_SMS__c.getValues('SMS Prenotazione Sessione');
        for (Tabella_Consultazioni__c tcs : (List<Tabella_Consultazioni__c>)trigger.new){
            if (tcs.Tipologia__c == 'Consultazione' && tcs.opportunita__c != NULL){
                String str=smsPC.Test_SMS__c;
                str = str.Replace('{!DATA}',FormatDate(System.date.today().day())+'/'+FormatDate(System.date.today().month())+'/'+System.date.today().year());
                str = str.Replace('{!ORA}',FormatDate(system.now().hour())+':'+FormatDate(system.now().minute()));
                str = str.Replace('{!COMMNAME}',users.get(oppsToUpdate.get(tcs.Opportunita__c).OwnerId).FirstName + ' ' + users.get(oppsToUpdate.get(tcs.Opportunita__c).OwnerId).LastName);
            	oppsToUpdate.get(tcs.Opportunita__c).SMS_Prenotazione_Consultazione__c = str;
            } else if (tcs.Tipologia__c == 'Sessione' && tcs.opportunita__c != NULL){
                String str=smsPS.Test_SMS__c+' '+smsPS.Test_SMS_2__c;
                str = str.Replace('{!DATA}',FormatDate(System.date.today().day())+'/'+FormatDate(System.date.today().month())+'/'+System.date.today().year());
                str = str.Replace('{!ORA}',FormatDate(system.now().hour())+':'+FormatDate(system.now().minute()));
                str = str.Replace('{!COMMNAME}',users.get(oppsToUpdate.get(tcs.Opportunita__c).OwnerId).FirstName + ' ' + users.get(oppsToUpdate.get(tcs.Opportunita__c).OwnerId).LastName);
            	oppsToUpdate.get(tcs.Opportunita__c).SMS_Prenotazione_Sessione__c = str;
            }
        }
        update oppsToUpdate.Values();
    }
    
    public static String FormatDate(integer inte){
        if (inte<10){
            return '0'+inte;
        } else{
            return String.ValueOf(inte);
        }
    }
    
    
    public static void PopulateSMSFieldFromDate(){
        Configurazione_SMS__c cSMS = Configurazione_SMS__c.getValues('SMS Conferma Sessione');
        for (Opportunity opp :(List<Opportunity>)trigger.new){
            if( (trigger.isInsert || ( opp.data_pagamento__c != NULL && ((Opportunity)trigger.oldMap.get(opp.Id)).data_pagamento__c == NULL) ) ){
                   opp.SMS_Conferma_Sessione__c = cSMS.Test_SMS__c;
               }
        }
    }
    
}

 
EldonEldon
Hi,

Its better you go through these links to have an idea about writing test classes 

https://trailhead.salesforce.com/en/modules/apex_testing
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm
http://www.sfdc99.com/2013/05/14/how-to-write-a-test-class/


Regards
Jasper WallJasper Wall

Hi Andrei,
create a test class as given the steps in
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm
Identify the objects used in your two methods and create their records in your test methods. You can use any number of test methods as per the needs of the code coverage.​Call your two methods from the test class like this,
@isTest 
private class HelloWorldTestClass {
    static testMethod void validateHelloWorld() {
      // create test data as per the needs of your logic
       Opportunity opp=new Opportunity();
       insert opp;
      //...
     // until all your objects are instantiated
     //now call your test methods
        OpportunityHelper.CreateURL_SendComingSoonEmail();
        OpportunityHelper.PopulateSMSFieldFromDate();
     // check whether your code working fine or not with assert statements
       System.assertEquals(90, b.Price__c);
    }
}


Mark as the best answer if it helps.

Thanks,
Balayesu

 
Ajay mishraAjay mishra
Hi Andrei,

As this class indicates this created for Opportunity. So, you should create the test class considering trigger hit point.  
@isTest 
private class HelloWorldTestClass {
    static testMethod void validateHelloWorld() {
       Opportunity oppObj = new Opportunity(/*Field As per your requirement*/);
       
       // DML as per your trigger 
       // For Example for After or Before Insert. Just do as below.
       insert b;
    
       // Confirm Output of operation performed.
       System.assertEquals(90, b.Price__c);
    }
}

Please refer above code. Let me know if you want help.

Regards,
Ajay Mishra
Email: mishraajay.m1@gmail.com