Showing posts with label Javascript. Show all posts
Showing posts with label Javascript. Show all posts

Sunday, January 1, 2023

Convert XML string to blob using Javascript

 This post explain how to convet to XML string to Blob and you can sent blob to servers.


   convertXMLStringToblob = (xmlstring) => {
      // Convert xml string to base64data
     let xmlval = new DOMParser().parseFromString(xmlstring, 'application/xml');
     let base64Data = window.btoa((new XMLSerializer()).serializeToString(xmlval));
   
     // Convert base64data to blob
     const byteCharacters = window.atob(base64Data);
     const byteNumbers = new Array(byteCharacters.length);
     for (let i = 0; i < byteCharacters.length; i++) {
       byteNumbers[i] = byteCharacters.charCodeAt(i);
     }
     const byteArray = new Uint8Array(byteNumbers);
     return new Blob([byteArray], {type:'application/xml'});
   }

Sample file



Monday, February 10, 2020

Difference between ( for… in ) and ( for… of ) statements in JavaScript

For… in

for… in loops over enumerable property names of an object, In each iteration, the name of the property is stored in the variable.

Ex:

var obj = {x: 5, y: 'abc', z: true};
for (var prop in obj) {
    console.log(prop + " = " + obj[prop]);
}

Output

For… of

The for...of loop is used to loop over any type of data that is iterable(arrays, maps, sets, and others).

Ex:

const digits = [0, 1, 2, 3, 4];
for (const digit of digits) {
  console.log(digit);
}

Output

Wednesday, July 31, 2019

Difference between var, let, const in Javascript

From ES6 we have three ways to declare a variables
var a = 48;
const b = 'Hello Shaik';
let c = true;
The type of declaration depends on the scope. The scope is the fundamental concept in all programming languages that defines the visibility of a variable.

Var

 The var keyword, which defines a variable globally, or locally to an entire function regardless of block scope.

Ex:
function testVar() {
  var x = 1;
  if (true) {
    var x = 2;  // same variable
    console.log(x);  // 2
  }
  console.log(x);  // 2
}
Output

let

let allows you to declare variables that are limited in scope to the block, statement, or expression in which they are used. 
if you run the below the code it will throw an error

if (true) {
let name = 'Hi Shaik';
}
alert(name); //generates an error

In this above case, the name variable is accessible only in the scope of the if statement because it was declared as let.
Ex:
function letTest() {
  let x = 1;
  if (true) {
    let x = 2;  // different variable
    console.log(x);  // 2
  }
  console.log(x);  // 1
}
Output


const

const variables have the same scope as variables declared using let. The difference is that const variables are immutable - they are not allowed to be reassigned.

Ex:
const a = 'Hello Shaik';
a = 'Bye';
console.log(a);
Output

Monday, September 24, 2018

How to Redirect to URL Based on Theme and Site Name in Salesforce

If you are Redirecting URL From Button and That Button Used In Different Places Like Community, Partner Portal, Lightning Experience.
In Lightning Experience, URL is Different Compare to the Classic Theme URL.
So First You Need to check the Current User Theme Based On Theme You Need to Redirect to The URL.
Check Bellow themes are available in Salesforce.

Theme1 —> Obsolete Salesforce theme
Theme2 —> Salesforce Classic 2005 user interface theme
Theme3 —> Salesforce Classic 2010 user interface theme
Theme4d —> Modern “Lightning Experience” Salesforce theme
Theme4t —> Salesforce1 mobile Salesforce theme
PortalDefault —> Salesforce Customer Portal theme
Webstore —> Salesforce AppExchange theme

To Check the Current theme in Visualforce page use below Global Variables

{!$User.UIThemeDisplayed}
OR
In Apex UserInfo.getUiThemeDisplayed().
To Check Site Name in Visualforce Page Use Global Variable
{!Site.Name}
In Portal or Communities URL is Different like
https://Baseurl/<portalName>/apex/apexpage/
Demo:
Check Below VisualForce Page to understand easily
<apex:page standardController="Account" showHeader="false" sidebar="false">
    <script>   
     if(typeof sforce !== 'undefined' && sforce !== null && '{!$User.UIThemeDisplayed}' === 'Theme4d') {
         window.open('/apex/ApexTestPage?id={!Account.Id}');
         sforce.one.back(true);
     }
     else if('{!$User.UIThemeDisplayed}' !== 'Theme4d' && ('{!$Site.Name}' == undefined || '{!$Site.Name}'  == '' || '{!$Site.Name}'  == null)) {
        window.open('/apex/ApexTestPage?id={!Account.Id}','_blank');
        window.top.location = '/{!Account.Id}';
     }
     else if('{!$Site.Name}' != null || ('{!$User.UIThemeDisplayed}' === 'Theme4d' && '{!$Site.Name}' != null)) {
         var siteName = '{!$Site.Name}';
         siteName = siteName.toLowerCase(); 
         window.open('/'+siteName+'/apex/ApexTestPage?id={!Account.Id}','_blank');
         window.top.location = '/'+siteName+'/{!Account.Id}';
     }
    </script>
</apex:page>

for Testing, Create Detail Page Button on Account(I Used Standard Controller as Account), Based on Your Preference create button and add the Page.
After that Add Button in Layouts based on your Preferences Like(Community, Portal, Lightning Experience).

Note: You can change the Script in Visualforce Page based on Your Requirement and you Can Extend the Functionality Further.

References:
Site Reference In Visualforce Page
sforce Methods
Let us know if you have any queries.

Happy Learning!!!