Sunday, August 23, 2020

OAuth 2.0 Username-Password Flow In Salesforce

We can use Username-Password flow to allow the external system to authorize using connected app However this mechanism is not recommended since it passes credentials back and forth.

WHAT IS CONNECTED APP IN SALESFORCE?

For an external application that needs to authenticate with Salesforce we need to create a connected app so as to inform Salesforce about the new authentication entry point. Connected app uses standard OAuth 2.0 protocol to authenticate.

OAuth 2.0 Username-Password Flow In Salesforce

Here are the three major steps involved in Username-Password Flow In Salesforce.

1) Using connected app generate an access token by sending the request to Salesforce token end point.

2) After verifying the request, Salesforce grants an access token to the connected app.


3) The connected app can use the generated access token to access the protected data.

Below example explains how to generate access token if we are calling another Salesforce system to get token from Salesforce system.



public string getRequestToken() {
        string reqBody = 'grant_type=password&client_id=' + cKey + '&client_secret=' + cSecret + '&username=' + uName + '&password=' + passwd;
        Http h = new Http();
        HttpRequest req = new HttpRequest();
        req.setBody(reqBody);
        req.setMethod('POST');
        req.setEndpoint('https://login.salesforce.com/services/oauth2/token');
        HttpResponse hresp = h.send(req);
        responseWrapper wResp = (responseWrapper) JSON.deserialize(hresp.getBody(), responseWrapper.class);
        system.debug('Instance url' + wResp.instance_url);
        system.debug('session id' + wResp.access_token);
        return wResp.access_token;
    }



grant_type: The OAuth 2.0 grant type that the connected app requests. The value must be password for this flow.

client_id: The connected app’s consumer key.

client_secret The connected app’s consumer secret.

username: The username of the user that the connected app is imitating.

password: The password of the user that the connected app is imitating.


If the request is verified the response will be returned in below format.


{"id":"https://login.salesforce.com/id/00Dx0000000BV7z/005x00000012Q9P",
"issued_at":"SomeTimeStamp",
"instance_url":"https://yourInstance.salesforce.com/",
"signature":"",
"access_token":"",
"token_type":"Bearer"}

The data can now be access using access token from the above response.


Hope you find the post on "OAuth 2.0 Username-Password Flow In Salesforce" usefull.

Saturday, August 22, 2020

Apex REST Annotations In Salesforce

Below are the supported "Apex REST Annotations In Salesforce".

@RestResource(urlMapping='/yourUrl')
@HttpDelete
@HttpGet
@HttpPatch
@HttpPost
@HttpPut

@RestResource

The @RestResource annotation is used at the class level and enables us to expose an Apex class as a REST resource.

To use @RestResource annotation, an Apex class must be defined as global.

@HttpDelete

This deletes the specified resource.

@HttpGet

This returns the specified resource.

@HttpPatch

This updates the specified resource.

@HttpPost

This creates a new resource.

@HttpPut

This creates or updates the specified resource.

To use @HttpDelete, @HttpGet, @HttpPatch, @HttpPost, @HttpPut Apex method must be defined as global static.

Example:


@RestResource(urlMapping='/getAccountOnExternalIdtofetchsinglerecord/*')
   global with sharing class getAccounttoSingleRecord {
      @Httpget
      global static Account fetchAccount(){
        Account obj=new Account();
        RestRequest req = RestContext.request;
        RestResponse res = Restcontext.response;
        string accId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
        obj=[Select id , name from Account where id=:accId];
        return obj;
      }
   }

What is the difference between @HttpPatch & @HttpPut in Salesforce?

@HttpPatch

This updates the specified resource.
Replace the part of  resource with the request received.

@HttpPut

This creates or updates the specified resource.
Replace the entire resource with the request received.

Hope, you like the post "Apex REST Annotations In Salesforce".

Saturday, July 25, 2020

How to call child component method from parent component in lightning web component

In this blog post, we will learn "How to call child component method from parent component in lightning web component".

childDemoComp.html

<template>
    <div>
        <p>
        Hi, I am from child component HTML file.
       </p>
        <p>
        <b>Message from parent comp is </b> : {messagefromparent}
       </p>
       <p>
        <b>Name of parent comp is </b>: {parentcompname}
       </p>
    </div>
</template>      


childDemoComp.js

import { LightningElement, api,track } from 'lwc';

export default class childDemoComp extends LightningElement {

    @track messagefromparent;
    @track parentcompname;

    @api childMethod(){
        this.messagefromparent='ParentCompMsg';
        this.parentcompname='ParentCompName';
    }
}


parentDemoComp.html

<template>
    <div>
        Hi, I am from parent component HTML file.
        <lightning-button variant="brand" label="CallChildCompMethod" onclick={handleButtonClick}>
        </lightning-button>
        <c-child-demo-comp></c-child-demo-comp>
    </div>
</template>


parentDemoComp.js

//* eslint-disable no-alert */
import { LightningElement } from 'lwc';

export default class parentDemoComp extends LightningElement {
  handleButtonClick(){
    this.template.querySelector("c-child-demo-comp").childMethod();
  }
}


testApp.app


<aura:application>
<c:parentDemoComp></c:parentDemoComp>
</aura:application>    



How to call child component method from parent component in lightning web component

How to call child component method from parent component in lightning web component



Hope you find this post on "How to call child component method from parent component in lightning web component" useful.

Monday, January 13, 2020

How to pass data from parent component to child component in lightning web component

In this blog post, we will learn "How to pass data from parent component to child component in lightning web component".


childcompdemo.html



<template>
    <div>
        <p>
        Hi, I am from child component HTML file.
       </p>
        <p>
        <b>Message from parent comp is </b> : {messagefromparent}
       </p>
       <p>
        <b>Name of parent comp is </b>: {parentcompname}
       </p>
    </div>
</template>      


childcompdemo.js



import { LightningElement, api } from 'lwc';

export default class Childcompdemo extends LightningElement {

    @api messagefromparent;
    @api parentcompname;

}


parentcompdemo.html



<template>
    <div>
        Hi, I am from parent component HTML file.
        <c-childcompdemo messagefromparent="Hi this message is from parent comp" parentcompname="parentcompdemo" ></c-childcompdemo>
    </div>
</template>



parentcompdemo.js



//* eslint-disable no-alert */
import { LightningElement } from 'lwc';

export default class Parentcompdemo extends LightningElement {

}


lightningapp.app



<aura:application extends="force:slds">
<c:parentcompdemo></c:parentcompdemo>
</aura:application>       


How to pass data from parent component to child component in lightning web component