have to encode by HTML link with base64

It is my first post and hope it won't be already solved previously. I'm using a call center software and also using Salesforce lightning.

When a caller calls, I would like to check in my CRM if it is a customer or not. It was possible easily with the basic version of Salesforce but is not anymore because the link is coded with base64. Please, read this post for more explanation : https://tomsmalara.blogspot.com/2019/01/create-lightning-component-that.html

So, I have to create a HTML page for collecting the caller phone number and compose + encrypt the Salesforce link and open the link encrypted.

<!DOCTYPE html>
<html>
	<head>
		<title>Waiting a call ...</title>
			<style> 
				body {text-align: center;} 
			</style> 
	</head>
	<body>
		<form name="form1" onsubmit="event.preventDefault();return displayResult();">
			<label for="name">Phone number:</label>
			<input type="text" id="PhoneNumber" name="PhoneNumber" size="10">
			<div id="myEncoding"></div>
		</form>	
		<script>
			function b64EncodeUnicode(PhoneNumber) {
				// first we use encodeURIComponent to get percent-encoded UTF-8,
				// then we convert the percent encodings into raw bytes which
				// can be fed into btoa.
				
				var Mytxt = '{"componentDef":"forceSearch:search","attributes":{"term":"'+PhoneNumber+'","scopeMap":{"resultsCmp":"forceSearch:resultsTopResults","label":"Top Results","type":"TOP_RESULTS","cacheable":"Y","id":"TOP_RESULTS","labelPlural":"Top Results"},"context":{"disableSpellCorrection":false,"SEARCH_ACTIVITY":{"term":1234567890}}},"state":{}}';
				
				return btoa(encodeURIComponent(Mytxt).replace(/%([0-9A-F]{2})/g,
					function toSolidBytes(match, p1) {
						var MyResult = String.fromCharCode('0x' + p1);
						return MyResult;
				}));
			}

			function displayResult() {
				var result = b64EncodeUnicode(PhoneNumber);
				document.getElementById('myEncoding').innerHTML = result;
				return false;
				window.open("https://mycompany.lightning.force.com/one/one.app#" +result,,,true)
			}
		</script>
	</body>
</html>

Something is wrong and tried different things without result. I will really appreciate if someone can find what is wrong and explain it to me Thank you in advance

1 answer

  • answered 2019-10-16 07:48 dgnoyon

    OK, thank you for your answer. So, I changed my code and seems work better

    <!DOCTYPE html>
    <html>
    	<head>
    		<title>Waiting a call ...</title>
    			<style> 
    				body {text-align: center;} 
    			</style> 
    	</head>
    	<body>
    		<form>
    			<label for="phone">Phone number:</label>
    			<input type="text" id="PhoneNumber" name="PhoneNumber1" oninput=encode()>
    			<div id="myEncoding"></div>
    		</form>
    
            <script> 
            function encode() { 
    			var original = document.getElementById('PhoneNumber').value; 
                var stringToEncode = '{"componentDef":"forceSearch:search","attributes":{"term":"'+original+'","scopeMap":{"resultsCmp":"forceSearch:resultsTopResults","label":"Top Results","type":"TOP_RESULTS","cacheable":"Y","id":"TOP_RESULTS","labelPlural":"Top Results"},"context":{"disableSpellCorrection":false,"SEARCH_ACTIVITY":{"term":1234567890}}},"state":{}}';
                var encoded = window.btoa(stringToEncode); 
      
                var output = "Encoded String : " + encoded; 
                document.getElementById("myEncoding").innerHTML = 
                  "Original String: " + original + "<br>" + output; 
    			window.location.assign("https://mycompany.lightning.force.com/lightning/one/one.app?source=alohaHeader#")+searchcriteria;
            } 
        </script> 
    	</body>
    </html>

    Now, I have another question, is it possible to don't use the input box to receive the phone number. I would like the JScript is automatically executed in my webpage with the argument (Phone number) received ? At the moment, i did it in 2 steps but maybe I can do it in 1 ???