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
Benjamin Zerbib 12Benjamin Zerbib 12 

My trigger doesn't work

Hi guys!

I created a trigger and a test class that successfully passed the tests, but when I try to run a test myself, nothing is happening...

Here is my trigger:
public class UserProfilePhotoPublicLink {
    public static void updateUserCustomURL(List<User> users) {
        for (User user : users) {
            if (user.SmallPhotoUrl != null && user.Custom_Photo_URL__c == null) {
                user.Custom_Photo_URL__c = user.SmallPhotoUrl.replace('caesarstone', 'BENJI');
            }
        }
        update users;
    }
}

And here is the test class:
@isTest
private class UserProfilePhotoPublicLinkTest {
    static User testUser;

    static testMethod void setUp() {
        // Create a test user
        testUser = new User(
            FirstName = 'Test',
            LastName = 'User',
            Email = 'testuser@example.com'
        );
        insert testUser;

        // Update the test user's SmallPhotoUrl
        //testUser.SmallPhotoUrl = 'https://example.com/s96-c/photo.jpg';
       // update testUser;
    }

    static testMethod void testUpdateUserCustomURL() {
        // Call the method being tested
        UserProfilePhotoPublicLink.updateUserCustomURL(new List<User> { testUser });

        // Verify that the Custom_Photo_URL__c field has been updated
        testUser = [SELECT Custom_Photo_URL__c FROM User WHERE Id = :testUser.Id];
        System.assertEquals(testUser.Custom_Photo_URL__c, 'https://example.com/photo.jpg');
    }
}

Somehow when I upload a new picture to a user (and then the SmallPhotoUrl is getting automatically populated), nothing is happening ​​in the Custom_Photo_URL__c.

I don't understand why because it should trigger the apex class.

Could you please help me?

Thanks a lot!
Benji
Amidou CisseAmidou Cisse
It seems like the test class does not reflect the actual scenario that you are trying to test. In the test class, you are updating the SmallPhotoUrl of the test user, but the actual scenario is that this field is automatically populated after uploading a photo. Also, in the test class, you are updating the SmallPhotoUrl to a value that does not match the target string in the trigger, 'caesarstone'.

Try changing the test class as follows:

typescript
Copy code
@isTest
private class UserProfilePhotoPublicLinkTest {
    static User testUser;

    static testMethod void setUp() {
        // Create a test user
        testUser = new User(
            FirstName = 'Test',
            LastName = 'User',
            Email = 'testuser@example.com'
        );
        insert testUser;

        // Update the test user's SmallPhotoUrl
        testUser.SmallPhotoUrl = 'https://caesarstone.com/s96-c/photo.jpg';
        update testUser;
    }

    static testMethod void testUpdateUserCustomURL() {
        // Call the method being tested
        UserProfilePhotoPublicLink.updateUserCustomURL(new List<User> { testUser });

        // Verify that the Custom_Photo_URL__c field has been updated
        testUser = [SELECT Custom_Photo_URL__c FROM User WHERE Id = :testUser.Id];
        System.assertEquals(testUser.Custom_Photo_URL__c, 'https://BENJI.com/s96-c/photo.jpg');
    }
}
This should correctly test the trigger logic and should show the expected behavior.