4

I have a requirement to display the following message when an opportunity has is closed won in the wrong situation.

Please make sure that the Renewal index field is filled in for all products, and if applicable, Gross Tonnage field is filled in for the P&I cover products.

However when I save this into the error message field the & is transformed into & in the metadata. When the message is displayed to the user the & is not converted back into &. So I get this instead.

Please make sure that the Renewal index field is filled in for all products, and if applicable, Gross Tonnage field is filled in for the P&I cover products.

I have tried amending the validation rule metadata xml to use <![CDATA[&]]> as suggested here: https://stackoverflow.com/a/1328563 but Salesforce strips the tags and replaces & with &amp; when I edit the object metadata directly in eclipse. When I use the web editor and write <![CDATA[&]]> it converts all the special characters to &lt;![CDATA[&amp;]]&gt;.

What can I do to get salesforce to display the '&' character correctly to the user?

I just realised that I have a custom button performing the change of stage to Closed Won and this is trapping the validation error message and displaying it to the user via a javascript alert. It is there that the ampersand is being transformed.

Here is the custom button code

{!requireScript("/soap/ajax/13.0/connection.js")}
var oppName = '{!URLENCODE( LEFT(Opportunity.Name , 243))}';
var safeOppName = '{!URLENCODE( 'Checklist - ')}' + oppName;

if ('{!Opportunity.RecordTypeName__c}' == 'P_I' && {!Opportunity.Checklist_Count__c} == 0) {
window.location = '/a0j/e?CF00NM0000000aKxP='+oppName + '&&Name=' + safeOppName + '&&CF00NM0000000aKxP_lkid={!Opportunity.Id}' + '&&retURL={!Opportunity.Id}';
}
else {
var opp = new sforce.SObject("Opportunity");

opp.Id = "{!Opportunity.Id}";
opp.StageName = "Closed Won";

var result = sforce.connection.update([opp]);

if (result[0].success=='false')
{ alert(result[0].errors.message); }
else { 
location.reload(true); 
}
}
  • I can't reproduce this error. When I put an & in the validation error message, it displays properly. Can you flesh this out more? Are you displaying on page or on field (didn't matter for me) or anything else that may be relevant? – Benj Sep 7 '12 at 10:51
  • Thanks Benj, the message is being displayed in a popup message... ah the problem is in the custom button that we have doing the close won action. It must be interfering with the contents of the message when it displays it in a javascript alert. – Born2BeMild Sep 7 '12 at 10:53
5

The problem was in the custom button that was automating the opportunity closure. It was displaying the validation error message in a javascript alert without decoding the &.

As a very quick fix I have changed the code to replace any occurrence of &amp; with &. Other solutions may exist but this seemed the quickest fix.

The custom button code is now.

{!requireScript("/soap/ajax/13.0/connection.js")}
var oppName =  '{!URLENCODE( LEFT(Opportunity.Name , 243))}';
var safeOppName =  '{!URLENCODE( 'Checklist - ')}' + oppName;

if ('{!Opportunity.RecordTypeName__c}' == 'P_I' && {!Opportunity.Checklist_Count__c} == 0) {
window.location = '/a0j/e?CF00NM0000000aKxP='+oppName + '&&Name=' + safeOppName + '&&CF00NM0000000aKxP_lkid={!Opportunity.Id}'  + '&&retURL={!Opportunity.Id}';
}
else {
var opp = new sforce.SObject("Opportunity");

opp.Id = "{!Opportunity.Id}";
opp.StageName = "Closed Won";

var result = sforce.connection.update([opp]);

if (result[0].success=='false')
{ var msg =  result[0].errors.message;
  var newmsg = msg.replace('&amp;','&');
alert( newmsg ); }
else { 
  location.reload(true); 
}
}
0

Why are you putting the validation rule stuff in the button? Just create a regular validation rule and let the error message display as it normally would. You're recreating standard functionality, which is generally a poor practice.

  • Thanks David, the original coding wasn't my work so I can't explain why it was set up like that. – Born2BeMild Sep 11 '12 at 19:23
  • .. Also the validation rule is not implemented within the button itself it is just that when the code in the button attempts to save the opportunity having changed its Stage the validation rule is violated and the error message is returned to the user. – Born2BeMild Sep 11 '12 at 19:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.