0
{!REQUIRESCRIPT("/soap/ajax/33.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/33.0/apex.js")} 

var rcn = "{!Opportunity.Description__c}"; 
var amt = "{!Opportunity.Amount}"; 
if( rcn!= '' && amt!= '' && amt!= '0.00') 
{ 
window.open("{!URLFOR( $Action.Opportunity.Submit, Opportunity.Id )}"); 
} 
else if (amt=='' || amt =='0.00') 
{ 
alert("Enter Amount"); 
} 
else if (rcn=='') 
{ 
alert("Enter Description"); 
} 
else 
{ 
alert("Enter Amount and Description"); 
}

Here my target is that I should be able to submit an opportunity for approval only if Amount and Description are entered. Else it should throw errors separately is any of those fields is null/zero. This code is alowing me to submit even though Amount is null and also Description is null.

3
  • 1
    Did you try to check what is the value of Amount and description when they are left blank? I reckon, it should be null, instead of blank space. Commented May 3, 2017 at 6:21
  • Yeah on the record, they are blank. In this code, I also tried adding the condition as rcn==null . But it still didn't work.
    – user42273
    Commented May 3, 2017 at 6:23
  • it is working for me can u specify the exact problem
    – Avijit Das
    Commented May 3, 2017 at 6:28

1 Answer 1

2

Lots of minor issues. Let's fix it up:

{!REQUIRESCRIPT("/soap/ajax/33.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/33.0/apex.js")} 

Not required here. Remove these lines, as they're just causing additional loading time.

var rcn = "{!JSENCODE(Opportunity.Description__c)}"; 

Make sure you encode text values.

var amt = {!BLANKVALUE(Opportunity.Amount, 0)}; 

Use a real number, it's easier.

if(rcn && amt) {

Nulls, undefined values, empty strings and 0 is a "falsy" value, anything else is "truthy" value.

    window.open("{!JSENCODE(URLFOR( $Action.Opportunity.Submit, Opportunity.Id ))}"); 

Again, just a good habit, even though this is most likely safe.

} else if(!amt && !rcn) {
    alert("Enter Amount and Description"); 
} else if(!amt) {
    alert("Enter Amount"); 
} else {
    alert("Enter Description"); 
}

You have to check the most specific situations first. In the original code, !amt && !rcn could never be reached, because the less specific conditions would be met first.


Final version:

var rcn = "{!JSENCODE(Opportunity.Description__c)}"; 
var amt = {!BLANKVALUE(Opportunity.Amount, 0)}; 
if(rcn && amt) {
    window.open("{!JSENCODE(URLFOR( $Action.Opportunity.Submit, Opportunity.Id ))}"); 
} else if(!amt && !rcn) {
    alert("Enter Amount and Description"); 
} else if(!amt) {
    alert("Enter Amount"); 
} else {
    alert("Enter Description"); 
}
3
  • Thanks for mentioning the load performance point here. I have only one issue here. Field Opportunity.Description__c is a picklist field. Picklist fields are only supported in certain functions. My Description field is a picklist field actually. I tried to put JSENCODE(TEXT(Opportunity.Description__c)) but it seems syntax error. Please let me know how to use picklist in JSEN function.
    – user42273
    Commented May 4, 2017 at 8:00
  • @user42273 Is it a multi-select field or single-select? It looks like it should be okay. Realistically, since all you're interested in is if there's a value or not, maybe you could use an expression like var hasDescription = {!NOT(ISPICKVAL(Opportunity.Description__c,"")}; instead.
    – sfdcfox
    Commented May 4, 2017 at 13:06
  • It is just a single picklist having three values. It gave me an error when using JSEN. I was not able to use ISPICKVAL in javascript earlier. Let me try if this works.
    – user42273
    Commented May 5, 2017 at 13:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .