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
Alaia OribelloAlaia Oribello 

Compile Error: Expecting '}' but was: 'trigger' at line 4 column 1

Hi All,

Im getting an error like Expecting '}' but was: 'trigger'. Can you help me with this one? Thanks!

@isTest
public class TestRestrictContactByName {

trigger RestrictContactByName on Contact (before insert) {
//check contacts prior to insert or update for invalid data
    For (Contact c : Trigger.New) {
        if(c.LastName == 'INVALIDNAME') {   //invalidname is invalid
            c.AddError('The Last Name "'+c.LastName+'" is not allowed for DML');
        }

    }

}
}
Best Answer chosen by Alaia Oribello
Nayana KNayana K
trigger inside test class!! It is wrong. What are you trying to do? Seems trigger is already present and you want test code to be written to cover that...

If yes,
@isTest
public class TestRestrictContactByName 
{
	@isTest
	public static void testInvalid()
	{
		try
		{
			Contact objContact = new Contact(LastName = 'INVALIDNAME');
			insert objContact;
		}
		catch(Exception e)
		{
			
		}
		
		system.assertEquals(0, [SELECT COUNT() FROM Contact]);
	}
}