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
Nicole LaperouseNicole Laperouse 

Need to remove carriage return from string

I have a JavaScript button that merely copies and pastes field values from one record type to another. My problem is that anytime I have a text box with a carraige return (user has hit the Return key), I get an "unterminated string constant" error. How do I remove these characters so that my button will work? I've used differet variations of the following syntax: 
URL = URL.replace(/\r?\n|\r/g, “ “);
Best Answer chosen by Nicole Laperouse
Shyam BhundiaShyam Bhundia
got it!

I made a mistake when getting the record type id...

try this:

{!REQUIRESCRIPT("/soap/ajax/31.0/connection.js")} 
//query for the record type, replace Case_Record_One with your case record type DeveloperName 
var rt = sforce.connection.query("Select Id From RecordType Where SobjectType = 'Case' and DeveloperName = 'Product_Request_Type_7'"); 
var records = rt.getArray("records");
var recordtypeID = records[0].Id;


var theNewCase = new sforce.SObject("Case"); 



//change the case record type 
theNewCase.RecordTypeId = recordtypeID; 
theNewCase.subject = "{!Case.Subject}"; 
theNewCase.description = "{!Case.Description}"; 
theNewCase.Target_Application__c = "{!JSINHTMLENCODE(Case.Target_Application__c)}" 
theNewCase.Description_of_New_Product__c ="  {!JSINHTMLENCODE(Case.Description_of_New_Product__c)}" 
//add your other fields here 
//.. 

//update the record
result = sforce.connection.create([theNewCase]);
if (result[0].getBoolean("success")){ 
 // Refresh window if successful 
 window.location.reload(); 
} 
else{ 
	//otherwise show the error message 
 alert("There is an error: "+result); 
}


All Answers

Shyam BhundiaShyam Bhundia
try  
URL = URL.replace(/[\n\r]/g, '');


Javascript replace() replaces  only the first occurance. The way around is to use regex, like above.
Nicole LaperouseNicole Laperouse
Thanks, but I get the same "Unterminated String Constant" error using that syntax.
Shyam BhundiaShyam Bhundia
hmmm..i just tried the above and it worked for me.  Can you post your code please.

cheers
Nicole LaperouseNicole Laperouse
I'm attempting to grab field values, create a new case record type, and insert the field values into the new case.
Here's the whole code for the Execute Javascript button:

var mplVals = '{!Case.X3rd_Party_Approval_Needed__c}';
var mplSFID = '00N60000002K2OS';
var mplVals = mplVals.split(';');
var params3 = '';
var i = 0;
for (i; i < mplVals.length; i++) {
    params3 = params3 + mplSFID + '=' + mplVals[i] + '&';
}

var mplVals = '{!Case.Market_s__c}';
var mplSFID = '00N60000002K2PQ';
var mplVals = mplVals.split(';');
var params = '';
var n = 0;
for (n; n < mplVals.length; n++) {
    params = params + mplSFID + '=' + mplVals[n] + '&';
}
var URL = '/500/e?' + params3 + params + 'RecordType=012600000009bMi&CF00N60000002uC2w={!Case.Id}&CF00N60000002uC2w_lkid={!Case.Id}&00N60000002KH9c={!Case.Description_of_New_Product__c}&CF00N60000002MN56={!Case.Application_EngineerId__c}&CF00N60000002MN56_lkid={!Case.Application_EngineerId__c}&00N60000002K2PV={!Case.Target_Application__c}&00N60000002K2Ow={!Case.Performance_Required__c}&00N60000002MNAV={!Case.Unit_Length_Quantity__c}&CF00N60000002K2PB={!Case.Requesting_CustomerId__c}&CF00N60000002K2PB_lkid={!Case.Requesting_CustomerId__c}&CF00N60000002IiZK={!Case.OpportunityId__c}&CF00N60000002IiZK_lkid={!Case.OpportunityId__c}&00N60000002K2P6={!Case.Date_Needed_for_Sale__c}&00N60000002KGtV={!Case.X3rd_Party_Approval_Needed_question__c}&00N60000002K2ON={!Case.X3rd_party_Other__c}&00N60000002MN51={!Case.Estimated_Sales__c}&00N60000002Kbyc={!Case.Anticipated_Margin_prt2__c}';
URL = URL.replace(/[\n\r]/gm, “ “);

location.replace(URL);
Shyam BhundiaShyam Bhundia
thanks.. which of the variables holds the value from the text area?
Nicole LaperouseNicole Laperouse
The following are multi-line text fields: 
{!Case.Description_of_New_Product__c}
{!Case.Target_Application__c}
{!Case.Performance_Required__c}
Shyam BhundiaShyam Bhundia
OK....so it seems like you just want to change the record type of the case?  Do you intend to keep the original case?

If not then you can simply change the case record type by:
{!REQUIRESCRIPT("/soap/ajax/25.0/connection.js")}
//query for the record type, replace Case_Record_One with your case record type DeveloperName
var rt = sforce.connection.query("Select Id From RecordType  Where SobjectType = 'Case' and DeveloperName = 'Case_Record_One'");

var theCase = new sforce.SObject("Case");
theCase.id = "{!Case.Id}";

//change the case record type
theCase.RecordTypeId = rt.id;

//update the record
result = sforce.connection.update([theCase]);


if (result[0].getBoolean("success")){
   // Refresh window if successful
    window.location.reload();
}
else{
   //otherwise show the error message
   alert("There is an error: "+result);
}


Nicole LaperouseNicole Laperouse
I need to keep the original case AND create a new case with the previous case's fields.
Shyam BhundiaShyam Bhundia
ok...this should do it...
{!REQUIRESCRIPT("/soap/ajax/25.0/connection.js")}
//query for the record type, replace Case_Record_One with your case record type DeveloperName
var rt = sforce.connection.query("Select Id From RecordType  Where SobjectType = 'Case' and DeveloperName = 'Case_Record_One'");



var theNewCase = new sforce.SObject("Case");

//change the case record type
theNewCase.RecordTypeId = rt.id;
theNewCase.subject = "{!Case.Subject}";
theNewCase.description = "{!Case.Description}";
//add your other fields here
//..

//update the record
result = sforce.connection.create([theNewCase]);

if (result[0].getBoolean("success")){
   // Refresh window if successful
    window.location.reload();
}
else{
	//otherwise show the error message
   alert("There is an error: "+result);
}
Where I've put "add your other fields here",  just carry on the mapping.

A question..why not automate this in a trigger?  So when a case is created it is automatically cloned?


Nicole LaperouseNicole Laperouse

I'm not sure if I entered your code correctly. But this is what I used to test it: 
var rt = sforce.connection.query("Select Id From RecordType Where SobjectType = 'Case' and DeveloperName = '{!Case.RecordType}'");

var theNewCase = new sforce.SObject("Case");

//change the case record type
theNewCase.RecordTypeId = rt.id;
theNewCase.Target_Application = "{!Case.Target_Application__c}";
theNewCase.Description_of_New_Product = "{!Case.Description_of_New_Product__c}";
//add your other fields here


//update the record
result = sforce.connection.create([theNewCase]);

if (result[0].getBoolean("success")){
// Refresh window if successful
window.location.reload();
}
else{
//otherwise show the error message
alert("There is an error: "+result);
}

---- 
I still get the Unterminated String error. And to answer your question from above, I am not automating this to "clone" because not all cases will need to be converted to a differnt case type.
 

Thank you again for your help so far!

Shyam BhundiaShyam Bhundia
Are you wanting to keep the record type the same?  Because if you don't, you should be querying for the new record type on the first line...not the existing one.
Shyam BhundiaShyam Bhundia
also Target_Application needs to be Target_Application__c and Description_of_New_Product needs to be Description_of_New_Product__c as they are custom fields
Nicole LaperouseNicole Laperouse
I changed the case type to the string value and the ID value; and changed the custom fields: but i'm still getting the unterminated string error.
var rt = sforce.connection.query("Select Id From RecordType Where SobjectType = 'Case' and DeveloperName = '012600000009bMi'");

var theNewCase = new sforce.SObject("Case");

//change the case record type
theNewCase.RecordTypeId = rt.id;
theNewCase.Target_Application__c = "{!Case.Target_Application__c}";
theNewCase.Description_of_New_Product__c = "{!Case.Description_of_New_Product__c}";
//add your other fields here


Shyam BhundiaShyam Bhundia
ok...very close with the record type query, but you should be using the name not the id of the record type.

Can you tell me what type of fields Target_Application__c and Target_Application__c are?  i will try to re-create this issue on my org.
Nicole LaperouseNicole Laperouse
Well, in this scenario I'm converting from one case type to a different case type, so I'm confused on how I should call the "new" case record type? 
Target application and Description of new product are multi-line text fields.
Shyam BhundiaShyam Bhundia
OK this should work....I just tried it and it works for me..

{!REQUIRESCRIPT("/soap/ajax/31.0/connection.js")}
//query for the record type, replace Case_Record_One with your case record type DeveloperName
var rt = sforce.connection.query("Select Id From RecordType  Where SobjectType = 'Case' and DeveloperName = 'Case_Record_One'");



var theNewCase = new sforce.SObject("Case");

//change the case record type
theNewCase.RecordTypeId = rt.id;
theNewCase.subject = "{!Case.Subject}";
theNewCase.description = "{!Case.Description}";
theNewCase.Target_Application__c = "{!JSINHTMLENCODE(Case.Target_Application__c)}"
//add your other fields here
//..

//update the record
result = sforce.connection.create([theNewCase]);

if (result[0].getBoolean("success")){
   // Refresh window if successful
    window.location.reload();
}
else{
	//otherwise show the error message
   alert("There is an error: "+result);
}
Note above that i've used JSINHTMLENCODE where the field is a text area.

The only thing you need to do is update the query to get the correct case record type.  To do this go to Setup > Case > Record Types this will list  out all your case record types.  Replace "Case_Record_One" in the code with the "Record Type Name" of the record type you want to use.
Nicole LaperouseNicole Laperouse
I applied those changes, and was able to click the button with no errors... but nothing happened. No new case was created. 
Shyam BhundiaShyam Bhundia
That wierd...can you post the all and the exact code you used..thanks 
(ps. I've made it my mission to help you get this working one way or another :) )
Nicole LaperouseNicole Laperouse
Absolutely, here's all of the code I used: 
{!REQUIRESCRIPT("/soap/ajax/31.0/connection.js")} 
//query for the record type, replace Case_Record_One with your case record type DeveloperName 
var rt = sforce.connection.query("Select Id From RecordType Where SobjectType = 'Case' and DeveloperName = 'Product_Request_Type_7'"); 



var theNewCase = new sforce.SObject("Case"); 

//change the case record type 
theNewCase.RecordTypeId = rt.id; 
theNewCase.subject = "{!Case.Subject}"; 
theNewCase.description = "{!Case.Description}"; 
theNewCase.Target_Application__c = "{!JSINHTMLENCODE(Case.Target_Application__c)}" 
theNewCase.Description_of_New_Product__c = "{!JSINHTMLENCODE(Case.Description_of_New_Product__c)}" 
//add your other fields here 
//.. 

//update the record 
result = sforce.connection.create([theNewCase]); 

if (result[0].getBoolean("success")){ 
 // Refresh window if successful 
 window.location.reload(); 
} 
else{ 
	//otherwise show the error message 
 alert("There is an error: "+result); 
}

And I thank you OVER AND OVER again for all of your help! :-) I'm obviously not a developer, but trying to make SF even better for our users. 
Nicole LaperouseNicole Laperouse
update: Looks like the button DID create a case, but of a different (I think the default) case record type. 
Shyam BhundiaShyam Bhundia
got it!

I made a mistake when getting the record type id...

try this:

{!REQUIRESCRIPT("/soap/ajax/31.0/connection.js")} 
//query for the record type, replace Case_Record_One with your case record type DeveloperName 
var rt = sforce.connection.query("Select Id From RecordType Where SobjectType = 'Case' and DeveloperName = 'Product_Request_Type_7'"); 
var records = rt.getArray("records");
var recordtypeID = records[0].Id;


var theNewCase = new sforce.SObject("Case"); 



//change the case record type 
theNewCase.RecordTypeId = recordtypeID; 
theNewCase.subject = "{!Case.Subject}"; 
theNewCase.description = "{!Case.Description}"; 
theNewCase.Target_Application__c = "{!JSINHTMLENCODE(Case.Target_Application__c)}" 
theNewCase.Description_of_New_Product__c ="  {!JSINHTMLENCODE(Case.Description_of_New_Product__c)}" 
//add your other fields here 
//.. 

//update the record
result = sforce.connection.create([theNewCase]);
if (result[0].getBoolean("success")){ 
 // Refresh window if successful 
 window.location.reload(); 
} 
else{ 
	//otherwise show the error message 
 alert("There is an error: "+result); 
}


This was selected as the best answer
Nicole LaperouseNicole Laperouse
The button works!!!! Only thing I noticed is that the jsinhtmlencode code adds an additional break line in between each line of the text box. 
Also, can I replace the window.location.reload() with the .replace([theNewCase])  so it opens up the new case that was just created? 
Lastly, is this java language? As much as i COMPLETELY appreciate your help; i'd like to understand what is actually happening within the code. :-) 
Shyam BhundiaShyam Bhundia
awesome!

ok to fix the extra lines breaks, use JSENCODE.  To redirect to the new record replace:
// Refresh window if successful
 window.location.reload();

with

//redirect to new record
  window.location.href = '/'+result[0].id;

So all this is done using javascript and ajax.  Below is a description of what the code does.

The below code gets the record type you want the new case to be.  Its better to query for it then hardcode the ID.  The reason for this is because in each environment (dev, test, production etc..) the record type id will be different.  This way all you need to worry about is making sure the record type is named the same in all environments
var rt = sforce.connection.query("Select Id From RecordType Where SobjectType = 'Case' and DeveloperName = 'Product_Request_Type_7'"); 
var records = rt.getArray("records");
var recordtypeID = records[0].Id;

Here we are creating a new case object, this will be used for your new case.  By using this method (compared to your original method) you are only dealing with field names and not ids.  Again ids will be different in each environment.  So this code will work in all your environments.  JSENCODE encodes text and merge field values for use in JavaScript.
var theNewCase = new sforce.SObject("Case"); 

//change the case record type 
theNewCase.RecordTypeId = recordtypeID; 
theNewCase.subject = "{!Case.Subject}"; 
theNewCase.description = "{!Case.Description}"; 
theNewCase.Target_Application__c = "{!JSENCODE(Case.Target_Application__c)}" 
theNewCase.Description_of_New_Product__c ="  {!JSENCODE(Case.Description_of_New_Product__c)}"

Now we have created the case, we insert it using the below code
//insert the record
result = sforce.connection.create([theNewCase]);

The final bit of code we check if the creation of the case was successful and if it was we redirect to the new case otherwise we show a pop up with the error.

if (result[0].getBoolean("success")){
  //redirect to new record
  window.location.href = '/'+result[0].id;  
} 
else{ 
   //otherwise show the error message 
  alert("There is an error: "+result); 
}

I hope the above helps makes things more sense. I've uploaded the code on git hub: https://github.com/shyambhundia/ajaxCaseClone/blob/master/cloneCaseButton.js


Nicole LaperouseNicole Laperouse
thank you so much again!! this works flawlessly. :-)