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');
}