Thursday, August 5, 2021

How to Use React Application Into Salesforce

 We can include  React Application into Salesforce with the help of using Lightning Container in VF/Aura/Lwc component 

The lightning container component hosts your custom JavaScript application in an iframe within your lightning pages ie you can embed any js application in salesforce lightning component using lightning container

      Features of Lightning Container

  • Lightning Container allows you to use an application developed in 3rd party framework like:- Angular, React Js etc. in Lightning Component.
  • Lightning Component can handle messages as well as errors from your embedded app in it's controller.
  • In the same way as above, the embedded javascript application can also handle messages as well as errors, from your lightning component
  • You can also call apex from your custom lightning:container application
Step For :React Application Into SalesForce
  • Create React Js Application
  • Build React Js Application
  • include below code into manifest.json
  •   "landing-pages": [ {
                    "path": "index.html",
                    "apex-controller": "LightningContainerDemo"
} ]
  •  Create Apex Class into Saelesforce with Name LightningContainerDemo
  •  Create a Zip file of Build Folder 
  •  Create Static Resource into SalesForce ,give same Name of build folder
  •  Create Lighting Aura component  with lightning:container
  •  Give Src value of the lightning:container= static resource 
src="{! $Resource.lightningcontainerDemo + '/index.html' }"
  •  Create Lighting App /Community and Use Lighting Aura component
  •  Open that App /Community






Working with JSON in Apex

We can serialize and deserialize JSON to strongly-typed Apex classes and also to generic collections like Map<String, Object> and List<Object> in Apex.

Different way to use JSON in Apex.

  •  Typed serialization and deserialization with JSON.serialize()/JSON.deserialize(). 
  •  Untyped deserialization with JSON.deserializeUntyped().

This JSON includes two levels of nested objects, as well as a list of primitive values.

Typed Serialization with JSON.serialize() and JSON.deserialize()

The methods JSON.serialize() and JSON.deserialize() convert between JSON and typed Apex values.

 When using JSON.deserialize(), you must specify the type of value you expect the JSON to yield, and Apex will attempt to deserialize to that type.

 JSON.serialize() accepts both Apex collections and objects, in any combination that’s convertible to legal JSON.

JSON as an example:

{

"errors": [ "Request failed validation rules" ],

"message": "Please provide data and retry",

"details": {

"record": "001000000000091",

"record_type": "Account"

}

}

Apex class:

public class Example {

public List<String> errors;

public String message;

public class ExampleDetail {

Id record;

String record_type;

}

public ExampleDetail details;

}

To parse JSON into an Example instance, execute

Example ex = (Example)JSON.deserialize(jsonString, Example.class);

Alternately, to convert an Example instance into JSON, execute

String jsonString = JSON.serialize(ex);


Untyped Deserialization with JSON.deserializeUntyped()


Using the JSON.deserializeUntyped() method yields an Object value, because Apex doesn’t know at compile time what type of value the JSON will produce

Map<String, Object> result = (Map<String, Object>)JSON.deserializeUntyped(jsonString);