1

I have a custom javascript button which has to check for a value in custom field on an object record and has to perform the functionality. In this case, if the field value is not null, it has to redirect to a page (Both in console view and in normal view). It works well for the positive condition. The "Else" condition is not working. Instead of throwing error to the user, it is taking them to Salesfore home page in both console view and standard view. Please see the code and let me know if there is any issue in this.

{!REQUIRESCRIPT("/support/console/38.0/integration.js")} 
{!REQUIRESCRIPT("/soap/ajax/38.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/38.0/apex.js")} 


    var outboundFaxActivityId = '{!Outbound_Fax__c.Activity_ID_Developer_Use__c}'; 
    console.log(outboundFaxActivityId); 

    if(outboundFaxActivityId != null) { 
      if(typeof(srcUp) == 'function') //Checks that it is in console 
      { 
       var openSubtab = function(result){ 
       sforce.console.openSubtab(result.id,'/{!Outbound_Fax__c.Activity_ID_Developer_Use__c}', true, 'Fax', null);
       }; 
       sforce.console.getEnclosingPrimaryTabId(openSubtab); 

      } 
      else{ 
       window.parent.location.href=('/{!Outbound_Fax__c.Activity_ID_Developer_Use__c}') 
      } 
   } 
   else { 
     throw new Error('There is no Activity record associated with this Fax. Please contact your System Adinistrator'); 
   }

Updated Answer:

{!REQUIRESCRIPT("/support/console/38.0/integration.js")} 
{!REQUIRESCRIPT("/soap/ajax/38.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/38.0/apex.js")} 


    var outboundFaxActivityId = '{!Outbound_Fax__c.Activity_ID_Developer_Use__c}'; 
    console.log(outboundFaxActivityId); 

    if(outboundFaxActivityId.size()> 0) { 
      if(typeof(srcUp) == 'function') //Checks that it is in console 
      { 
       var openSubtab = function(result){ 
       sforce.console.openSubtab(result.id,'/{!Outbound_Fax__c.Activity_ID_Developer_Use__c}', true, 'Fax', null);
       }; 
       sforce.console.getEnclosingPrimaryTabId(openSubtab); 

      } 
      else{ 
       window.parent.location.href=('/{!Outbound_Fax__c.Activity_ID_Developer_Use__c}') 
      } 
   } 
   else { 
     alert('There is no Activity record associated with this Fax. Please contact your System Adinistrator'); 
   }
1

If you use javascript button, throw the error using alert like this:

alert ('There is no Activity record associated with this Fax. Please contact your System Administrator');

It should stay on the same page.

Note: throw new Error() works best in apex classes.

  • Also, I have changed outboundFaxActivityId != null to outboundFaxActivityId.size() <= 0. This made it to work along with alert statement. – SFDC_BigDog Feb 25 '17 at 2:06

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.