0

I have a trigger which is calling several non-void methods of apex classes.But I always get 'null' as a return value from these methods. Can someone explain why is this happening? I tried using the static key-word too, but it is showing the same result, 'null'. Following is the code snippet I am using to call class methods from trigger:

String[] operations=cd.CustBusinessPro__Operations__c.split(',');
          System.debug('splitted operations>>>>'+operations);
          for(String str:operations)
          { 
              if(str == ' PAN_Check')
             { system.debug('inside  pan');
               PAN_Check pan = new PAN_Check();
              result = Pan.PanCheck(app.CustBusinessPro__PAN__c,app.CustBusinessPro__Last_Name__c);
              system.debug('>>>>>>>>returned value'+result);

             }
             else if(str == 'De_Dup')
             {
                system.debug('inside de_dup');
                De_Dup d = new De_Dup();
                d.DeDup(app.CustBusinessPro__PAN__c,app.CustBusinessPro__Type_of_Loan__c);
             }
             else if(str == ' CIBIL Verification')
             {
              system.debug('inside cibil');
              CIBILVerification cibil=new CIBILVerification();
              cibil.CIBILCheck(app.CustBusinessPro__CIBIL_Score__c);
             }
             else if(str == ' Eligibility Verification')
             {
             system.debug('inside eligibilty');
             EligibilityVerification elig=new EligibilityVerification();
             elig.EligibilityCheck(app.CustBusinessPro__Salary__c,app.CustBusinessPro__Type_of_Loan__c,app.CustBusinessPro__Loan_Amount__c);
             }
             else if(str == ' EMI Verification')
             {
                system.debug('inside EMI');
                EMI_Calculation emi = new  EMI_Calculation();
                emi.EMICalculation(app.CustBusinessPro__Loan_Amount__c,app.CustBusinessPro__Loan_Tenure__c);
             }
             else if(str == ' NegativeAreaCheck')
             {
               system.debug('inside NAC');
               NegativeAreaCheck NAC = new NegativeAreaCheck();
               NAC.PincodeCheck(app.CustBusinessPro__Postal_Code__c);
             }
          }

        }
        else
        {
          System.debug('Criteria not satisfied');
        }

And this is the pan_check class:

public with sharing class PAN_Check{

public CustBusinessPro__Applicant__c applicant{get;set;}

String panCard{get;set;}
String LastName{get;set;}
String PanFormat{get;set;}

public PAN_Check(){

    applicant = new CustBusinessPro__Applicant__c();
    PanFormat = '[A-Z]{5}[0-9]{4}[A-Z]{1}'; //Definition of format of PAN consisting of 5 letters in the start followed by 4 numbers and again a letter. 


}



public  Boolean PanCheck(String panCardNo,String lname){

    Boolean flag;
    LastName = lname;
    System.debug('------'+lname);

    panCard = panCardNo;
    System.debug('----------------------' + panCard);

    //Checking the PAN number against the defined format. 
    pattern MyPattern = pattern.compile(PanFormat);
    matcher MyMatcher = MyPattern.matcher(panCardNo);

    //for splitting the characters of PAN and the last name of the applicant. 
    string[] chars = panCardNo.split('');
    string[] chars1 = lname.split('');

    //checking if the 5th character of the PAN is equal to the 1st character of last name. 
    if(MyMatcher.matches()){   

        if(chars[4] == chars1[0]){

            //if its equal PAN check passed or else failed. 
            System.debug(panCard + '---------------PAN Check passed!!'); 
            flag=true; 
        }

    }

    else{

        System.debug(panCard + '---------------PAN Check failed!!');
        flag=false;

    }

    insert new Account(Name='MyPANCheck');
    return flag;
}

}
8
  • is it even getting into the if statement of ` if(MyMatcher.matches()){ ` Does it show the debug statement within the if statements? – Prady Apr 11 '16 at 13:09
  • Yes the debug statements are generated – Jyoti Zanwar Apr 11 '16 at 13:14
  • 2
    Your declaration Boolean flag; leaves that value null if the field is not set in the if/else logic later in the method. So if you want the method to return false if it isn't set to a value in the if/else logic, initialise it where you declare it `Boolean flag = false;'. – Keith C Apr 11 '16 at 13:20
  • Yes as Prady pointed out, Its not getting inside the if/else part. But it was working fine when executed alone! – Jyoti Zanwar Apr 11 '16 at 13:21
  • 1
    What do you mean by executed alone? Here is what could be happening ` if(MyMatcher.matches()){ ` is returning true and it goes into the if statement and thenchecks if(chars[4] == chars1[0]){ . If this statement returns false then the flag is null. You are not setting the value of Flag if this statement is false – Prady Apr 11 '16 at 13:28
1

The issue looks to me with you not setting the value of flag in the else statement for if(chars[4] == chars1[0]){

If the code execution does not satisfy the this if statement, then it exits out without setting any value for flag. You need to set the value of Flag in the else statement.

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.