How to access metadata from APEX?

How to access metadata from APEX? – Summer’17 release feature

Access Metadata from Apex, What is metadata in salesforce? In Salesforce, if you create object, field, class, page, trigger and any other component type which represents org configuration and customization will be stored in the form of metadata. We can access this metadata by using Metadata API. With Summer’17 Salesforce release, we can use classes in the metadata name space to access metadata components from Apex Code.

As per the salesforce release notes documentation, Access metadata from your Apex code to handle below tasks.

Customizing app installs or upgrades: During or after an install (or upgrade), your app can create or update metadata to let users configure your app.

Customizing apps after installation: After your app is installed, you can use metadata in Apex to let admins configure your app using the UI that your app provides rather than having admins manually use the standard Salesforce setup UI.

Securely accessing protected metadata: Update metadata that your app uses internally without exposing these types and components to your users.

Creating custom configuration tools: Use metadata in Apex to provide custom tools for admins to customize apps and packages.

This feature (Metadata access in apex) is currently limited to custom metadata records and page layouts. We can retrieve, create or update custom metadata but we cannot delete metadata through Apex.

– Use Metadata.Operations class to retrieve and deploy metadata.

– Use the Metadata.Operations.retrieve() method to synchronously retrieve metadata from the current org.

-Use the Metadata.Operations.enqueueDeployment() method to asynchronously deploy metadata to the current org.

Refer Salesforce Apex developer guide to know how to use this feature.

Salesforce integration testing – WebServiceMock interface

Salesforce integration testing – WebServiceMock interface

Salesforce integration testing: Generating web service class and how to use those web service classes was explained in my previous post. Here I am going to explain about how to write test class for web services class generated WSDL2Apex.

Every Salesforce developer know about importance of test classes. At least 75% code coverage is required to deploy code to production. Test coverage includes classes generated by WSDL2Apex.

As a Salesforce developer, to write test classes for web service you should spend some time as test methods do not support web service callouts. Apex provides inbuild interface “WebServiceMock” and the “Test.setMock” method. Developers can use this interface to receive fake response in a test method.

What is WebServiceMock interface?

WebServiceMock interface enables sending fake response when testing web service callouts of a class auto-generated from WSDL.

doInvoke is the method of this interface.

doInvoke(stub, soapRequest, responseMap, endpoint, soapAction, requestName, responseNameSpace, responseName, responseType)

The implementation of this method is called by the Apex Runtime to send a fake response when a service callout is made after Test.setMock has been called.

You can refer below Salesforce documentation to know more about this interface.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_interface_webservicemock.htm

SOAP web service – Creating a class from WSDL

SOAP web service – How to creating a class from WSDL document?

SOAP web service class can be generated automatically by using WSDL document. Generating WSDL document allows Salesforce developers to make callouts to the external web service in their Apex code.

Follow below steps to generate apex class from a WSDL:

1.Go to Apex Classes form setup menu.
2.Click “Generate from WSDL” button.

SOAP Web Service 1
3.Select WSDL document(XML file) you are using to generate apex class by clikcing choose file lookup button. This WSDL document is base to generate apex class.

SOAP Web Service 2Here I selected Test-SOAP webservice WSDL file which is rounded by red color line in the above image. You can select WSDL by clicking on choose File button.

4.Click Parse WSDL button which is highleted in the above image to verify WSDL document contents. It generates default class name based on WSDL document. It report errors in case of the WSDL contains schema types or constructs that aren’t supported by Apex classes, or if the resulting classes exceed the 1-million-character limit on Apex classes. For example, the Salesforce SOAP API.

SOAP Web Service 3You can rename the class. And click on Generate Apex button. The final page of the wizard shows which classes were successfully generated, along with any errors from other classes. The page also provides a link to view successfully generated code.

Generated Apex classes include stub and type classes for calling the third-party Web service represented by the WSDL document. These classes allow you to call the external Web service from Apex.

When you generate Apex classes by using WSDL, it generated 2 classes, one is with given name for synchronus callouts and other is prefixed with Async for asynchronus callouts.

SOAP Web Service 4

Refere here to know more about SOAP callouts: https://trailhead.salesforce.com/modules/apex_integration_services/units/apex_integration_soap_callouts

Salesforce Integration – SOAP API basics

SOAP API basics

SOAP API (Simple Object Access Protocol API) can be used to create, retrieve, delete, update records. We can use SOAP API in any language that supports web services.

We can create, update, delete, retrieve records in force.com from any external system that supports SOAP based web services by using force.com SOAP API (like Java, .net etc.…) with more than 20 different calls.

SOAP-API also allows you to maintain passwords, perform searches, retrieve metadata.

Salesforce provides two different SOAP API WSDLs (WSDL: Web service description language).

     1. Enterprise WSDL

     2. Partner WSDL

What is WSDL?

WSDL (Web service description language) is an XML document which contains a standardized description on how to communicate using a web service.

What is Enterprise WSDL?

Enterprise WSDL is a strongly typed WSDL for customers who want to build an integration with their Salesforce organization.  It is intended primarily for customers.

What is partner WSDL?

Partner WSDL is a loosely typed WSDL for customers and partners who are building client applications for multiple originations. This WSDL can be used to access data within any organization. It I intended primarily for partners.

How to download WSDL file?

Click Setup -> Customize -> Develop -> API
Click the link to download the appropriate WSDL.
and Save the file locally, giving the file a “.wsdl” extension.

What are the different types of Salesforce APIs?

Different types of Salesforce APIs

Salesforce APIs: Below are the different types of Salesforce APIs available.

REST API: This is useful to access objects in your Salesforce organization using REST.

SOAP API: This API is useful to integrate your Salesforce organization data with other application using SOAP.

Bulk API: This API is useful to work with large number of records (Load or delete large number of records).

METADATA API: This API is useful to manage build tools and manage customizations.

Streaming API: This API provides a stream of data reflecting data changes in your Salesforce organization.

Tooling API: This API is useful to build custom development tools for force.com applications.

Chatter REST API: This API is useful access chatter feeds and social data using REST.

Wave Analytics REST API: We can access Wave analytics data sets programmatically by using this API.

Apex REST API: This API is useful build your own REST API in APEX. This API exposes Apex Classes as RESTful web services.

Apex SOAP API: This API is useful to create custom SOAP web service. This API exposes Apex Classes as SOAP web service.

Salesforce Integration– Apex REST API basics

REST API or Representational state transfer(REST) or RESTful web service basics

As per previous post REST API is based on HTTP callouts. Force.com platform provides multiple built in classes to work with HTTP services and create services like GET, POST, PUT and delete.

Note: HTTP classes also useful to integrate SOAP based services as an alternate option to generate code from WSDL.

Below are the pre-defined HTTP classes.

HTTP Class: This class is useful to initiate HTTP request & Response.

HttpRequest Class: This class supports Request types like GET, POST, PUT, DELETE, TRACE, CONNECT, HEAD, OPTIONS. Supports Request headers, redirects, read and connection timeouts and Content of the message body.

HttpResponse Class: This class is useful to handle the HTTP response returned by HTTP like HTTP status code, Response headers and content of the response body.

Example/Sample Class to understand basic of HTTP callouts:

public class HttpCalloutExampleClass {
// Pass in the endpoint to be used using the string url
public String getCalloutResponseContents(String url) {
// Instantiate a new http object
Http ht = new Http();
/* Instantiate a new HTTP request, specify the method (GET) as well as the endpoint */
HttpRequest req = new HttpRequest();
req.setEndpoint(url);
req.setMethod(‘GET’);
// Send the request, and return a response
HttpResponse res = ht.send(req);
return res.getBody();
}
}

Salesforce Integration – Introduction

Salesforce Integration/Apex Integration

Salesforce Integration/Apex Integration Introduction

Apex Callout: A callout makes a call to external web service or sends HTTP request to from Apex Code, and then receives the response.

An Apex callout helps you to integrate Apex code with an external web service.

Below are the two types Apex Callouts: 

SOAP: Web service callout to SOAP web service use XML. This require a WSDL document for code generation.

REST: HTTP callouts to services typically use REST with JSON.

Both SOAP & REST callouts are similar in terms of sending a request to a service and receiving a response. WSDL-based callouts apply to SOAP Web services, HTTP callouts can be used with any HTTP service, either SOAP or REST.

Remote site administration and security: Register you external site in salesforce remote site settings before callout can call external site, otherwise callouts will fail. Salesforce prevents calls to unauthorized network addresses.

How to add remote site setting?

To add remote site setting go to setup -> remote site setting Click New remote Site Setting enter remote site setting name and URL click on save.

Salesforce Integration

Remote Site Setting 1

Wave analytics in salesforce?

Wave analytics in salesforce?

– Wave analytics gives immediate access to powerful data insights through its intuitive point-and-click visual interface.
– You can use “Laptop/desktop, iphone & Ipad and apple watch” with wave analytics to answer key business questions.
– It is a self service analytics application that enables users to make sense of large amounts of data. Users can explore their data to discover new data insights. And also we can create dashboards to monitor key business metrics.

To enable wave-analytics feature you have to reach out Salesforce. Once this feature is enabled, admins have to assign required licences and permissions to users to access wave analytics.

Wave-analytics vs reports and dashboards:

Wave-analytics: It is designed for self-service data discovery, It extends beyond Reports and Dashboards to give you new views into your pipeline, end-to-end customer insight, and historical analytics to help you plan your next best step.

Report & Dashboard: It gives an instant snapshot of the metrics that matter to your business, including team performance, lead volume, and conversion rates.

How to deploy ApexTestSuite from one org to other org?

How to deploy ApexTestSuite from one org to other org?

deploy ApexTestSuite: Are you working/running on same set of test classes by creating test suites and manually creating test suites in your dev, test and multiple environments? If you create you test suite, you can migrate it to multiple environments using metadata migration tool now. “ApexTestSuite” is the metadata name to migrate apex test suite.

<?xml version=”1.0″ encoding=”UTF-8″?>

<Package xmlns=”http://soap.sforce.com/2006/04/metadata”>

    <types>       

        <members>TestSuite Name</members>

        <name>ApexTestSuite</name>

    </types>

    <version>38.0</version>

</Package>

Above script is sample package.xml script to migrate apex test suites. One you retrieve apex test suites, metadata components will be stored with .testSuite suffix and these files are stored in the testsuite folder.

If you are deploying new test classes along with test suite, then you have to add new apex classes to your package.xml

<?xml version=”1.0″ encoding=”UTF-8″?>

<Package xmlns=”http://soap.sforce.com/2006/04/metadata”>

    <types>       

        <members>TestSuite Name</members>

        <name>ApexTestSuite</name>

    </types>

                <types>       

        <members>ApexClass Name</members>

        <name>ApexClass</name>

    </types>

    <version>38.0</version>

</Package>

Apextestsuite

Test Suites – Salesforce? How to create test suite

What is test suites in Salesforce?

Test suite is a collection of test classes that you can run together. If you are working on specific set of test classes and running regularly, create a test suite with the list of test classes that you want to execute and run that test suite instead of running each class individually. And you can use this test suite regularly to run the list of test classes again and again. We can create test suites in the developer console.

How to create test suites?

  1. To create test Suite, go to developer console -> Select tab “Test” -> click New suite see the below image for reference.

Test Suite 1

2. Enter Suite name and click on ok. See the below image for reference.

Test Suite 2

3. Add list of classes you want execute from available test classes section to selected test classes See the below image for reference.

Test Suites

Use arrows which are highlighted with yellow color to move classes from one list to other. Only one test class is there in this list. You will more test classes if there are more on your salesforce org.

How to run test suites?

To run test suite, in developer console go to tab “Test” and click on New Test Suite run and select the test suite you want to execute and click on “Run suites” button.