0

I'm getting a compile error at line 49 column 42, saying variable loginstring does not exist, but it seems to exist at line 38, what am I doing wrong?

    global class cstschedulable implements schedulable{


    global User cstUser {get;set;}
     public string Idstring;    
     public string cstUsername {get;set;}
     public string cstPassword {get;set;}
     public Id ClientAcctId {get;set;}
     public datetime cstLastUpdate{get;set;}
     public string loginstring {get;set;}
     public map<String, string> cstUserMap{get;set;}

     global void execute(SchedulableContext sc){
     returnAuth();
     }

    @future(callout=true)
     static void returnAuth(){    
          string xAuthToken;
          string st_Today;

        If (cstUser == null){
        User cstUser = [select Id, CST_User_Name__c,CST_Password__c, CSTlastupdate__c from User where CST_User_Name__c != null ];

        if(cstUser.CST_User_Name__c == null)
        {
        system.debug('cstUser null at UserData');
        }
        else{
        system.debug('cstUser not null at UserData');
        }
        if(cstUser.CST_User_Name__c != null){
        String loginstring = ('"'+cstUser.CST_User_Name__c+'" "password": "'+cstUser.CST_Password__c+'"}');
        String Idstring = string.valueof(cstUser.Id);
        }


        if(loginstring != null){

        HttpRequest req = new HttpRequest();
        HttpResponse res = new HttpResponse();
        Http http = new Http();

        req.setMethod('POST' ); // Method Type

        req.setEndpoint('web210.ntree.com/twig-cstc3/v1/login');//salestree url

        req.setHeader('Content-Type','application/json');
        req.setHeader('{ "username" : ', loginstring);


            res = http.send(req);

            if(res.getBody() != null){
                if(res.getBody() == 'Rejected: check_login returned false:'){
                    system.debug('Rejected: check_login returned false:');
                }

            }else{
                system.debug('SalesTree Failed to respond');
            }



  // Parse entire JSON response.
  JSONParser parser = JSON.createParser(res.getBody());
  while (parser.nextToken() != null) {
    if (parser.getCurrentToken() == JSONToken.START_ARRAY) {
        while (parser.nextToken() != null) {
            if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&
            (parser.getText() == 'authenticationToken')) {
                // Get the value.
                parser.nextToken();
                xAuthToken = parser.getText();

            if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&
            (parser.getText() == 'st:today')){
                parser.nextToken();
                parser.nextToken();
                st_Today = parser.getText();

             } 
             if (xAuthToken != null){
                system.debug('Auth Token received');
             }
             else
             {
             system.debug('Auth Token not received');
             }
             if(xAuthToken != null){
                User UpdateUser = new User();
                UpdateUser.Id = Id.valueof(Idstring);

                UpdateUser.X_Auth_token__c = xAuthToken;
                UpdateUser.CSTlastupdate__c = Datetime.now();
                Update UpdateUser;


             }   

            }           
           }        
          }
         }
        }   
        else{
       system.debug('loginstring null');
       }                
       }
       }
}
1
  • 1
    You'll find that people will be more willing to spend time helping you with these kinds of problems if you tell them which lines of code you're having a problem with (hint: knowing the line number isn't helpful since we can't tell the number when viewing it here, so mark you code sections).
    – crmprogdev
    Aug 25, 2015 at 20:08

1 Answer 1

2

Do not assume that an error reported at a line number implies that all the preceding lines of code are error free. The Apex compiler is not as optimised as some compilers in how it reports errors.

Indenting code to reflect the block structure will held you find problems like this: the loginstring local variable in the returnAuth method is in its own block (and so not referencable from the code below) and as returnAuth is a static method the loginstring class field is not referencable either.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .