Thursday, February 16, 2017

How to Create Lightning Component: -

Before start coding we will see where we will write code for lightning components,

1. In developer Console --> Goto Developer Console --> select File --> New --> Lightning Component

2. we can write in ecllipse editor, but for compilation we have save that code in salesforce 

AIM : Create a simple lightning component to fetch account records and display on lightning App

Required lightning components: <Aura:Component>, <Aura:attribute>, <aura:handler>, UI Controller and apex class

Component Code:-

<aura:component controller="TechSoft.getActInfo">    
 <aura:attribute name="accountId" type="String" default=" " />
 <aura:attribute name="accountlst" type="account[]" />
 <aura:handler name="init" value="{!this}" action="{!c.getAcctInfo}" /> 
    <table>
        <thead>
           <tr><td>Account name</td><td>Account Phone</td></tr>
        </thead>
     <aura:iteration items="{!v.accountlst}" var="acct" >
        <tbody>
            <tr>               
                <td>
                    <div class="slds-truncate" title="Name">{!acct.Name}</div>
                </td>
                <td>
                    <div class="slds-truncate" title="Phone">{!acct.Phone}</div>
                </td>
            </tr>
        </tbody>    
  </aura:iteration>
    </table>    
</aura:component>

Controller.js:-  
    ({
 getAcctInfo : function(component, event, helper) {
  var action = component.get("c.getaccountData");
        action.setCallback(this,function(response){
            var state = response.getState();
            if(state="SUCCESS"){
                var items = response.getReturnValue();
                component.set("v.accountlst",items);
            }
        });
        $A.enqueueAction(action);
 }
})

Apex Class:-

  
public class getActInfo {    
 @AuraEnabled
    public static list<account> getaccountData(){
        return [select Name, phone,AccountNumber from Account];
    }
  }

Output:-

Creating, Updating, deleting, patching and putting case record using rest api :-

Following is the same code for all the above actions,


@RestResource(urlMapping='/Cases/*')
global with sharing class KnowledgeCaseManager {
    
    @HttpGet
    global static Case getCaseById() {
        RestRequest request = RestContext.request;
        // grab the caseId from the end of the URL
        String caseId = request.requestURI.substring(
          request.requestURI.lastIndexOf('/')+1);
        Case result =  [SELECT CaseNumber,Subject,Description,Status,Origin,Priority
                        FROM Case
                        WHERE Id = :caseId];
        return result;
    }

   @HttpPost
    global static ID createCase(String subject, String status,
        String origin, String priority) {
        Case thisCase = new Case(
            Subject=subject,
            Status=status,
            Origin=origin,
            Priority=priority);
        insert thisCase;
        return thisCase.Id;
    }  

    @HttpDelete
    global static void deleteCase() {
        RestRequest request = RestContext.request;
        String caseId = request.requestURI.substring(
            request.requestURI.lastIndexOf('/')+1);
        Case thisCase = [SELECT Id FROM Case WHERE Id = :caseId];
        delete thisCase;
    }     

    @HttpPut
    global static ID upsertCase(String Subject,String Description,String Status,
        String Origin,String Priority,String id) {
        Case thisCase = new Case(
                Id=id,
                Subject=Subject,
                Description=Description,
                Status=Status,
                Origin=Origin,
                Priority=Priority);
        // Match case by Id, if present.
        // Otherwise, create new case.
        upsert thisCase;
        // Return the case ID.
        return thisCase.Id;
    }

    @HttpPatch
    global static ID updateCaseFields() {
        RestRequest request = RestContext.request;
        String caseId = request.requestURI.substring(
            request.requestURI.lastIndexOf('/')+1);
        Case thisCase = [SELECT Id FROM Case WHERE Id = :caseId];
        // Deserialize the JSON string into name-value pairs
        Map<String, Object> params = (Map<String, Object>)JSON.deserializeUntyped(request.requestbody.tostring());
        // Iterate through each parameter field and value
        for(String fieldName : params.keySet()) {
            // Set the field and value on the Case sObject
            thisCase.put(fieldName, params.get(fieldName));
        }
        update thisCase;
        return thisCase.Id;
    }    

}

Retrieve Data with GET Method using Workbench:

In Workbench--Workbench-->>select GET -->>
Enter the URI /services/apexrest/Cases/<Record ID>, replacing <Record ID> with the ID of the record you created in the previous step.
Click Execute.

This invocation calls the method associated with the GET HTTP method, namely the getCaseById method.

To view the response returned, click Show Raw Response.

The returned response looks similar to this response. The response contains the fields that the method queried for the new case record. 

For posting data to record using workbench: -->> reset explorer -->> Post -->> 

replace the default URI with /services/apexrest/Cases/

For the request body, insert the following JSON string representation of the object to insert.

{
  "subject" : "Bigfoot Sighting!",
  "status" : "New",
  "origin" : "Phone",
  "priority" : "Low"

To access it from other systems like java and .net we should provide the entire url path like, https://yourInstance.salesforce.com/services/apexrest/<Cases>/

Here "https://yourInstance.salesforce.com/services/apexrest/" is the base endpoint for REST API and followed by URL mapping whatever we provided to rest class in our example it is "/Cases/" mentioned on top.

Annotation  Action  Details
@HttpGet    Read    Reads or retrieves records.
@HttpPost   Create  Creates records.
@HttpDelete Delete  Deletes records.
@HttpPut    Upsert  Typically used to update existing records or create records. 

@HttpPatch  Update  Typically used to update fields in existing records.