function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Rajendra Prasad 44Rajendra Prasad 44 

can anyone help me with this test class

@isTest
public class TestDataFactory {
   
    public static testMethod void testdata_for_manipulateDataForInviteBlast(){
        insert new Pardot_Settings__c(Name = 'Pardot User Key', User_Key__c ='xxxxxxxxxxxxxxxxxxx');
        
        Test.setMock(HttpCalloutMock.class, new MockHttpResponseGeneratorpost());
        Account a = new Account();
        
        a.Name = 'Test Account';
        Insert a;
       
        Contact con1 = new Contact();
        con1.FirstName='Seema';
        con1.LastName= 'singh';
        con1.AccountId = a.Id;        
        con1.Email='seemax1.singh@accelerize360.com';
        insert con1; 
        
        Analyst__c an= new Analyst__c();
        an.Name='asdf122345';
        an.Order_of_Precedence__c = '1';
        insert an;
        
        
        Custom_Event__c cEvent = new Custom_Event__c();
        cEvent.name = 'testingEvent';
        cEvent.CAT_external_event_name__c = 'testing External Event';
        cEvent.Type_of_Meeting__c = 'Analyst Marketing';
        cEvent.Start_Date__c = DateTime.Now().AddDays(10);
        cEvent.End_Date__c = DateTime.Now().AddDays(13);
        insert cEvent;
        
        Campaign com = new Campaign();
        com.Analyst__c=an.id;
        com.Related_Event__c = cEvent.Id;
        com.Name='test';
        insert com;
        
        CampaignMember cm = new CampaignMember();
        cm.ContactId=con1.id;
        cm.CampaignId=com.id;
        cm.CampaignId = com.id;
        insert cm;
        
         EmailMessage incomingMail = new EmailMessage();
         incomingMail.fromaddress='test@email.com';
         incomingMail.toAddress = 'test@test.com';
         incomingMail.subject = ' Test Message';
         incomingMail.TextBody= 'This is the message body ';
         incomingMail.RelatedToId = cEvent.id;
         insert incomingMail ;       
        
    }
}

here I'm not testing integration directly, but it was like whenever contact is created it happens, so its making callout indirectly that's why I have to use more class. 

here when I run the test I'm getting error:

System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out

and when I remove emailmessage record insertion part its working. but I need that
m 10m 10
Hi Rajendhra could you please share apex class
Rajendra Prasad 44Rajendra Prasad 44
this is the test class from which i m calling above test class: 
@isTest
private class ManipulateDataToSendEmailTest {
     private static testMethod void testing_manipulateDataForInviteBlast(){
      
        TestDataFactory.testdata_for_manipulateDataForInviteBlast();
         
        CampaignMember cm = [select id,name from CampaignMember limit 1];
         system.debug('cm'+cm);
        Custom_Event__c ce = [select id from Custom_Event__c limit 1]; 
          
        emailmessage em = [select id, ToAddress, Subject, htmlbody from emailmessage where relatedtoid=:ce.id order by createddate desc limit 1];
        system.debug('emessage'+em);
        GetEmailInfoWrapper getEmailInfo = new GetEmailInfoWrapper(ce.id);
        system.debug('getEmailInfo'+getEmailInfo);
        ManipulateDataToSendEmail InviteBlast=new ManipulateDataToSendEmail();
            InviteBlast.manipulateDataForInviteBlast(getEmailInfo, em);
             system.debug('eeeee'+Limits.getEmailInvocations());
       
    }
}


GetEmailInfoWrapper :
/**
 * @File Name          : GetEmailInfoWrapper.cls
 * @Description        : Called from SendEmailEventSchedulable class 
 * @Author             : Accelerize360
**/
public without sharing class GetEmailInfoWrapper {
    
    Public string assistant {get;set;} 
    Public string analystEmail {get;set;}
    Public Map<string,CampaignMember> campaignMemberMap {get;set;}
    Public Map < string, Custom_Event__c > eventDetailsMap {get;set;}
    Public Map<string,string> contactIdAndEmailMap {get;set;}
    Public Map<string,string> contactListMemberMap {get;set;} // map of email, id
    Public Map<string,string> contactListMemberAssistantMap {get;set;} // map of email, id
    Public Map<string,string> contactListMemberEmailAnalystMap {get;set;} // map of contact list member email, capaign id i.e. contact list id
    Public Id campaignId {get;set;} // map of contact list member email, capaign id i.e. contact list id
    /**
     * @Description class constructor 
     * @param event id
     */
    public GetEmailInfoWrapper(Id eventId) {
        if (eventId != null) {
            getcontactListMembersByEvent(eventId);
        }
    }
    /**
     * get contact list members based on event id
     * @param eventId
     * @return contact list members
     */
    @TestVisible
    private void getcontactListMembersByEvent(Id eventId) {
        Map < string, campaignmember > campaignMemberMapx = new Map < string, campaignmember > ();
        Map < string, Custom_Event__c > eventDetailsMapx = new Map < string, Custom_Event__c > ();
        Map < string, string > contactIdMapx = new Map < string, string > ();
        Map < string, string > contactMemberMap = new Map < string, string > ();
        Map < string, string > campaignAnalystMap = new Map < string, string > ();
        // get contact list member in sub query 
        LIST < Custom_Event__c > contactListMembers = [SELECT Id,name,CAT_external_event_name__c,Type_of_Meeting__c,End_Date_Local_Time_Formula__c,Start_Date_Local_Time_Formula__c,(select Id, Email, contactid, Name, Contact_target_send_email__c,CampaignId, status,Related_Interaction_Record__c from Contact_List_Members__r 
                                                        where Status = 'Subscribed' or Status ='Invited') from Custom_Event__c where id =: eventId ];
       

        if (contactListMembers.size() > 0) {
            for (Custom_Event__c c: contactListMembers) { 
                //Loop through child records
                for (campaignmember con: c.Contact_List_Members__r) {
                    if (con.email != null) {
                        campaignMemberMapx.put(con.id, con);
                        eventDetailsMapx.put(con.id, c);
                        contactMemberMap.put(con.Contact_target_send_email__c, con.id);
                        contactIdMapx.put(con.ContactId, con.Contact_target_send_email__c);
                        campaignAnalystMap.put(con.Contact_target_send_email__c, con.campaignId);
                    }
                } // end inner loop 
            }
            this.campaignMemberMap = campaignMemberMapx;
            this.eventDetailsMap = eventDetailsMapx;
            this.contactListMemberMap = contactMemberMap;
            this.contactIdAndEmailMap = contactIdMapx;
            this.contactListMemberEmailAnalystMap = campaignAnalystMap;
            getcontactListMembersAssistant();
            getcontactListAnalyst(campaignAnalystMap);
        }
    }
    /**
     * get getcontactListMembersAssistant
     */
    @TestVisible
    private void getcontactListMembersAssistant() {

        Map < string, string > contactIdMap = new Map < string, string > ();
        List < Contact > contactAssistant = [SELECT id, Assistant__r.Email from contact where id in: this.contactIdAndEmailMap.keySet()];
        for (Contact caEmail: contactAssistant) {
            if (this.contactIdAndEmailMap.containsKey(caEmail.id) && caEmail.Assistant__r.Email != null) {

                contactIdMap.put(this.contactIdAndEmailMap.get(caEmail.id), caEmail.Assistant__r.Email);
            }

            this.contactIdAndEmailMap.remove(caEmail.id);
        }
        this.contactListMemberAssistantMap = contactIdMap;
    }

    /**
     * get analyst from Campaign(contact list) object field = Analyst__c lookup to employee obj = Analyst__c
     * @param Map < string, string >
     */
    @TestVisible
    private void getcontactListAnalyst(Map < string, string > campaignAnalystMap) {

        List < Campaign > getAnalyst = [select Analyst__r.Email__c from Campaign where id in: campaignAnalystMap.values()];
        if (getAnalyst.size() > 0) {
            this.analystEmail = getAnalyst[0].Analyst__r.Email__c;
        }

    }
}

ManipulateDataToSendEmail :
@isTest
private class ManipulateDataToSendEmailTest {
     private static testMethod void testing_manipulateDataForInviteBlast(){
      
        TestDataFactory.testdata_for_manipulateDataForInviteBlast();
         
        CampaignMember cm = [select id,name from CampaignMember limit 1];
         system.debug('cm'+cm);
        Custom_Event__c ce = [select id from Custom_Event__c limit 1]; 
          
        emailmessage em = [select id, ToAddress, Subject, htmlbody from emailmessage where relatedtoid=:ce.id order by createddate desc limit 1];
        system.debug('emessage'+em);
        GetEmailInfoWrapper getEmailInfo = new GetEmailInfoWrapper(ce.id);
        system.debug('getEmailInfo'+getEmailInfo);
        ManipulateDataToSendEmail InviteBlast=new ManipulateDataToSendEmail();
        try{
            InviteBlast.manipulateDataForInviteBlast(getEmailInfo, em);
             system.debug('eeeee'+Limits.getEmailInvocations());
        } catch (exception e) {
            system.debug('error'+e.getMessage());
        }
    }
}

 
Ashaar Gautam 15Ashaar Gautam 15
Hi

Move this Test.setMock(HttpCalloutMock.class, new MockHttpResponseGeneratorpost()); to after the last DML statement and then run it. It should work as you never call a callout before DML you neeed to call out after the last DML.

Let me know the results please.

Thanks