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
LoveyLovey 

How to add a record in a object using apex class when a condition satisfies for a different object.

 

I want to add a record to my object BD which is having a single field 'Name' using apex class.

The Scenario is like this -

A object 'Employee' is having field 'Emp_name' and 'Date' and if the Date for a particular record is equal to TODAY's date then a record should be added in object 'BD' with the matching name from Employee.

I will perform the scheduling using 'Schedule Apex' for daily .

 

Actually i have to do scheduling thats why i have to use Apex Class.

 

Thanks in Advance.

Pranay

Best Answer chosen by Admin (Salesforce Developers) 
devendra dhakadevendra dhaka

{        global void execute(SchedulableContext ctx)

 

         {           Employee__c emp = [Select EmpName__c from Employee__c where Next_Birthday__c =:System.TODAY()];

                     

                     if(emp <> Null)

                   

                    {            Bd__c b = new Bd__c(Emp_Name__c = emp.EmpName__c);

                             

                                 insert b;

                    

                    }

 

       }

 

}

 

All Answers

devendra dhakadevendra dhaka

Write down a Apex Class which is implements Schedulable

 

and inside a class write a method which will be creating an instance of that class BD

 

 

 

global  class DemoClass impelements Schedulable

  {

 

     global void execute(SchedulableContext sc)

          {

                       createBD();

          }

 

    public void createBD()

    {

 

        BD bd= new BD(Name='demoname');

       insert bd;

 

   }

}

 

 

 

 

HOPE THIS HELPS :)

 

LoveyLovey

Thnks for ur reply but i need to add this record on cheking whether Employee object is having any record with Date equals to TODAY. How should i perform this check????. I have written this class but not sure how to make it work if i got 2 records on running this query.

 

 

 

 

global class Birthday implements Schedulable

 

 

 

{        global void execute(SchedulableContext ctx)

 

         {           Employee__c emp = [Select EmpName__c from Employee__c where Next_Birthday__c =TODAY];

                     

                     if(emp.EmpName__c <> Null)

                   

                    {            Bd__c b = new Bd__c(Emp_Name__c = emp.EmpName__c);

                             

                                 insert b;

                    

                    }

 

       }

 

}

 

 

Just need you to Verify this apex code.

devendra dhakadevendra dhaka

{        global void execute(SchedulableContext ctx)

 

         {           Employee__c emp = [Select EmpName__c from Employee__c where Next_Birthday__c =:System.TODAY()];

                     

                     if(emp <> Null)

                   

                    {            Bd__c b = new Bd__c(Emp_Name__c = emp.EmpName__c);

                             

                                 insert b;

                    

                    }

 

       }

 

}

 

This was selected as the best answer