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
raysfdc1988raysfdc1988 

how to write scedular apex to create a opportunity record on perticular day of every month.

Hi,
I want to write a schedur apex code which will automaticaly creates opportunity record.
In detail: I have 5 accounts records now i want create 5 opportunity records on perticular date of every month(11/18/2014).

Please help to write code for this.

thanks

 
pconpcon
I did something very similar and wrote about it [1]. I would review this and if you have any specific questions about the code or have any specific questions after writing your code, please let me know.

[1] http://blog.deadlypenguin.com/blog/2012/05/26/scheduled-actions-in-salesforce-with-apex/
raysfdc1988raysfdc1988
Hi Pcon
Thnak you very much..I read ur blog..Its good ..Now I wrote some Scedular apex.
But I am unable to insert a record
global class scheducleOpportunity implements schedulable{

global void execute(SchedulableContext scx)
{

list<account> accountlist= new list<Account>([select id, name,AccountNumber,Billing_Date__c,Phone from account]);
list<Opportunity> newopp= new list<Opportunity>();

for(account a:accountlist)
{
Opportunity opp=new Opportunity();
opp.name=a.name+'Invoice';
opp.Account.name=a.name;
opp.StageName='Pending';
opp.CloseDate=a.Billing_Date__c;
newopp.add(opp);
}
insert newopp;
}

}

Please help to whats a mistake in code.. And how to add products belonging to Account contact Roles
pconpcon
I think your issue is that you are not setting the AccountId on the opportunity properly.
 
global class scheduleOpportunity implements schedulable {
    global void execute(SchedulableContext scx) {
        List<Opportunity> newOpp= new list<Opportunity>();

        // Probably want to make this a little more selective
        //   or you could hit govenor limits
        for (List<Account> accountList: [
            select AccountNumber,
                Billing_Date__c,
                Name,
                Phone
            from account
        ]) {
            for (Account account: accountList) {
                newOpp.add(new Opportunity(
                    Name = account.Name + ' Invoice',
                    AccountId = account.Id,
                    StageName = 'Pending',
                    CloseDate = account.Billing_Date__c
                ));
            }
        }  

        if (!newOpp.isEmpty()) {
            insert newopp;
        }
    }
}

This version is a little bit more bulk ready and should generate your opportunities correctly.

NOTE: This code has not been tested and may contain typographical / logical errors