i wan't to make a unit test to cover my method, but he doesn't work and i don't know if i'm on a good way. I have this error : List has no rows for assignment to SObject

And this is my method code :

    public static void PB17_EventAddTypeRDV (List <Event> e, String operation) {

    for (Event evt : e){
        if( operation == 'Before')
            if (evt.Type != null && evt.Type != evt.Type_RDV__c) {
                evt.Type_RDV__c = evt.Type;

And my test method :

static void testEventAddTypeRDV(){
    Event evt = [SELECT Id, Type FROM Event LIMIT 1];
    List <Event> addEvt = new List<Event>();
    evt.Type = 'Rdv Physique';
    evt.Type_RDV__c = 'Rdv';
    update addEvt;

1 Answer 1


In the test class your org data will be not be visible. So the query below is not giving you the data.

Event evt = [SELECT Id, Type FROM Event LIMIT 1];

It is always best practice to create the test data(event) in test class as below.

        Account acc= new Account ();
        a.name='Test Account';
        insert a;
        Event e = new Event();
        e.Type = 'Rdv Physique';
        insert e;

We can also use SeeAllData=true. But it is not advisable to use it . Please find the article for the same

  • Thx, i know what i do now !
    – user120125
    Commented Sep 30, 2022 at 12:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .