function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
DesaiDesai 

Custom Exception not working

Hi,

We are invoking a apex class through js on click of a button. In that apex class we are invoking the webservice. 
Before invoking the webservice we are checking for some conditions and need to display some custom exceptions but it is not wokring.
js code behind the button
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/29.0/apex.js")}
var result = sforce.apex.execute("HPGenerateLicnese","HPLic",{LicId:"{!HP_License__c.HP_License_Id__c}"});
window.location.reload();

custom exception class:
public class HPCusotmException extends Exception {}

Custom class to invoke the webservice:
global class HPGenerateLicnese 
{
   WebService static String HPLic(Id LicId)
    {
        String keyString;
        Integer version;
        Integer seats;
        String expireDate;
        String subExpireDate;
        Boolean cleanserEnabled;
        String returnvalue;
       
        HP_License__c  L = [SELECT Id, HP_Registration_Number__c,HP_Version__c,HP_Qutantity__c, HP_Temporary_License_Expires__c, HP_Expires__c, HP_License_Type__c FROM HP_License__c WHERE Id = :LicId] ;
        
        String sLicType = L.HP_License_Type__c ;
        
        //covert the quantity to integer
        Integer intSeats = L.HP_Qutantity__c.intValue(); 
        
        //Converting version to integer
        String tes = L.HP_Version__c ;
        Integer sld = tes.length();
        String sVer ;
        Integer myInt ;
        if(sld >= 0)
        {
            sVer = tes.substring(0, 1);
            myInt = Integer.valueof(sVer);
        }
       
       //Show error message if service expiry is null and licnese type = Permanent    
        DateTime d;String monthName;String sday;String sYesr;String sDt; String sError;
        if((sLicType == 'Permanent')&&(L.HP_Expires__c == null))
        {           
             try
             { 
                sError = 'Y';
                if(L.HP_Expires__c == null) throw new HPCusotmException('Hi'); 
             }
             catch(HPCusotmExceptione)
             {
               throw new HPCusotmException('Hi');   
             }

             // returnvalue = 'Please enter the Service Expire Date'; 
                
        }
         
        // to get the temporary license expired in a specified format ex. May 21, 2107
        DateTime td;String monthNm;String sdy;String sYr;String sDat;
        if((L.HP_Temporary_License_Expires__c == null)&&(sLicType == 'Temporary'))
        {
           sError = 'Y';
           returnvalue = 'Please enter the Temporary License Exp Date';
        }
        
        //method inputs
        keyString = L.HP_Registration_Number__c ;
        version = myInt ;
        seats = intSeats ;
       
        cleanserEnabled = false;
        
        if((sLicType == 'Permanent')&&(L.HP_Expires__c != null))
        {
            expireDate = 'Never';
             // to get the service expired in a specified format ex. May 21, 2107
             d = L.HP_Expires__c ;                    
            monthName= d.format('MMM');
            sday = d.format('dd');
            sYesr = d.format('YYYY');
            sDt = monthName+' '+sday+','+' '+sYesr ;
             subExpireDate = sDt ;
            //invoke the webservice method
            
        }

can some one let me where am i going wrong ?

Regards,
Pallavi
UC InnovationUC Innovation
Javascript won't be able to catch the exception from the Apex class.  Try returning a string instead of throwing a custom exception.
Rohit K SethiRohit K Sethi
Hi ,

This is possible. But its done from some different way.
First you need to know that whenever we request httpCallout it's called asynchronous.
And you are write code as
    "var result = sforce.apex.execute("HPGenerateLicnese","HPLic",{LicId:"{!HP_License__c.HP_License_Id__c}"});".
This is wrong you need work on callback to handle response.


Here is a syntax :
  
var callback = {
        onSuccess:function(result){
            alert('sucess');
        },
        onFailure:function(error){
            alert('Fail'+error);   
        }
    };
    sforce.apex.execute("HPGenerateLicnese","HPLic",{LicId:"{!HP_License__c.HP_License_Id__c}"},callback);


Thanks.