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
siva sssiva ss 

how to learn salesforce

iam new to salesforce how to write the test class bleow  code explain any one 


trigger Conference on User__c (after insert,after Update) 
{
    //Checking the Data Inserting || updating
    if(Trigger.isInsert || Trigger.isUpdate)
    {
        // fetching newly added User__C from Conference__c 
        list<Conference__c> conference = [SELECT Id,ConferenceName__c,Location__c,StartDate__c,Detail__c,EndDate__c,(select id,Name__c, Email__c,Relationship__c from Users__r where id in:Trigger.newMap.keySet())
                                          FROM Conference__c where id in (select Relationship__c From User__c where Id =:Trigger.newMap.keySet())];
        // Checking the Condition either value is their or Not
        if(!conference.IsEmpty())
        {
            // Conference Loop For (Parent-Object) 
            for(Conference__c con : conference)
            {
                //User Loop For (Child-Object) 
                for(User__c use : con.Users__r)
                {
                    //Checking the Condition Whether Relationship is Empty or Value
                    if(use.Relationship__c != null)
                    {
                        con.Id = use.Relationship__c;
                        
                        // Step 1: Create a new Email
                        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                        // Step 2: Set list of people who should get the email
                        List<String> sendTo = new List<String>();
                        sendTo.add(use.Email__c);  
                        mail.setToAddresses(sendTo);
                        
                        // Step 3: Set who the email is sent from
                        mail.setReplyTo(use.Email__c);
                        mail.setSenderDisplayName('Export Solutions and Techonologies');
                        
                        // Step 4. Set email contents - you can use variables!
                        mail.setSubject('URGENT BUSINESS COnfernce ');
                        String body = 'Dear ' +use.Name__c  + ',<br/> ';
                        body += 'Conference Name:' +con.ConferenceName__c+',<br/>';
                        body += 'Location:' +con.Location__c+',<br/>';
                        body += 'StartDate:' +con.StartDate__c+',<br/>';
                        body += 'StartDate:' +con.EndDate__c+',<br/>';
                        body += 'Detail:' +con.Detail__c+',<br/>';
                        body += 'Your Added This Metting .<br/>';
                        mail.setHtmlBody(body);
                        
                        // Step 6: Send all emails in the master list
                        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
                    }
                }
            }
        } 
    }
}
Deepali KulshresthaDeepali Kulshrestha
Hi Siva,

I've gone through your apex class, and you must note some important points before writing testClass:-->

Important-->
{
1.You should always write your code of trigger in apexHandlerClass so it would be protable for you to test the trigger and apexClass.
 Example-->
 
 trigger Conference on User__c (after insert,after Update) 
 {
  if(Trigger.IsAfter)
  {
    if(Trigger.IsInsert || Trigger.IsUpdate)
    {
       yourApexClass.yourApexMethod(Trigger.New);
    }
  }
 }//trigger
 
 
 //apex class
 public class yourApexClass
 {
 public static void yourApexMethod(List<User__c> allUser)
 {
 
 }
 }
 
}


Below is link of the best practice for writing testclass:

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_best_practices.htm

You should follow below steps to write testclass of your code:

1.Create a testDataFactory Class(i.e. apexclass only)-> where it will have all the created data.
2.Create Conference record in testdatafactory in bulk 
3.Then Create the User record as per your condition defined in query.
4.Then Create testclass for your class:
 example-->
 
 @isTest public class yourTestClass
 {
 
 @isTest static void checkTestClass()
 {
    //call the testdatafactory method where data is creating
    
    Test.startTest();
    //your System.assertEquals(expectedResult,yourResult);
    Test.stopTest();
 }
 }
 
 
 Note:--> You can query yourResult from the data you created and the expectedResult you should return from you apex class
 
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com