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
mac adminmac admin 

Test class for the schema class

Hi all,

Can anyone help me in writting the test class for the below class.
 
trigger EdiGra on Form__c (after update) {
  Schema.DescribeSObjectResult objSchema = Form__c.sObjectType.getDescribe();
  Set<String> fields = objSchema.fields.getMap().keySet();
  
    String[] changedFields  = new String[]{};

    for(Form__c c: trigger.new){
        for(string s: fields){
            if(c.get(s) != trigger.oldMap.get(c.Id).get(s)){
                changedFields.add('GForm :' + c.Name + ' - ' + s + ' - Old Value:' + trigger.oldMap.get(c.Id).get(s) + ' New Value:' + c.get(s));
            }
        }
    }
    if(changedFields.size()>0){
      messaging.singleEmailMessage mail = new messaging.singleEmailMessage();
        mail.setToAddresses(new string[]{UserInfo.getUserEmail()});
        mail.setReplyTo(UserInfo.getUserEmail());
        mail.setSubject('Postal updated');

         
        string htmlBody = '';
        for (String s : changedFields){
            htmlBody += '<div> ' + s + ' </div>';
        }
        mail.setHtmlBody(htmlBody);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[]{mail});
  }
}

Thanks in advance.

Regarding,
mac.
Shivdeep KumarShivdeep Kumar
Hi Mac,

Use the below code, 
@istest
public class test_test {
    static testMethod void data1(){
		
		User thisUser = [Select id from User where Id =: UserInfo.getUserId()]; 
        System.RunAs(thisUser){
            Contact con = New Contact();
            con.LastName = 'Dev101';
            con.Email = 'tesrd@gmail.com';
            insert con;
            
            Case c = New Case();
            c.Status = 'New';
            c.ContactId = con.Id;
            c.SuppliedEmail = 'asxdc@gmail.com';
            insert c;
           
            c.status = 'Closed';
            update c;
            
          
        }
    }

}
Create one more method for testing the null pointer exception testing.

Please let me know if this help !

Thanks 
Shivdeep
Shivdeep KumarShivdeep Kumar
Hi Mac,
As per your scenerio, no need to insert contact and replace case sobject with your sobject i.e., "Form__c".
update any fields of form__c (In my case i updated status field) and then save.

Thanks
Shivdeep