You need to sign in to do that
Don't have an account?
Nicole 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, “ “);
URL = URL.replace(/\r?\n|\r/g, “ “);
I made a mistake when getting the record type id...
try this:
All Answers
Javascript replace() replaces only the first occurance. The way around is to use regex, like above.
cheers
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);
{!Case.Description_of_New_Product__c}
{!Case.Target_Application__c}
{!Case.Performance_Required__c}
If not then you can simply change the case record type by:
A question..why not automate this in a trigger? So when a case is created it is automatically cloned?
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!
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.
Target application and Description of new product are multi-line text fields.
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.
(ps. I've made it my mission to help you get this working one way or another :) )
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.
I made a mistake when getting the record type id...
try this:
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. :-)
ok to fix the extra lines breaks, use JSENCODE. To redirect to the new record replace:
with
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
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.
Now we have created the case, we insert it using the below code
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.
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