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
RabRab 

Help! Writing my first Test Class

Hello,

I'm new to Salesforce Apex coding and was wondering if there is someone that can help me write my first test class for a trigger.

Thank you.
Suraj Tripathi 47Suraj Tripathi 47
Hi
 
trigger AccounttTrigger on Account(after insert){
    HelperclassQ1.disp(trigger.new);
}

//////////////
public class HelperclassQ1 {
		public static void disp(list<account> aclist)
        {
           list<contact> con = new list<contact>();
            for(account conob: aclist)
            {   
              for(integer j=0;j<conob.NumberOfEmployees;j++)
                {
                    contact co= new contact();
                    co.lastname='kumar';
                    co.AccountId = conob.id;
                con.add(co);
                }
               }
            insert con;
            }

//////////////////////////////////////////////////////

@istest
public class HelperclassQ1Test {
@istest
		public static void testAcocunt()
        {
           account ac = new account();
            ac.name= 'Test';
            ac.NumberOfEmployees = 5;

         insert ac;
            }
}

This code will work whenever you insert an account and when you run the test class you don't need o call the main class as it will automatically run after inserting an account because we have written a trigger on the insert account.

Please mark it as Best Answer if it helps you.

Thanks & Regards
Suraj Tripathi
CharuDuttCharuDutt
Hii Rab
Try Below Refference
trigger AContact on Account (After insert,before update) {
     list<Contact> contacttocreate = new list<Contact>(); 
    list<Contact> contacttoupdate = new list<Contact>();
    if(trigger.IsAfter && Trigger.IsInsert){
        For(Account acc :trigger.new){
             Contact con = new Contact();
                con.Firstname = acc.Name;
                con.LastName =  acc.Name;
                
                con.AccountId = acc.Id ;
                contacttocreate.add(con);
        }
        if(contacttocreate.size()>0){
            insert contacttocreate;
        }
    }
    if(trigger.IsBefore && Trigger.IsUpdate){
        For(Account acc :trigger.new){
            
                   For(Contact c : [select id,FirstName,LastName,AccountId from Contact where AccountId =:acc.Id] ){
                    c.FirstName = acc.Name;
                    c.LastName =  acc.Name;
                    contacttoupdate.add(c);}
            }
    }
    if(contacttoupdate.size()>0){
            update contacttoupdate;
        }
    }



Test Class

@isTest
public class AContactTest {
@isTest
    public Static void UnitTest(){
       Account Acc= new Account();
            Acc.Name='test';
            insert Acc;
        
        Contact Con =New Contact();
        Con.FirstName = 'Test';
        Con.LastName = 'Con';
        Con.AccountId = Acc.Id;
        Insert Con;
        
        Acc.Name = 'Test Acc';
        update Acc;
       

}
}
Please Mark It As Best Answer If It Helps
Thanks You!

 
RabRab

Hi,

Thank you both for replying.  

Currently this is my Trigger:

trigger AssignedResourceTrigger on AssignedResource (after delete) {
    if(trigger.isafter && trigger.isdelete) {
        map<string,object> params = new map<string,object>();
        params.put('AssignedResources', trigger.new);
        flow.Interview.Add_Assigned_UserIds_to_the_Service_Appointment_Delete_Trigger myFlow = new flow.Interview.Add_Assigned_UserIds_to_the_Service_Appointment_Delete_Trigger(params);
        myFlow.start();
    }
}
Test Class:  I don't think this right
 
@isTest
private class AssignedResourceTestClass {
    @isTest static void validateAssignedResource() {
		// data creation
		Id serviceAgentRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Service Agent').getRecordTypeId();

		Account newPersonAccount = new Account();

		newPersonAccount.put('FirstName', 'Test');
		newPersonAccount.put('LastName', 'Agent');
		newPersonAccount.RecordTypeId = serviceAgentRecordTypeId;

		insert newPersonAccount;

		ServiceAppointment sa = new ServiceAppointment(
				ParentRecordId = newPersonAccount.id
		);

		insert sa;


		//retrieve service appointment
		Test.startTest();

		Test.stopTest();

		// assert statements

	
	}
		
}

I am trying to assign a resource to a service appointment, then delete the assigned resource without deleting the other resources from the service appointment.  Kind of lost from here. Not sure what to put.  Hope that makes sense.
Kenneth Weiss 10Kenneth Weiss 10
Thanks for the recommendations! Very useful!
Ronald SpencerRonald Spencer
Oh, I'm writing it now and also facing some problems. I have a lot of courses this year, and I can't be focused only on that and have to do other tasks. And I have to write a lot of papers, and as you know, it takes a lot of time and effort. But last time I used this service https://papersowl.com/write-my-assignment when I faced some problems and needed someone to help to write my assignment, and saved a lot of time. And finished results were amazing, and I also managed to finish other tasks faster. So I think I'll use such help more often and will practice more in writing codes.