Tuesday, 26 April 2022

How to Encode a string to Base64 format in Apex

 Hi ,

Here we are going to learn how to encode a string to Base64 format in Apex 

Eg:

String urlparam = 'Salesforce Techbook';

String urlString = EncodingUtil.base64Encode(Blob.valueof(urlparam));

System.debug(urlString);

Output: U2FsZXNmb3JjZSBUZWNoYm9vaw==


Reference: 

https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_restful_encodingUtil.htm

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


Sunday, 24 April 2022

Generate JSON with Map in apex

 Hi ,

Here we are going to learn how to generate JSON with the help of Map in apex.

Let's take the following JSON and see how to generate the same with the help of Map in apex.

JSON:

{

    "lastname":"SF",

    "firstname":"Techbook",

    "account":{

        "name":"Salesforce Techbook",

        "address":{

            "shippingstreet":"test_street",

            "shippingcity":"test_city"

        }

    },

    "phone":"12454565",

    "mailingcity":"test_cont_City"    

}

Let's try to generate the above JSON with Map:

//Preparation of  outer JSON object

Map<String,Object> jsonGenMap = new Map<String,Object>();

jsonGenMap.put('lastname','SF');

jsonGenMap.put('firstname','Techbook');

jsonGenMap.put('phone','12454565');

jsonGenMap.put('mailingcity','test_cont_City');

//Preparation of "account" JSON object

Map<String,Object> jsonActMap = new Map<String,Object>();

jsonActMap.put('name','Salesforce Techbook');

Map<String,Object> jsonActAddressMap = new Map<String,Object>();

jsonActAddressMap.put('shippingstreet','test_street');

jsonActAddressMap.put('shippingcity','test_city');

jsonActMap.put('address',jsonActAddressMap);

jsonGenMap.put('account',jsonActMap);

//Generation of JSON from the above with the help of "serialize" method under "JSON" class.

string jsonstring = JSON.serialize(jsonGenMap);

System.debug(jsonstring);


References:

https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_System_Json.htm


Saturday, 2 April 2022

JSON parsing with Map Sample for getting list of values

 Hi,

Here we are going to learn how to get the values from an array that has plain values instead of a list of JSON objects.

Let's take the example below.

Here we have "langaugeskills" array in the following JSON String with the values "English,Hindi, Telugu". To capture those values we have to follow the below approach.


String jsonStr = '{"LastName": "Balu","langaugeskills": ["English","Hindi","Telugu"]}';

Map<String,Object> jsonMap = (Map<String,Object>)JSON.deserializeUntyped(jsonStr);

System.debug('JSON :::'+jsonMap);

List<Object> langSkillsList = (List<Object>)jsonMap.get('langaugeskills');

System.debug('---->'+langSkillsList);

for(Object langStr:langSkillsList){

    System.debug((String)langStr);

}


output:



Reference:

https://salesforce-walker.blogspot.com/2021/04/json-parsing-using-map-sample.html

What’s the difference between Einstein Article Recommendations and Suggested Articles?

How Does Einstein Article Recommendations Work? Einstein Article Recommendations helps support agents resolve customer cases efficiently by ...