Skip to main content

#Code0 discussing

While running test class.. I am continuously getting this error

Invalid type: Schema.SingleEmailMessage

#Code:-

@isTest

public class TestContactEmailTrigger {

    @isTest

    public static void testTrigger() {

        // Create test data

        List<Contact> contacts = new List<Contact>();

        for (Integer i = 0; i < 5; i++) {

            Contact contact = new Contact(

                LastName = 'TestContact' + i,

                Email = 'test' + i + '@example.com',

                Message_sent__c = true

            );

            contacts.add(contact);

        }

        insert contacts;

 

        // Perform DML operation to trigger the email sending

        Test.startTest();

        for (Contact contact : contacts) {

            contact.Message_sent__c = true;

        }

        update contacts;

        Test.stopTest();

 

        // Verify the emails are sent correctly

        List<Messaging.SingleEmailMessage> sentEmails = [SELECT Id, ToAddress, Subject, PlainTextBody FROM Messaging.SingleEmailMessage];

        System.assertEquals(5, sentEmails.size());

 

        // Check the content of the email

        for (Integer i = 0; i < 5; i++) {

            Messaging.SingleEmailMessage email = sentEmails[i];

            Contact contact = contacts[i];

            System.assertEquals(contact.Email, email.getToAddresses()[0]);

            System.assertEquals('Important Update', email.getSubject());

            System.assertEquals('Hello, \n\nThis is an important update regarding your contact information.', email.getPlainTextBody());

        }

    }

} #Salesforce Developer

1 comment
  1. Oct 10, 2024, 4:47 PM

    Hello, i am currently having the same issue, i keep receiving the error : Invalid type: Schema.SingleEmailMessage . have you solved you problem? or is it that we can't access the sent email messages in test class as they are not stored within salesforce?

0/9000

Resources from the September Edition of Salesforce Developers Ask Me Anything

 

I hope you joined us for the fantastic SFDevsAMA on September 27th featuring Karishma Lalwani, Stephanie Maddox, and Alice Oh. They loved answering all your questions about the great new developer announcements from Dreamforce 2023, including Code Builder, Lightning Web Components, and Scale Center. If you missed it, don't worry - we recorded the series and our experts have provided some great resources that our experts for you to continue your learning! 

Still have questions? Post them in this thread for our experts!

 

#Code Builder #Developer Tools #LWC #Lightning Web Components #Ask An Expert #SFDevsAMA

Show Less

0/9000

📣 Help shape Code Builder GA 📣

 

Code Builder is a browser-based tool where you can build SOQL Queries with clicks, browse metadata, check out a visual flame graph of an Apex log, and much more! Code Builder is currently in open beta, available for anyone to try.

 

Whether you prefer clicks or code, we want to make it super easy to get started with Code Builder. To do that, we need your input. The focus group will be run remotely and asynchronously, and you can participate on your own time from a computer, smartphone or tablet. As part of the study you will be asked to navigate 1-3 prototypes and provide feedback based on questions we'll provide. It will take about 10 minutes.

➡️ If you’re interested in participating in this research, please sign up here.

 

Haven't tried Code Builder yet? Check out the community group for all the resources you need to dive in #Code Builder

0/9000

Hi Guys,

 

I have huge amount of csv files under different projects folder. What I want to do is to set up a project filter first, and then based on this project Python code will scan all the files under this folder. Then I can use this return file lists as another filter. After that, python will open specific csv files based on these two steps filter. The reason to do that is that I don't want to load all the data into tableau one time, it's huge amount of data as I mentioned.

 

I can easily write code in the Python Jupyter Notebook. But it's not easy to complete this in Tableau for me. Hopefully, I can get the idea from you guys. Thank you so much.

 

My code will be like this:

#Code to read all the files under a filtered project

⌗This code is okay in Jupyter notebook, but has error in the tableau, I cannot figure it out why it has error

script_str(

"

import os

Proj = _arg1 # Project filter

Folder = r"C:\Samples\" + Proj

csvfiles = [f for f in oslist(Folder) if f.endswith(".csv")

datefile = [f[:8] for f in csvfiles]

return datefiles

",

attr([Project]))

 

#Code to read a specific file based on csvfiles filter

#The reason to return a value list, because I don't know how to return the whole dataset

#If we can return a dataframe directly, it will great. Otherwise, I need to repeat this step many many time to get the value I want

script_str(

"

import pandas as pd

import os

Proj = _arg1 # Project filter

DT = _arg2 # The csv file filter

Folder = r"C:\Samples\" + Proj

csvfiles = [f for f in oslist(Folder) if f.endswith(".csv")

for item in csvfiles:

     if DT in item:

          df = pd.read_csv(Folder + "//" + item)

return df["Value"].tolist()

",

attr([Project]), attr([Date]))

0/9000