0

I am writing a webservice & getting an error:

Variable does not exist: Cal Could use little help :)

@RestResource(urlMapping='/Sync/*')                            
 global class CreatrixSync{

 @HttpPost
  global static String doPost(String application_email, String application_mobile, String application_program, ) {

        External_System_Mapping__c[] LeadProgram = [SELECT Salesforce_Value__c from External_System_Mapping__c
                WHERE Mapping_Type__c = 'Program' 
                AND External_System_Value__c =:application_program ];

        if(LeadProgram.size() > 0 && LeadProgram[0].Salesforce_Value__c != 'No Program')
         {
           Calender__c[] Cal = [SELECT Id, Start_Date__c, End_Date__c, Program__c, Intake_Month__c, Intake_Year__c 
                FROM Calender__c
                WHERE Start_Date__c <= :System.Today() 
                AND End_Date__c >= :System.Today() and Program__c = :LeadProgram[0].Salesforce_Value__c]; 
        } 
        List<Lead> matchingLeadsListWIProgram;
        if (leadProgram != null && leadProgram.size() > 0) {
            matchingLeadsListWIProgram = [Select Id, Status from Lead where isConverted = false and ((Program__c =: LeadProgram[0].Salesforce_Value__c) and ( (Email != null AND Email = :application_email) OR (MobilePhone != null AND MobilePhone = :application_mobile) OR (Email != null AND Email=:spjat_email) OR (MobilePhone != null AND MobilePhone =:spjat_mobile))) ];
        }else{
            matchingLeadsListWIProgram = null;
        }

        if(matchingLeadsListWIProgram != null && matchingLeadsListWIProgram.size() > 0){ 
            for(Lead lead : matchingLeadsListWIProgram ){

                   //some code             
                if(Cal.size()>0){
                 lead.Intake__c = Cal[0].id;
                }      

         }

            update matchingLeadsListWIProgram;
        }

    }

}
1

This is a simple scoping issue. A variable only exists between { and }. You need to move the variable outside of the if statement. Here's a revision:

@RestResource(urlMapping='/Sync/*')                            
global class CreatrixSync{
    @HttpPost
    global static String doPost(String application_email, String application_mobile, String application_program) {

        Calender__c[] Cal = new Calendar__c[0];
        List<Lead> matchingLeadsListWIProgram = new Lead[0];

        External_System_Mapping__c[] LeadProgram = [Select Salesforce_Value__c from External_System_Mapping__c Where Mapping_Type__c = 'Program' and External_System_Value__c =:application_program ]; 
        if(LeadProgram.size() > 0 && LeadProgram[0].Salesforce_Value__c != 'No Program'){
          Cal = [SELECT Id, Start_Date__c, End_Date__c, Program__c, Intake_Month__c, Intake_Year__c FROM Calender__c WHERE Start_Date__c <= :System.Today() and End_Date__c >= :System.Today() and Program__c = :LeadProgram[0].Salesforce_Value__c]; 
        } 
        if (leadProgram.size() > 0) {
            matchingLeadsListWIProgram = [Select Id, Status from Lead where isConverted = false and ((Program__c =: LeadProgram[0].Salesforce_Value__c) and ( (Email != null AND Email = :application_email) OR (MobilePhone != null AND MobilePhone = :application_mobile) OR (Email != null AND Email=:spjat_email) OR (MobilePhone != null AND MobilePhone =:spjat_mobile))) ];
        }
        for(Lead lead : matchingLeadsListWIProgram ){
            if(Cal.size()>0){
                 lead.Intake__c = Cal[0].id;
            }      
        }
        update matchingLeadsListWIProgram;
    }
}

You'll notice also how I simplify the code by initializing the variables with an empty list.

0

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.