Salesforce JavaScript unterminated string constant Error - javascript

I created a button in Salesforce using JavaScript which converts an enquiry (custom object) to a registration (Contact). The button works for myself and any test user I log in as on the same profiles and permissions, however one of my users is report an error when they use the button.
The error is as follows:
'unterminated string constant'
The Code I'm using in the button is as follows:
{!REQUIRESCRIPT('/soap/ajax/27.0/connection.js')}
alert('Migration in progress, this page will refresh once it is complete. Please click ok, Please DO NOT press this button again');
if ('{!Enquiry__c.Surname__c}' != ''){
if ('{!Enquiry__c.Client_if_already_on_the_system__c}' == ''){
var Reg = new sforce.SObject('Contact');
Reg.FirstName = '{!Enquiry__c.First_Name__c}';
Reg.LastName = '{!Enquiry__c.Surname__c}';
Reg.Gender__c = '{!Enquiry__c.Gender__c}';
Reg.MailingStreet = '{!Enquiry__c.Address__c}';
Reg.MailingPostalCode = '{!Enquiry__c.Post_Code__c}';
Reg.MailingCity = '{!Enquiry__c.City_Town__c}';
Reg.MailingState = '{!Enquiry__c.County__c}';
Reg.HomePhone = '{!Enquiry__c.Home_Number__c}';
Reg.MobilePhone = '{!Enquiry__c.Mobile_Number__c}';
Reg.Email = '{!Enquiry__c.E_mail__c}';
Reg.Lastest_Enquiry__c = '{!Enquiry__c.Id}';
result = sforce.connection.create([Reg]);
if (result[0].success == 'true'){
alert('A new Registration with the name - ' + Reg.FirstName + ' ' + Reg.LastName + ' was successfully created, Please DO NOT press this button again as duplication\'s will be created, Your client will be automatically connected to this enquiry');
javascript: document.location.reload(true);
}
}else{
alert('Cannot Migrate to Registration as there is already a Client connected to this Enquiry');
}
}else{
alert('Cannot Migrate to Registration as Surname is blank!');
}
The user has told me they are using IE as a browser, I've tested the button on Chrome, Edge and the most up to date IE. Anyone have any ideas of whats wrong?

what is the type for Reg.FirstName and LastName? Are you using textarea type for these two fields?
Because if user enter any of the name with a carriage return \r or \n alert will fail with the same error and it will be a random based on the input.

Related

Chrome app storage settings itself works, but in another script returned value is undefined

I've built simple options form with 3 radio buttons.
Seems that my options.js works correctly - i.e. if I change selection of the button, close options dialog and reopen it, the selection that I made previously is restored to the correct choice.
But if I try to pick up setting from another script, using the same statement that was used in options.js, it returns "undefined" string as if there would be no variable value stored in chrome storage.
Code in options.js that works ok:
chrome.storage.local.get(['card'], function(result) {
var card = null;
if(result.card) {
card = result.card;
} else {
card = 'douay';
chrome.storage.local.set({'card': 'douay'});
}
document.getElementById("demo").innerHTML = 'Value currently is ' + card;
document.getElementById(card).checked = true;});
Code in another script (returns "Value currently is undefined"):
chrome.storage.sync.get(['card'], function(result) {
window.alert('Value currently is ' + result.card);
});
And if I set settings and read them in the same script, they work as expected - message with "Value Got! Value is This text shall be stored"
chrome.storage.sync.set({'value': "This text shall be stored"}, function()
{window.alert("Value Saved!")});
chrome.storage.sync.get('value', function(data){
window.alert("Value Got! Value is " + data.value)});

casperjs test register form

I have a form inside my page for user that want to register inside the site. After registration I insert user inside database create an activation key and send an email, until user doesn't click the link inside the email with the activation key he can't login inside the site.
With CasperJS I would like to test this functionality, the fillForm is ok but how can I test the activation key?
I have thought to create an input hidden with the activation key (only if is in TEST mode not i production!) and retrieve this value with getFieldValue function.
Is this the right way to do it or there is a better mode to do?
this is my casperjs test to retrieve activation key after registration (I create an input hidden with the activation code):
view = 'users/registered';
casper
.start(webroot + view)
.then(function() {
__utils__.log("Retrieving data from input activation-key");
activationKey = __utils__.getFieldValue('activation-key');
}).then(function() {
__utils__.log("Activating account with the key " + activationKey);
}).then(function(){
this.evaluate(function() {
__utils__.log("Activating account with the key " + activationKey);
window.location = webroot + 'users/activate/' + activationKey;
});
}).then(function(){
this.echo(this.getCurrentUrl());
});
casper.run(function() {
this.echo('test registeration successful!').exit();
});
casper.viewport(page.width, page.height);
casper.test.done();
I managed to do what i wanted with registration, it could help you : CasperJS- Register on a site and validate the mail sent on Gmail -for both slimer and phantom-
And before i did some scraping with an activation code too, for manual activation (pure JS, no jQuery here, i didn't want to inject JQuery on gmail DOM environment) :
this.waitForSelector("div.msg",function(){
this.test.assertSelectorHasText("a","Activation message");
//var code declared in the main scope
code = this.evaluate(function(){
var strongs = document.getElementsByTagName('strong')
,i
,l = strongs.length
;
for (i = 0; i < l; ++i) {
if(strongs[i].textContent === "activation code:"){
//get back the code in DOM context -> split to get back only what I want
return (strongs[i].parentNode.textContent.split(' ')[2]);
}
}
});
this.echo("code : " + code,"INFO");
});

User is Typing Notification

I am developing my project on mvc4 , i have an issue with user is typing status notification on my chat module of the project.The problem with this j script is that it is showing the status to same user who is typing but i want that other user to see that i am typing in my text box
var textarea = $('#textarea');
var typingStatus = $('#typing_on');
var lastTypedTime = new Date(0); // it's 01/01/1970
var typingDelayMillis = 5000; // how long user can "think about his spelling" before we show "No one is typing -blank space." message
function refreshTypingStatus() {
if (!textarea.is(':focus') || textarea.val() == '' || new Date().getTime() - lastTypedTime.getTime() > typingDelayMillis) {
typingStatus.html('No one is typing -blank space.');
} else {
typingStatus.html('User is typing...');
}
}
function updateLastTypedTime() {
lastTypedTime = new Date();
}
setInterval(refreshTypingStatus, 100);
textarea.keypress(updateLastTypedTime);
textarea.blur(refreshTypingStatus);
<label>
<textarea name="textarea" id="textarea" cols="45" rows="5"></textarea>
</label>
<div id="typing_on"></div>
If you want users to see a different users typing status, you have to send the current users status to the server via AJAX and retrieve it from the server to display.
So basically, assuming two people are chatting, each client (browser) must:
Get the current users typing status and send to the server
Retrieve the other users typing status from the server and display it.
If you are unsure of how to use AJAX, you should look up jQuery's ajax documentation
Your client side code would have to change like so:
function refreshTypingStatus() {
if (!textarea.is(':focus') || textarea.val() == '' || new Date().getTime() - lastTypedTime.getTime() > typingDelayMillis) {
typingStatus.html('No one is typing -blank space.');
} else {
typingStatus.html('User is typing...');
}
//AJAX call to send typingStatus.html to server
//AJAX call to retrieve other users typingStatus (could be combined into one call)
}
Writing the code necessary is beyond an SO answer as it involves both server side and client side code, but start with this tutorial, and if you have more questions, just ask a new question specific to what you need.

Mailto: subject as a variable

** EDIT: Thanks, Shivan Raptor! I kinda forgot about "encodeURIcomponent()"! **
For some reason, I'm making a Javascript bookmarklet that quickly emails one of my two emails. (I'll just show the indented JS to make it easier, as I can convert it.) It uses a couple variables for the subject and body, which are inputted by prompts.
var address = confirm("School email?");
var sub = prompt("What is the subject?");
var bod = prompt("What would you like the message to read?");
if(address == true) {
window.location.assign("mailto:areiter#hightechhigh.org?Subject=sub&body=bod");
alert("Message sent to 'areiter#hightechhigh.org'.");
}
else {
window.location.assign("mailto:burningphantom13#gmail.com?Subject=sub&body=bod");
alert("Message sent to 'areiter#hightechhigh.org'.");
}
The only problem is that the variables "sub" and "bod" are what the subject and body appear as, respectively. So the code opens up Outlook, and has an email ready to be sent with the subject "sub" and "bod" in the body. Is there a way for the subject and body of a Javascript code to be the values of variables? Remember, it's going to be a bookmark, so there can't be any HTML.
You can replace :
window.location.assign("mailto:areiter#hightechhigh.org?Subject=sub&body=bod");
with:
window.location.assign("mailto:areiter#hightechhigh.org?Subject=" + encodeURIComponent(sub) + "&body=" + encodeURIComponent(bod));
so that the variable sub and bod will be dynamic according to user input. encodeURIComponent encodes the parameters, so if the user inputs special characters, the codes will not crash.
Change code as following,
var address = confirm("School email?");
var sub = prompt("What is the subject?");
var bod = prompt("What would you like the message to read?");
if(address == true) {
window.location.assign("mailto:areiter#hightechhigh.org?Subject=" + sub + "&body=" + bod);
alert("Message sent to 'areiter#hightechhigh.org'.");
}
else {
window.location.assign("mailto:burningphantom13#gmail.com?Subject=" + sub + "&body=" + bod);
alert("Message sent to 'areiter#hightechhigh.org'.");
}
Hope this will work :)

jQuery Error - Generating undefined

So I have a list of users registered on my site in 1 column, in the 2nd is their email address with a checkbox next to it. On this page a user can check the box (or multiples) and click a submit button. Once they do that it will generate a list of the emails semicolon separated.
My issue is after they hit submit the lists generates, but the first email address has "undefined" written right next to it.. so instead of saying "domain1#test.com; domain2#test.com" it reads "undefindeddomain1#test.com; domain2#test.com".
Here is my jQuery:
jQuery(document).ready(function() {
jQuery('#memberSubmit').click(function() {
var emailList;
jQuery('.email-members input:checked').each(function() {
var $this = jQuery(this);
emailList += $this.next('a').html() + "; ";
});
jQuery('.email-message').hide();
jQuery('.email-members').hide();
jQuery('.email-checks').hide();
jQuery('#memberSubmit').hide();
jQuery('.email-results a').attr('href', "mailto: " + emailList).fadeIn(2000);
jQuery('.email-results .email-list p').html(emailList).fadeIn(2000);
jQuery('.email-results h2').fadeIn(2000);
jQuery('.email-results p').fadeIn(2000);
jQuery('.email-list h2').fadeIn(2000);
//console.info('Emails: ' + emailList);
});
});
I think my error is on the line: emailList += $this.next('a').html() + "; ";
But I am not sure... any ideas?
Thanks!
Initialize the emailList the variable first, that means it doesn't start at undefined when you perform your first go around. Coincidently, when you're calling += for the first time, it's actually converting undefined to a string, thus meaning your string always starting with that.
var emailList = "";
Try replacing emailList's declaration with this code:
var emailList = "";
That's because emailList starts out as undefined if you don't initialize it. Therefore undefined + "this is a test" would turn out as undefinedthis is a test.

Categories