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
srodriguezchsrodriguezch 

Automatic creation of opportunities

I want to create opportunity objects on a scheduled basis. I want to have a scheduled process run every night and based on a date listed on the account, I want this process to create an opportunity for that account. Can someone help me determine what the best way to do this is?

Best Answer chosen by Admin (Salesforce Developers) 
TheIntegratorTheIntegrator

you could try something like this

 

global class AccountCronJob implements Schedulable{
  
    global void execute(SchedulableContext SC) {
        Integer DayOfEvent   = date.today().day();
        Integer MonthOfEvent = date.today().month();
        List <Opportunity> ops = new List <Opportunity>();
        
        List <Account> accounts = [Select Id, Some_Date__c, Name From Account 
                                    Where DAY_IN_MONTH(Some_Date__c)= :DayOfEvent
                                    And   CALENDAR_MONTH(Some_Date__c)= :MonthOfEvent];
        for(Account acc:accounts){
            Opportunity newOp = new Opportunity();
            newOp.AccountId = acc.Id;
            newOp.Name = acc.Name+DayOfEvent+MonthOfEvent;
            newOp.CloseDate = acc.Some_Date__c;
            newOp.StageName = 'Some Stage';
            newOp.Type = 'Some Type';
            ops.add(newOp);
        }                                  
        insert(ops);
    }
}

 

you will need to define you own account selection criteria in the query as well as the values you need to set in the new opportunity, but this should get you started.

All Answers

TheIntegratorTheIntegrator

you could try something like this

 

global class AccountCronJob implements Schedulable{
  
    global void execute(SchedulableContext SC) {
        Integer DayOfEvent   = date.today().day();
        Integer MonthOfEvent = date.today().month();
        List <Opportunity> ops = new List <Opportunity>();
        
        List <Account> accounts = [Select Id, Some_Date__c, Name From Account 
                                    Where DAY_IN_MONTH(Some_Date__c)= :DayOfEvent
                                    And   CALENDAR_MONTH(Some_Date__c)= :MonthOfEvent];
        for(Account acc:accounts){
            Opportunity newOp = new Opportunity();
            newOp.AccountId = acc.Id;
            newOp.Name = acc.Name+DayOfEvent+MonthOfEvent;
            newOp.CloseDate = acc.Some_Date__c;
            newOp.StageName = 'Some Stage';
            newOp.Type = 'Some Type';
            ops.add(newOp);
        }                                  
        insert(ops);
    }
}

 

you will need to define you own account selection criteria in the query as well as the values you need to set in the new opportunity, but this should get you started.

This was selected as the best answer
srodriguezchsrodriguezch

Thank you!