You need to sign in to do that
Don't have an account?
Lovey
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
{ 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
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 :)
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.
{ 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;
}
}
}