+ Start a Discussion
Ankit Khurana24Ankit Khurana24 

HOW TO Create a scheduler

I need to create a schedular which will find all opportunity whose status is lost and will create campaign for them and add their contact as campaign member.

mrajmraj

Hi Ankit,

 

To schedule an apex class u need to implement Schedulable interface in ur class. Ur class will look as describe below
 

Pseudo code:

 

global class Class_Name implements Schedulable{

          

          // method that will be called when the scheduled job runs

           global void execute(SchedulableContext SC) {

                 // call a method that will create Campaign for lost opportunity

                 createCampaign();
           }
          

           // method to create Campaign for lost opportunity

            public void createCampaign() {

           

                   // Logic to fetch lost opportunity ie. using SOQL

                   // logic to create Campaign

                   // batch insert 

            }

}

 

Now schedule this class using Schedule Apex button on Apex Classes page

Ankit Khurana24Ankit Khurana24

first please let  me know how do i create  a camapgin for the lost opportunities..i did like this but its not working..

public class moveopportunity
{
List<Opportunity> selectopp;
public string campaignid1;
public string conactid;
public moveopportunity()
{
selectopp=[Select o.Id, o.CampaignId, (Select OpportunityId, ContactId From OpportunityContactRoles) From Opportunity o where Opportunity.StageName =:'Closed Lost'];
}
public void move()
{
if(selectopp!=null)
{
for(Opportunity O:selectopp)
{
O.CampaignId ='70190000000MjHs';
update O;
}
}
}
}

Ankit Khurana24Ankit Khurana24

how will i create a campaign