1

I have Consumed some Rest Api in Salesforce. for Callout the REST API I used Batch class and this batch class is Called in Trigger.It work fine. Pls Find Below Condition

1: Rest Api Call when New Opportunity is Created with Some Condition Met Like StageName is "Value Proposition" then Web service is Called and get Response. This Response is Stored in Custom fields on Opportunity.

Problem is That
when new Opportunity Create with Criteria the Response don't store Quickly i have to refresh Opportunity Page then After i able to see Response.

I want that my Response Should Quick store as soon as Opportunity is Created.

2
  • initially i Used @Future annotation but after i read some Documents. According to that Documents i have developed One Apex class then this Class is called in Batch Class.
    – Sfdce
    Jan 23, 2017 at 11:52
  • 1
    You might do well with a visualforce page overriding the new action that has a custom save method that makes the callout synchronously. In addition to the trigger for records created outside of the ui.
    – martin
    Jan 23, 2017 at 12:14

2 Answers 2

2

Your requested batches will be queued by Salesforce and completed sometime in the near future. As your save is synchronous, the save will most likely complete before your batches are executed and the Standard Page will only load with the data present at the time of load (e.g. not the data that you are saving via batches). Unfortunately, the view-state architecture of the Standard pages does not allow you to constantly pull from your Database and does not allow you to attach a listener to the callback of your asynchronous batches.

However, if this is a requirement and you have to do this development, there is 2 possible work-arounds that I can think of:

  1. Use an Inline Visualforce page to display the data from the Asynchrnous Batch Job by "pulling" via Javascript. That is, repetitively set timeouts to call an @RemoteAction that queries for your information until the batches have returned. You can set a "Flag" using the batch's finish() method if necessary.

  2. If you don't want an Inline Visualforce page to display the data, create an Inline Visualforce page with 0 height and use it to execute the same Javascript code that I mentioned in #1. However, instead of displaying the information in the Inline Visualforce page, you can force a reload of page.

Obviously these are both "hackish" answers, but if it is a requirement hopefully they can help you temporarily solve the problem or spark some thought for another way of tackling the issue.

1

Thanks for such a Valuable Response. Finally got Solution for this. I creat one field on Opportunity called Refresh_Count__c and this field is used in my Batch class When my Batch class run and Web service is called and Refresh_Count__c is Increase by 1 (Default values is 0).

when new Opportunity is Create Default value for Refresh_Count__c is 0

public class AutoRefreshOpportunity{

    public Boolean refreshPage{get;set;}
    private final Opportunity Opp;
    public Opportunity Oppo;

    // set the value to false in the constructor
    public AutoRefreshOpportunity(ApexPages.StandardController stdController){
        refreshPage = false;
        opp = (Opportunity)stdController.getRecord();
        System.debug('Opp Refresh Data::::::' + ' ' + opp);
        AutoRefreshDetailsPage();
    }   


    // This Method is Called in Constructor
    public void AutoRefreshDetailsPage(){
     try{
            Oppo = [SELECT
                   id, 
                   Refresh_Count__c
                   FROM
                   Opportunity
                   WHERE
                   Id = : opp.Id ];
        }

        catch(Exception Ex){
          Oppo = null;
        }   
        System.debug('Opp Refresh Data*****' + ' ' + Oppo);
        if(Oppo != null){

            if(Oppo.Refresh_Count__c == 0){
                refreshPage = true;
                system.debug('Inside IFFF::::' + ' ' + refreshPage);
            }
            else{

                refreshPage = false;
                system.debug('INSIDE ELSE ::::' + ' ' + Oppo.Refresh_Count__c + ' ' + refreshPage);
            }
        }
    }
}

VF Page is

<apex:page StandardController="Opportunity" extensions="AutoRefreshOpportunity"  tabStyle="Opportunity">

<!-- AUTOREFRESH STANDARD DETAILS  PAGE OF OPPORTUNITY  -->
 <apex:outputPanel id="refresh" rendered="true">
 <h1> Auto Refresh Opportunity </h1>
 <apex:outputPanel id="refresh1" rendered="{!refreshPage}">
  <script>
   window.top.location='/{!Opportunity.id}';
  </script>
 </apex:outputPanel>
</apex:outputPanel>
</apex:page>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .