Monday, 14 August 2017

Language Translation in VisualForce Page | Translation Workbench


Salesforce offer functionality through that we'll be able to create single VF page which can be translated in several languages based on language/locale preference of current logged in user. We just need to upload the translation of all custom fields that you're going to use in VF page . To render the VF page in particular language, use Language attribute on <apex:page> tag.

Please follow below step to achieve the same. 
Step 1) Enable Translation Workbench and all Translation   Please follow below post to enable Translation Workbench
URL :-Translation Workbench - Salesforce ( Multilingual picklist values )

Step 2) You Can use the "Language" attribute of the VF page.

Step 3) Try to use Salesforce standard way to is to use apex:inputField tag with assigned sobject field. In this case a field will be generated automatically with respect to the current user language. 
Apex Class:-

public with sharing class TranslationWorkbenchController {
    public string selectedLang{get;set;}
    public List<selectoption> listOfLang {get;set;}
    public TranslationWorkbenchController(ApexPages.StandardController controller) {
        selectedLang='en';

        listOfLang = new List<selectOption>();
        listOfLang.add(new selectOption('en','English'));
        listOfLang.add(new selectOption('it','Italian'));
        listOfLang.add(new selectOption('es','Spanish'));
        listOfLang.add(new selectOption('de','German'));
        listOfLang.add(new selectOption('fr','French'));
    }
}


VF Page :-


<apex:page standardcontroller="Account" extensions="TranslationWorkbenchController" language="{!selectedLang}" >
<apex:form >

        <apex:selectList value="{!selectedLang}" size="1">
            <apex:selectoptions value="{!listOfLang}"/>
            <apex:actionsupport event="onchange"/>
        </apex:selectlist>
   
        <apex:pageblock >
            <apex:pageblocksection >
                <apex:inputfield value="{!Account.Name}"/>
                <apex:inputfield value="{!Account.Type}"/>
                <apex:inputfield value="{!Account.Industry}"/>
                <apex:inputfield value="{!Account.BillingCountry}"/>
            </apex:pageblocksection>
        </apex:pageblock>
   
</apex:form>
</apex:page>


Screen shot :-

English :-


Spanish:-




Thanks
Amit Chaudhary

Sunday, 6 August 2017

Webservice callout from Scheduled Apex | Report in Batch Job | System.CalloutException: Callout from scheduled Apex not supported


We used to get "System.CalloutException: Callout from scheduled Apex not supported" Error when we are making a webservice callout from a class which is implementing Database.Schedulable interface because Salesforce does not allow us to make callouts from Schedulable classes.

Issue

Sample Scheduler Class


global class SampleScheduler implements Schedulable{
    
    global void execute(SchedulableContext sc) 
    {        
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals');
        request.setMethod('GET');
        HttpResponse response = http.send(request);

        if (response.getStatusCode() == 200) {
            Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
            List<Object> animals = (List<Object>) results.get('animals');
            System.debug('Received the following animals:');
            for (Object animal: animals) {
                System.debug(animal);
            }
        }
    }    
}

Execute Above Scheduler Class


String sch = '0 00 * * * ?';
system.schedule('Test Schedule', sch, new SampleScheduler());


Error Screen Shot 

Scheduler: failed to execute scheduled job: jobId: 7076A00000EmLPu, class: common.apex.async.AsyncApexJobObject, reason: Callout from scheduled Apex not supported




Solution :- We can solved this issue with below solution.
  1. @future method
Scheduler Class

global class SampleScheduler implements Schedulable{
    
    global void execute(SchedulableContext sc) 
    {        
 
  BatchUtilClass.futureMethodSample();
    }    
}

Future Class Method

public class BatchUtilClass {
    @future(callout=true)
    public static void futureMethodSample() {
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals');
        request.setMethod('GET');
        HttpResponse response = http.send(request);

        if (response.getStatusCode() == 200) {
            Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
            List<Object> animals = (List<Object>) results.get('animals');
            System.debug('Received the following animals:');
            for (Object animal: animals) {
                System.debug(animal);
            }
        }
    }
}


Thanks
Amit Chaudhary