0

I am new to Salesforce and need to write an Apex test class for this Trigger. I tried this code, but it seems not correct.

@isTest
public class FormBeforeDeleteTest {

     public static testmethod void FormBeforeDeleteTest()
    {
        Profile pr = new Profile(profileName =='France');
        Database.SaveResult result = Database.insert(pr, false);
        System.assertEquals('contact your admin',result.getErrors()[0].getMessage());
    }

}
    }

}

==> The triggers :

trigger BeforeDelete on Form__c (before delete) {
    
    Id currentUser = UserInfo.getUserId();
    
    Id currentUserProfileId = userinfo.getProfileId();
    
    String profileName = [SELECT Id, Name FROM Profile WHERE Id = :currentUserProfileId].Name;
        
    if (
            profileName =='France' ||
            profileName =='Egypt'){
     
     for (Form__c form : trigger.old )
        
    {
        if(form.CreatedById != currentUser)
        {
            form.addError('contact your admin');
        } 
        
        if(form.CreatedById == currentUser)
        {
            form.addError('contact your admin');
        }
    }
    }
    }

trigger BeforeUpdate on Form__c (before update) {

     Id currentUser = UserInfo.getUserId();
    
     Id currentUserProfileId = userinfo.getProfileId();
    
    String profileName = [SELECT Id, Name FROM Profile WHERE Id = :currentUserProfileId].Name;
    
        for (Form__c form : trigger.new )
        {
          if(form.Top_Service__c == false){
             form.Account__c = form.TestAccount__c ;
           }
            
         if (
            profileName ==' France ' ||
            profileName =='Egypt' ||
           )
         {
        
         String etats= trigger.oldMap.get(form.id).Etat__c;
           
        if(form.CreatedById != currentUser)       
        {
            form.addError('contact your admin');
        } 
        
        if(form.CreatedById == currentUser && etats== 'end')
        {
            form.addError('contact your admin');
        }       
    }
        }
    }
2
  • your trigger are using 'before update' and 'before delete', and your test class is executing an insert operation. That means they will never hit the order sequence as required. If you want to run an update try updating the record on the test class instead. I'd recommend you check the examples on trailhead check here
    – Carlos
    Feb 23, 2022 at 11:04
  • @Carlos , yes i make an error, i modified my code , can you please check it now Feb 23, 2022 at 11:08

1 Answer 1

1

Currently in your test class you are creating a Profile object only. You also need to create and edit, delete the object that you are testing: Form__c

e.g.

Form__c f = new Form__c();
f.Name = 'testname';
f. = 'xxx';  //here you populate some Form__c mandatory fields
f. = 'yyy';  //here you populate some Form__c mandatory fields
insert f;

//to begin testing Update, you need to update the fields of interest and update statement e.g.

f.Name = 'newTestName';
update f;

//to delete the record, just add the delete statement to your test:

delete f;

Frankly speaking this will give you some test coverage, but you need to read more about how to test different outcomes. For example in your scenario, you would need to create a user and a profile like 'France', then assign that profile to the user, and then assign that user as the owner of the record / create the Form__c as that new user. then you run your update and delete:

@isTest
private class TestRunAs {
   public static testMethod void myTest() {
        // Setup test data
        // Create a unique UserName
        String uniqueUserName = 'standarduser' + DateTime.now().getTime() + '@testorg.com';
        // This code runs as the system user
        Profile p = [SELECT Id FROM Profile WHERE Name='Egypt'];
        User u = new User(Alias = 'standt', Email='[email protected]',
        EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
        LocaleSidKey='en_US', ProfileId = p.Id,
        TimeZoneSidKey='America/Los_Angeles',
         UserName=uniqueUserName);

        System.runAs(u) {

         // PASTE HERE THE CODE SNIPPETS FROM ABOVE [...Form__c f = new Form__c... ] *****

        }
    }
}
3

You must log in to answer this question.

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