function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AstroCRMAstroCRM 

Unterminated String error on Text areas

Hi all,

I am trying to grab the contents from a custom text area field in account and write them (inherit them) to another record type with an scontrol. It all works fine except for when there are line breaksin the field and I haven't been able to work out a function that will let me encode the values of the carriage returns within the text area and write them on the new record.

Could anyone please provide some help on this?

Thanks and best regards.


DevAngelDevAngel
What are you using - java, .net, javascript?

Are you sure it's line breaks and not embedded quotes or double quotes?
SteveBowerSteveBower
I think Dave may have already solved your problem here:

http://community.salesforce.com/sforce/board/message?board.id=ajax_toolkit&message.id=121

Good luck, Steve.
AstroCRMAstroCRM
Angel :) I am using Javascript and I am sure it's line breaks.


Guys, I am going to try the solution. Thanks for your help, much appreciated. :)
AstroCRMAstroCRM
Hmmm I have tried it and unfortunately it's the same. If I merge any other field it works fine, but if I have a textarea and the values inside have line breaks, I get the error "Unterminated string constant...." Code 0

Here is part of the code. Any help would be highly appreciated:smileyindifferent:

{
'<textarea id="address" style="display: none">{!Account_Site_Address_Street}</textarea>';
}
}
}

function myCreate(objType, recType, currencyCode){
var obj= new Sforce.Dynabean(objType);
var objArray=new Array(1);
obj.set("Name","{!Account_Name} (child)"); //naming convention
obj.set("ParentId","{!Account_ID}"); //Associate the new record with the LE Account
obj.set("CurrencyISOCode",currencyCode); //inherited
obj.set("Country__c","{!Account_Country}"); //inherited
obj.set("Time_Zones__c","{!Account_Time_zones}"); //inherited
obj.set("Language__c","{!Account_Language}"); //inherited
obj.set("ShippingStreet", alert(document.getElementById("address").value); //inherited
obj.set("RecordTypeId", recType);
obj.set("Validation__c", "TRUE");
objArray[0]=obj;
//alert (obj.className);
var objSaveResult = sforceClient.Create(objArray)[0];
if (objSaveResult.success == true) {
opener.location.replace("/" + objSaveResult.id + "/e?retURL=%2F" + objSaveResult.id); //redirect to the newly created record
//window.close();
setTimeout("window.close();",2000);
}else{
//alert('KO!' + typeof objSaveResult);//object
//alert('KO' + objSaveResult[0].message);
document.getElementById("wait").innerHTML=objSaveResult.toString();
}
}
//-->
SteveBowerSteveBower
It's late and I haven't tried your code, but it looks right.  You're sure there are no single quotes in the data, correct?  :-)

Well, last but not least, there is always another alternative.

Since you've also got "{!id}" at your disposal, you can always issue a retrieve on the current ID and use the value from there instead of using the "{!Account_Site_Address_Street}".  E.G.  (assuming it's an account field.)

var tmp = sforceClient.retrieve('Account_Site_Address_Street__c','Account','{!id}'); //
obj.set("ShippingStreet",tmp.get("Account_Site_Address_Street__c"));

Steve.  (going to sleep)




AstroCRMAstroCRM
Steve, thanks for your great help. Unfortunately I can't even save the scontrol as it gives me a bad link error.

By the way guys, none of the solutions I have tried work. I think by now you have most likely realized I am not an expert on this so, if by any chance you might have more ideas, they'd be rather appreciated.

Thanks.
:smileysad:
DevAngelDevAngel
Bad link error is an incorrect merge field name.  Start there
AstroCRMAstroCRM
Hehe, thanks, I am trying my best to spot the error and yeah, I spotted that I was ignoring the actual name of the field so it now reads


var tmp = sforceClient.retrieve('Site_Address_Street__c','Account','{!id}'); //
obj.set("ShippingStreet", tmp.get("Site_Address_Street__c"));

Still get the bad link error:smileymad:
DevAngelDevAngel
Hey Astro, why is your textarea tag in the script block?

You are essentially declaring a string literal that contains a script tag. ???

You html should follow this structure.

Code:
<head>
<script>...</script>
<style>...</style>
</head>
<body>
...
<!-- Your textarea tag here! -->
</body>

 

AstroCRMAstroCRM
Hi again,

Yeah that is a bad error from my part. I am a real novice to this and I am finding it quite difficult. It's not that I haven't tried though, this must be my 7th hour today trying to get this to work with no avail. I was just wondering if I post the whole code you guys might have any ideas that could point me to the right direction. Any help at all
  
 Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head>
<script src="https://www.salesforce.com/services/lib/ajax/beta3.3/sforceclient.js" type="text/javascript"></script>


<script>

// Record type for Accounts var gTypeArray=new Array("012200000008Zj8", //Site - Conference Centre "012200000008Zzf", //Site - Hotel "012200000008ar3", //Site - Miscellaneus "012200000008Yyv"); //Legal Entity var gUserType=1;//index of the above array, see the selection list var gC_Account="Account"; var gC_Contact="Contact"; function initPage() { document.body.innerHTML='<div id="wait"><img src="/img/waiting_dots.gif" border="0" width=156 height=34></div>'; sforceClient.registerInitCallback(setupPage); sforceClient.init("{!API_Session_ID}", "{!API_Partner_Server_URL_70}"); } function setupPage() { //asynchroneous call => create the record after having received data var queryResult = sforceClient.query("Select CurrencyIsoCode from Account where Id = '{!Account_Id}'", layoutResults); } function layoutResults(queryResult) { if (queryResult.className == "Fault") { alert("There was an error: " + queryResult.toString()); } else { if (queryResult.size > 0) { // a little hack because no shortcut like !Account_currency myCreate(gC_Account,gTypeArray[gUserType],queryResult.records[0].get("CurrencyIsoCode")); } else { document.getElementById("wait").innerHTML="No records matched."; } } } function myCreate(objType, recType, currencyCode){ var obj= new Sforce.Dynabean(objType); var objArray=new Array(1); obj.set("Name","{!Account_Name} (Site)"); //naming convention obj.set("ParentId","{!Account_ID}"); //Associate the new record with the LE Account obj.set("CurrencyISOCode",currencyCode); //inherited obj.set("Country__c","{!Account_Country}"); //inherited obj.set("Time_Zones__c","{!Account_Time_zones}"); //inherited obj.set("Language__c","{!Account_Language}"); //inherited obj.set("ShippingAddress", THIS IS WHAT I CAN'T GET TO WORK); obj.set("RecordTypeId", recType); obj.set("Validation__c", "TRUE"); objArray[0]=obj; //alert (obj.className); var objSaveResult = sforceClient.Create(objArray)[0]; if (objSaveResult.success == true) { opener.location.replace("/" + objSaveResult.id + "/e—retURL=%2F" + objSaveResult.id); //redirect to the newly created record //window.close(); setTimeout("window.close();",2000); }else{ //alert('KO!' + typeof objSaveResult);//object //alert('KO' + objSaveResult[0].message); document.getElementById("wait").innerHTML=objSaveResult.toString(); } } //--> </script> </head> <body> <textarea id="address" style="display: none">{!Account_Site_Address_Street}</textarea> Please choose the record type below <form name="myform"> <select name="choosetype" onchange="gUserType=document.myform.choosetype.value;" size=4> <option value="0">Site - Conference Centre</option> <option value="1" selected>Site - Hotel</option> <option value="2">Site - Miscellaneus</option> <option value="3">Legal Entity</option> </select> </form> <input type=button onclick="initPage();" value="Create"> </body></html>

 

DevAngelDevAngel
Hey Astro,

I'll take a look at it tomorrow.  I'm sure we can get this resolved, after all, it's not brain surgery!

:smileywink:
DevAngelDevAngel
So, I took a look at your code and made a few changes.  The main problem is that there is no field on account called shippingaddress.  Although you can obtain the full address from a merge field, you can't set the full address through the api, you need to set each part of the address (street, city, state, postal code and country) separately.

The code below might need to be tweaked to work for you but should be closer.  Also, it's not a good idea to replace the innerHTML of the body tag.

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>

    <script src="https://www.salesforce.com/services/lib/ajax/beta3.3/sforceclient.js" type="text/javascript"></script>

    <script>

// Record type for Accounts
var gTypeArray=new Array("012200000008Zj8", //Site - Conference Centre
"012200000008Zzf", //Site - Hotel
"012200000008ar3", //Site - Miscellaneus
"012200000008Yyv"); //Legal Entity
var gUserType=1;//index of the above array, see the selection list
var gC_Account="Account";
var gC_Contact="Contact";


function initPage() {
    //Hide the form div
    document.getElementById("myForm").style.display = "none";
    //Move this div tag to the html and set only it's innerHTML, not the body innerHTML
    //document.body.innerHTML='<div id="wait"><img src="/img/waiting_dots.gif" border="0" width=156 height=34></div>';
    document.getElementById("wait").innerHTML = "<img src='/img/waiting_dots.gif' border='0' width='156' height='34'>";
    //Show the wait div
    document.getElementById("wait").style.display = "block";
    
    //Call the init and register in a onTimeout so that the page will draw
    window.setTimeout("phase2Init()", 100);
}
function phase2Init() {
    sforceClient.registerInitCallback(setupPage);
    sforceClient.init("{!API_Session_ID}", "{!API_Partner_Server_URL_70}");
}

function setupPage() { //asynchroneous call => create the record after having received data
    var queryResult = sforceClient.query("Select CurrencyIsoCode from Account where Id = '{!Account_ID}'", layoutResults);
}

function layoutResults(queryResult) {
    if (queryResult.className == "Fault") {
        alert("There was an error: " + queryResult.toString());
    } else {
        if (queryResult.size > 0) {
            // a little hack because no shortcut like !Account_currency
            myCreate(gC_Account,gTypeArray[gUserType],queryResult.records[0].get("CurrencyIsoCode"));
        } else {
            document.getElementById("wait").innerHTML="No records matched.<br><input type='button' value='Try Again' onclick='javascript: document.getElementById(\"wait\").style.display = \"none\"; document.getElementById(\"myForm\").style.display=\"block\";'";
        }
    }
}

function myCreate(objType, recType, currencyCode){
    var obj= new Sforce.Dynabean(objType);
    var objArray=new Array(1);
    obj.set("Name","{!Account_Name} (Site)"); //naming convention
    obj.set("ParentId","{!Account_ID}"); //Associate the new record with the LE Account
    obj.set("CurrencyISOCode",currencyCode); //inherited
    obj.set("Country__c", "{!Account_ShippingCountry}"); //inherited
    obj.set("Time_Zones__c","{!Account_Time_zones}"); //inherited
    obj.set("Language__c","{!Account_Language}"); //inherited
    //obj.set("ShippingAddress", THIS IS WHAT I CAN'T GET TO WORK);
    obj.set("ShippingStreet", document.getElementById("shippingStreet").value);
    obj.set("ShippingCity", "{!Account_ShippingCity}");
    obj.set("ShippingState", "{!Account_ShippingState}");
    obj.set("ShippingPostalCode", "{!Account_ShippingPostalCode}");
    obj.set("ShippingCountry", "{!Account_ShippingCountry}");
    obj.set("RecordTypeId", recType);
    obj.set("Validation__c", "TRUE");
    objArray[0]=obj;
    //alert (obj.className);
    var objSaveResult = sforceClient.Create(objArray)[0];
    if (objSaveResult.success == true) {
        opener.location.replace("/" + objSaveResult.id + "/e—retURL=%2F" + objSaveResult.id); //redirect to the newly created record
        //window.close();
        setTimeout("window.close();",2000);
    }else{
        //alert('KO!' + typeof objSaveResult);//object
        //alert('KO' + objSaveResult[0].message);
        document.getElementById("wait").innerHTML=objSaveResult.toString();
    }
}
//-->
    </script>

</head>
<body>
    <textarea id="shippingStreet" style="display: none">{!Account_ShippingStreet}</textarea>
    <div id="content">
        <div id="wait" style="display: none"></div>
        <div id="myForm" style="display: block">
            Please choose the record type below<br />
            <select name="choosetype" onchange="gUserType=document.myform.choosetype.value;" size="4">
                <option value="0">Site - Conference Centre</option>
                <option value="1" selected>Site - Hotel</option>
                <option value="2">Site - Miscellaneus</option>
                <option value="3">Legal Entity</option>
            </select><br />
            <input type="button" onclick="initPage();" value="Create">
        </div>
    </div>
</body>
</html>

 

AstroCRMAstroCRM
Dev Angel, you are too kind to take this and try to help me. Thank you.


I did try this solution before

Code:
 obj.set("ShippingStreet", document.getElementById("shippingStreet").value);

 But unfortunately it gives me the error "Object Required"


Following your comment on ShippingStreet:

Code:
obj.set("ShippingStreet"...
Is correct because if I merge any single lined field (e.g {!Account_ShippingCountry}) into it, it works fine.


It is only when I have:

Code:
obj.set("ShippingStreet", {!Account_Site_Address_Street);

 And it is multilined, that I get the unterminated string constant error.


Again, I do thank you for your efforts and help. If you have any other idea it'd be very welcome indeed. You are a top guy:)




DevAngelDevAngel
Ok, so put the street into a hidden text area element and reference it through the DOM.  The reason why you are getting object required is because you replace all the html in your body in your initPage function, thereby removing the textarea from the html.

Cheers
AstroCRMAstroCRM
DevAngel you are really an Angel. I finally got it to work by removing the hide all body tag at the function init page. I thank you immensely, you really have no idea how much.:smileyvery-happy::smileyvery-happy:
tggagnetggagne

I realize we're five years later, but I'm having the same problem inside a custom button/link with some javascript.

 

Essential, if my lead's Street is

 

123 Main St

 

Then {!Lead.Street} expands perfectly inside JavaScript.  However if the Street is

 

123 Main St.

Suite 100

 

Then I get a page error clicking on the link, complaining of an unterminated string constant.

 

The JavaScript looks something like: