Using Lightning Components In Visualforce page

In this blog, I am going to explain how to invoke the lightning component from the visualforce page by using Lightning out. Add Lightning components to your Visualforce pages to combine features you’ve built using both solutions. Implement new functionality using Lightning components and then use it with existing Visualforce pages. Lightning Components for Visualforce is based on Lightning Out, a powerful and flexible feature that lets you embed Lightning components into almost any web page.

Step 1: Create Sample Component

Create a lightning component with below code which contains two attributes

Step 2: Create  Lightning Dependency App

To use Lightning Components for Visualforce, define component dependencies by referencing a Lightning dependency app. This app is globally accessible and extends ltng:outApp. The app declares dependencies on any Lightning definitions (like components) that it uses.

Step 3: Using in Visualforce from the page

There are three steps to add Lightning components to a Visualforce page.

  1. Add the Lightning Components for Visualforce JavaScript library to your Visualforce page using the <apex:includeLightning/>  component.
  2. To reference this app on your page, use the following JavaScript code.
  3. Finally, add your top-level component to a page using $Lightning.createComponent(String type, Object attributes, String locator, function callback). This function is similar to $A.createComponent(), but includes an additional parameter, domLocator, which specifies the DOM element where you want the component inserted.

Here is the final code.

 

 

Salesforce Einstein Intent

Introduction

In this blog, I am going to explain how to use salesforce Einstein Intent.  Einstein Intent  Categorize unstructured text into user-defined labels to better understand what users are trying to accomplish. You can use Einstein Intent API to analyze text from emails, chats, or web forms  and many other use cases like

  • Determine what products prospects are interested in and send customer inquiries to the appropriate salesperson.
  • Identify topics across unstructured text in emails, meeting notes, or account notes to summarize key points.
  • Route service cases to the correct agents or departments, or provide self-service options.
  • Understand customer posts to provide personalized self-service in your communities

Prerequisites

Salesforce Einstein Platform Services APIs user OAuth token for authenticating. To generate a token, you create a JWT payload, sign the payload with your private key, and then call the API to get the token. So you need to get your own einstein_platform private key. please follow this steps to get your einstein_platform key.

  1. Go to the sign up page.
  2. Click Sign Up Using Salesforce.
  3. On the Salesforce login page, type your username and password, and click Log In.
  4. Click Allow so the page can access basic information, such as your email address, and perform requests.
  5. Download Key to save the key locally. The key file is einstein_platform.pem
  6. Upload the einstein_platform.pem key to Salesforce files.
  7. Copy JWTBearerFlow and JWT Apex Class from this git repository https://github.com/salesforceidentity/jwt

Refer this link for more information on the above steps.

Create custom metadata type or custom setting whatever works for you here to store the service endpoint, token endpoint and account details. Below is the custom metadata type which we are going to use it in this blog post.

Let’s Get Started.
As we are going to user cURL for model upload and training, we need to Generate a token for authentication. Go to this link and generate the token as shown below. We are going to use this generated token for model upload and train the model for predictions.

Remote Site Setting

Add remote site setting as shown below.

 Step 1: DEFINE YOUR CLASSES AND GATHER DATA

In this step, you define the labels that you want the model to output when text is sent into the model for prediction. Then you gather text data for each of those labels, and that text is used to create a model.This is typically the most time-consuming part of the process. To make it easier for you to go through these steps, we provide a case routing .csv file that you can use.

The labels in the case routing dataset define the intent behind the text. The intent can then be used to route that case to the right department. Those labels are:

  • Billing
  • Order Change
  • Password Help
  • Sales Opportunity
  • Shipping Info

STEP 2: CREATE THE DATASET

In this step, you use the data you gathered to create a dataset. In the following command, replace with<TOKEN> your JWT token and run the command. This command:

  • Creates a dataset called case_routing_intent from the specified .csv file by accessing the file via a URL
  • Creates five labels as specified in the .csv file

To verify that the data has been loaded, make a call to get the dataset. Replace with<TOKEN> your JWT token and with<DATASET_ID> ID of the dataset you created.

Once you run the above command you will get below response with each label  in your dataset.

STEP 3: TRAIN THE DATASET 

Now you need to train your data set to create prediction ready model. Use this command to train the dataset and create a model. Replace <TOKEN> with your JWT token and <DATASET_ID> with ID of the dataset you created

You will get below response from the above service.You will get modelId which we will use it for further predictions.

 

Use the modelId to make this call and get the training status. Replace<TOKEN> with your JWT token and withMODEL_ID the ID of the model you created.


 


 

STEP 4: SEND TEXT IN FOR PREDICTION

Now your model is ready to go! To test it out, send some text in for prediction. This cURL call takes the ofmodelId the model from which you want to return a prediction and the text string to analyze. Replace with<TOKEN> your JWT token and with<MODEL_ID> the ID of your model.

The response looks like this JSON. The model predicts that the text indicates that the user has a comment or question about shipping, so the model returns asShipping Info the top probability. Your app can then use this information to route the case to the right department or agent.

 

Code :

 

Lightning component

 

 

 

You can refer this link for additional information .

 

Salesforce Reports and Dashboards API via Apex Export PDF

Sample code to export the report into PDF format by using apex report and dashboards API

Export tabular Report as PDF 

 

To Run the code pass the report Id as query parameter to teh page as shown below

 

 

 

 

Asynchronous Callouts from Visualforce Pages with Continuations

In this blog, I am going to explain how to use salesforce  Continuations Asynchronous Callouts from the visualforce page.Continuations  Apex feature that allows you to escape the limit of ten concurrent long-running callouts. Asynchronous Callouts work through a pattern called a Continuation; your controller creates its HTTP request as it normally would, but, instead of firing it off there and then, you create a Continuation object, pass the HTTP request and an Apex callback method to the Continuation, and return it to the platform for processing.Use asynchronous callouts to make long-running requests from a Visualforce page to an external Web service and process responses in callback methods. Asynchronous callouts that are made from a Visualforce page don’t count toward the Apex limit of 10 synchronous requests that last longer than five seconds. As a result, you can make more long-running callouts and you can integrate your Visualforce pages with complex back-end assets.An asynchronous callout is a callout that is made from a Visualforce page for which the response is returned through a callback method. An asynchronous callout is also referred to as a continuation.This diagram shows the execution path of an asynchronous callout, starting from a Visualforce page. A user invokes an action on a Visualforce page that requests information from a Web service (step 1). The app server hands the callout request to the Continuation server before returning to the Visualforce page (steps 2–3). The Continuation server sends the request to the Web service and receives the response (steps 4–7), then hands the response back to the app server (step 8). Finally, the response is returned to the Visualforce page (step 9).

Diagram for the execution flow of a continuation

Using Continuation 

Step1: Action Method

To use asynchronous callouts, create a Continuation object in an action method of a controller, and implement a callback method.To invoke an asynchronous callout, call the external service by using a Continuation instance in your Visualforce action method. When you create a continuation, you can specify a timeout value and the name of the callback method. For example, the following creates a continuation with a 60-second timeout and a callback method name of processResponse.

Next, associate the Continuation object to an external callout.

The method that invokes the callout (the action method) must return the Continuation object to instruct Visualforce to suspend the current request after the system sends the callout and waits for the callout response. The Continuation object holds the details of the callout to be executed.Here is the complete code to invoke from the action.

Step 2: Defining a Callback Method

The response is returned after the external service finishes processing the callout. You can specify a callback method for asynchronous execution after the callout returns. This callback method must be defined in the controller class where the callout invocation method is defined. You can define a callback method to process the returned response, such as retrieving the response for display on a Visualforce page as shown below .

 

Example code

In the following example application, the button action is implemented in an Apex controller method. The action method creates a Continuation and returns it. After the request is sent to the service, the Visualforce request is suspended. The user must wait for the response to be returned before proceeding with using the page and invoking new actions. When the external service returns a response, the Visualforce request resumes and the page receives this response.

 

Simple Apex class

 

Visuaforce page

 

Continuation Status Code 

  • 2000: The timeout was reached, and the server didn’t get a chance to respond.
  • 2001: There was a connection failure.
  • 2002: Exceptions occurred.
  • 2003: The response hasn’t arrived (which also means that the Apex asynchronous callout framework hasn’t resumed).
  • 2004: The response size is too large (greater than 1 MB).

Limitation 

  • You can make up to three asynchronous callouts in a single continuation.
  • Asynchronous callouts are supported only through a Visualforce page. Making an asynchronous callout by invoking the action method outside a Visualforce page, such as in the Developer Console, isn’t supported.
  • Asynchronous callouts are available for Apex controllers and Visualforce pages saved in version 30.0 and later. If JavaScript remoting is used, version 31.0 or later is required.
  • This Continuation pattern does not allow you to make DML operations before the web service is invoked.
  • The server has to return within 2 minutes, or there will be a timeout

 

 

Apex Web service call from Process Builder

In this blog, I am going to explain how to call the simple web service calls from the process builder. Use the InvocableMethod annotation to identify methods that can be run as invocable actions.Invocable methods are called with the REST API and used to invoke a single Apex method.In this post, I am using Google URL shortening API to shorten the lead website URL.The below code us for the same.

Now go and create a process builder as shown below on lead object.

Add the below conditions.

create an immediate apex action as shown below.

 

Invocable Method Considerations

  • The invocable method must be static and public or global, and its class must be an outer class.
  • Only one method in a class can have the InvocableMethod annotation.
  • Triggers can’t reference invocable methods.
  • Other annotations can’t be used with the InvocableMethod annotation.

Inputs and Outputs

There can be at most one input parameter and its data type must be one of the following:

  • A list of a primitive data type or a list of lists of a primitive data type – the generic Object type is not supported.
  • A list of a sObject type or a list of lists of a sObject type – the generic sObject type is not supported.
  • A list of a user-defined type, containing variables of the supported types and with the InvocableVariable annotation. Create a custom global or public Apex class to implement your data type, and make sure your class contains at least one member variable with the invocable variable annotation.

If the return type is not Null, the data type returned by the method must be one of the following:

  • A list of a primitive data type or a list of lists of a primitive data type – the generic Object type is not supported.
  • A list of a sObject type or a list of lists of a sObject type – the generic sObject type is not supported.
  • A list of a user-defined type, containing variables of the supported types and with the InvocableVariable annotation. Create a custom global or public Apex class to implement your data type, and make sure your class contains at least one member variable with the invocable variable annotation.