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
Isak Stensmar 8Isak Stensmar 8 

How to set up batch/schedulable class and access it from VF page?

Hello,

I have a problem with one of my functions in one of me classes.
The scenario is as follows:
I override the Save functionality by adding some other code to the function after the record has been inserted.
The other code include a query to get the latest Opportunity, which is created by a trigger when a new record is inserted.
I then have a PageReference that sets the current page to a process template, that is implemented by another app from appexchange, which need an OpportunityID.
public PageReference save()
    {
        List<Opportunity> opp;
        insert myMD_Object;
        try{
        	opp = [SELECT Id FROM Opportunity ORDER BY CreatedDate DESC];
        }
        catch(Exception e) 
       {}
        currentPage = new PageReference('/apex/scrive__scrivedocumentfromtemplate?Id=a0E4E000000DAp3UAG&sourceRecordId=' + opp[0].id);
        currentPage.setRedirect(true);
        return currentPage;
    }
The problem is that when I create and insert a new record, which would trigger insertion of an oppertunity aswell, the query after the insertion doesn´t find it. 

I´ve read a little about how the function is one big transaction and that could be one reason to why the query doesn´t find it, since the transaction isn´t complete. So I thought about how I should solve this, if I could split up the transaction in to two parts.
This article http://salesforce.stackexchange.com/questions/77490/can-i-have-multiple-transactions-within-1-schedulable-class had an interesting way of solving the problem, but I couldn´t make it work on my own code. I´ve read about BatchableContext and SchedulableContext but it didn´t make me much wiser.
That article doesn´t talk about how you can call on these functions from a visualforce page either, which I need to do.
So I´m wondering how I should solve this. I liked the way that the article above was using but can´t seem to figure it out. I also need to figure out if/how I can call on a batch or schedule from a visualforce page.

Any thoughts?

Regards,
Isak