Friday 29 December 2017

Set environment variable for Apache ANT

To use ant in windows, you need to download it and should configure the windows environment variable.

Download file here,


Steps to set path:

Download the latest ant zip file from apache ant official website and unzip it to the folder you want to store ant.

Example:


To add the ANT_HOME as the windows environment variable. Please Navigate to My Computer -> right click My Computer -> Properties -> Advanced system setting -> Environment Variables...


Update the Path variable, Append the %ANT_HOME%\bin to the end, so that you can run the ant's command anywhere.


Now path added successfully to verify that go to command prompt and type ant -version and click enter. You see the message like give an image.



Wednesday 20 December 2017

Create a Simple Camping List Lightning Component

1.Create a campingList component that contains an ordered list of camping supplies that include Bug Spray, Bear Repellant, and Goat Food.

<aura:component >
<ol>
<li>Bug Spray</li>
<li>Bear Repellant</li>
<li>Goat Food</li>
</ol>
</aura:component>

2.Create a campingHeader component that displays Camping List wrapped in an H1 tag with a font size of 18.

<aura:component >
    <h1>Camping List</h1>
</aura:component>

use this for style 

.THIS {
}
h1.THIS {
font-size: 18px;
}

3. Create a camping component that contains the campingHeader and campingList components.

<aura:component >
    <c:campingHeader/>
    <c:campingList/>
</aura:component>

Create a Packing List Item Component

Steps:


1.Create the component named campingListItem


2.Include the below code to complete this challenge


<aura:component >

<aura:attribute name="item" type="Camping_Item__c" required="true"/>

<ui:outputText value="{!v.item.Name}"/>

<lightning:formattedNumber value="{!v.item.Price__c}" style="currency"/>

<lightning:formattedNumber value="{!v.item.Quantity__c}" style="Number"/>

<lightning:input type="toggle" label="Packed" name="togglevalue" checked="{!v.item.Packed__c}" />



</aura:component>




Thanks :)

Monday 18 December 2017

Invalid Partner Network Status error

Invalid Partner Network Status error

Why this error occurs:

This error will occur when you are trying to forward records to connected org programmatically if the connected org doesn’t subscribe that object then this INVALID_PARTNER_NETWORK_STATUS error will be thrown.

How to solve this error:


Before forward the record from an object to which the connection check if the connected org subscribes the object that we are trying to forward.

How to run a single test methods from test classes in Salesforce

We can run specific test methods from test class in Salesforce. If you have many test methods in an Apex test class but you don’t want to run all of them, then you can select the specific method and run in the developer console.

In such scenarios, If you want to run failed test method out of 50 test methods in a class you can do this instead of running entire class.

Follow the below steps:

  • From developer console, select the Test -> New Run 

  • Select the test class and click add selected then select the methods, click Run


Sunday 17 December 2017

Run more than one test class at a time in Salesforce

Run more than one test class at a time

We can verify our apex code functionality by running test classes. We can run more than one test classes at a time from developer console.

o   From developer console, select the Test menu -> click New Run.



o   Select the classes you want to run, click add selected -> click run.



o   Now all the test classes will run asynchronously.

Thursday 25 May 2017

Lightweight Apex Trigger Framework

Overview:

A trigger handler framework is a way to remove logic from your triggers and enforce consistency across the platform. The framework itself will do the heavy lifting for you in terms of figuring out which kind of trigger is currently running and firing the correct logic.

Triggers should (IMO) be logic-less. Putting logic into your triggers creates UN-testable, difficult-to-maintain code. It's widely accepted that a best-practice is to move trigger logic into a handler class.
But the most important part of this framework is that it's minimal and simple to use.
Advantages of using a trigger framework:
  • Removing trigger logic from the trigger makes unit testing and maintenance much easier.
  • Standardizing triggers means all of your triggers work in a consistent way.
  • A single trigger per object gives full control over order of execution.
  • Prevention of trigger recursion.
  • It makes it easy for large teams of developers to work across an org with lots of triggers. I recently worked on an org which had triggers on around fifty objects, and the fact that every trigger was implemented in a consistent manner made it easy to go in and make changes or add new triggers.

The interface

The interface dictates which methods every trigger handler must implement, even if these methods have no code in them. By implementing the methods in this class, the Trigger Dispatcher (discussed below) can be confident that the trigger handler has a method for each of these events:
  1. Before/After Insert
  2. Before/After Update
  3. Before/After Delete
  4. After Undelete
  5. IsDisabled
Here’s the code:


public interface ITriggerHandler 

{
    void BeforeInsert(List<SObject> newItems);
    void BeforeUpdate(Map<Id, SObject> newItems, Map<Id, SObject> oldItems);
    void BeforeDelete(Map<Id, SObject> oldItems);
    void AfterInsert(Map<Id, SObject> newItems);
    void AfterUpdate(Map<Id, SObject> newItems, Map<Id, SObject> oldItems);
    void AfterDelete(Map<Id, SObject> oldItems);
    void AfterUndelete(Map<Id, SObject> oldItems);
    Boolean IsDisabled();
}

The Dispatcher

The dispatcher is responsible for making sure all of the applicable methods on your trigger handler are called, depending on the current trigger context. It also contains a check to make sure that the trigger has not been disabled. If the trigger has been disabled (more on this below), then the trigger events will not be fired (See lines 10/11).
Again, this is part of the framework and you do not need to change this code in any way. Just make sure the class is in your org.
Here’s the code:
public class TriggerDispatcher {
    /* Call this method from your trigger, passing in an instance of a trigger handler which implements ITriggerHandler.
  This method will fire the appropriate methods on the handler depending on the trigger context.
    */
    public static void Run(ITriggerHandler handler)    {   
        // Detect the current trigger context and fire the relevant methods on the trigger handler:
         // Before trigger logic
        if (Trigger.IsBefore )
        {
            if (Trigger.IsInsert)
                handler.BeforeInsert(trigger.new); 
            if (Trigger.IsUpdate)
                handler.BeforeUpdate(trigger.newMap, trigger.oldMap); 
            if (Trigger.IsDelete){
                handler.BeforeDelete(trigger.oldMap);
            }
        }         
        // After trigger logic
        if (Trigger.IsAfter)
        {
            if (Trigger.IsInsert)
                handler.AfterInsert(Trigger.newMap); 
            if (Trigger.IsUpdate)
                handler.AfterUpdate(trigger.newMap, trigger.oldMap); 
            if (trigger.IsDelete)
                handler.AfterDelete(trigger.oldMap); 
            if (trigger.isUndelete)
                handler.AfterUndelete(trigger.oldMap);
        }
    }
}

Creating a Trigger Handler

This trigger framework bundles a single TriggerHandler base class that you can inherit from in all of your trigger handlers. The base class includes context-specific methods that are automatically called when a trigger is executed.

Here’s the code:


public class OpportunityTriggerHandler implements ITriggerHandler {


    public void BeforeInsert(List<SObject> newItems){ // Your Logic}
    
    public void BeforeUpdate(Map<Id, SObject> newItems, Map<Id, SObject> oldItems){ // Your Logic}
    
    public void BeforeDelete(Map<Id, SObject> oldItems){ // Your Logic}
    
    public void AfterInsert(Map<Id, SObject> newItems){ // Your Logic}
    }   
    public void AfterUpdate(Map<Id, SObject> newItems, Map<Id, SObject> oldItems){ // Your Logic}
    
    public void AfterDelete(Map<Id, SObject> oldItems){ // Your Logic}
    
    public void AfterUndelete(Map<Id, SObject> oldItems){ // Your Logic}
}

Hooking up the trigger

Now we just need to hook the trigger itself in. Create a trigger on your object and make sure it fires on all events.We only need a single line of code to hook the trigger handler in via the dispatcher. See below.
Here’s the code:
trigger OpportunityTrigger on Opportunity (before insert, before update, before delete, after insert, after update, after delete, after undelete) {
    TriggerDispatcher.Run(new OpportunityTriggerHandler());
}
We simply call the static method on the TriggerDispatcher, and pass it a new instance of our OpportunityTriggerHandler. The framework takes care of the rest.
This may have seemed like a bit of work, but going forward you would only need to create the triggerhandler and one line trigger for any future triggers.

Thank You...😄😄😄
--------------------------------------------------------------------------------------------------------------------------

Apex Continuations: Asynchronous Callouts

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).
Execution Flow of an Asynchronous Callout
A typical Salesforce application that benefits from asynchronous callouts is one that contains a Visualforce page with a button that users click to get data from an external Web service. For example, the Visualforce page might get warranty information for a certain product from a Web service. This page can be used by thousands of agents in the organization. Consequently, a hundred of those agents might click the same button to process warranty information for products at the same time. These hundred simultaneous actions exceed the limit of concurrent long-running requests of 10, but by using asynchronous callouts, the requests aren’t subjected to this limit and can be executed.
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 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.
This is the Visualforce page of our sample application. This page contains a button that invokes the startRequest method of the controller that’s associated with this page. After the continuation result is returned and the callback method is invoked, the button renders the outputText component again to display the body of the response.
Visual force Page:
<apex:page controller="ContinuationController" showChat="false" showHeader="false">
   <apex:form >
      <!-- Invokes the action method when the user clicks this button. -->
      <apex:commandButton action="{!startRequest}" 
              value="Start Request" reRender="result"/> 
   </apex:form>
   <!-- This output text component displays the callout response body. -->
   <apex:outputText id="result" value="{!result}" />
</apex:page>
The following is the Apex controller that’s associated with the Visualforce page. This controller contains the action and callback methods.
  • Before you can call an external service, you must add the remote site to a list of authorized remote sites in the Salesforce user interface. From Setup, click Security Controls | Remote Site Settings, and then click New Remote Site.

EndPoint URL: https://raw.githubusercontent.com/Prasath18/JSON/master/StudentRecords

Apex controller
public with sharing class ContinuationController {
    // Unique label corresponding to the continuation
    public String requestLabel;
    // Result of callout
    public String result {get;set;}
    // Endpoint of long-running service
    private static final String LONG_RUNNING_SERVICE_URL = 
        '<Insert your EndPoint URL>';   
   // Action method
    public Object startRequest() {
      // Create continuation with a timeout
      Continuation con = new Continuation(40);
      // Set callback method
      con.continuationMethod='processResponse';      
      // Create callout request
      HttpRequest req = new HttpRequest();
      req.setMethod('GET');
      req.setEndpoint(LONG_RUNNING_SERVICE_URL);      
      // Add callout request to continuation
      this.requestLabel = con.addHttpRequest(req);      
      // Return the continuation
      return con;  
    }   
    // Callback method 
    public Object processResponse() {   
      // Get the response by using the unique label
      HttpResponse response = Continuation.getResponse(this.requestLabel);
      // Set the result variable that is displayed on the Visualforce page
      this.result = response.getBody();
      // Return null to re-render the original Visualforce page
      return null;
    }
}
  • You can make up to three asynchronous callouts in a single continuation. Add these callout requests to the same continuation by using the addHttpRequest method of the Continuation class. The callouts run in parallel for this continuation and suspend the Visualforce request. Only after all callouts are returned by the external service for does the Visualforce process resume.
  • 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.

Set environment variable for Apache ANT

To use ant in windows, you need to download it and should configure the windows environment variable. Download file here, Ant St...