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
Lavanya Ponniah 3Lavanya Ponniah 3 

How to create test class for the below code?

trigger CompanynameforClientprofile on Client_Profile__c (before insert, before update) 
{
  for(Client_Profile__c c:trigger.new)
    {
        Account a=[select id from Account where name=:c.Name];
        if(a.id!=null)
        {
          c.Company_name__c=a.id;
        }
        else if(a.id==null)
        {
            c.Company_name__c='001N000000LobTJ';
        }
    }
ManjunathManjunath
Hi,

1: Created a class with annotation with "@istest"
2: In your testmethod create a  record  of "Account" record with the same name what is used for the "Client_Profile__c "
3:  Next create a record "Client_Profile__c  " with all the required class.

This should do. By the way the above trigger has SOQL inside the For loop. Try to get it out of the for loop(Best practice).
This link should help you https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods (https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods)

Regards,
MCS
 
Jagan ReddyJagan Reddy
Hey Lavanya,

try this code may help you but i am not sure  :)

@isTest
public class CompanynameforClientprofile_test
{
    static testMethod void testvaliddate()
    {
        Account acc=new Account();
        acc.Name='Jagan';
        insert acc;
        
        Client_Profile__c objclient=new Client_Profile__c();
        objclient.Company_name__c=acc.Id;
        insert objclient;
        
    }
    static testMethod void testvaliddate1()
    {
        Account acc=new Account();
        acc.Name='';
        insert acc;
        
        Client_Profile__c objclient=new Client_Profile__c();
        objclient.Company_name__c='001N000000LobTJ';
        insert objclient;
        
    }
}