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
RP2RP2 

Why can I not nest this string?

This works fine...

 

    public void searchAndCreateContact(){
        
        String payerEmail = '';
        if(username != ''){
            payerEmail = username;
            List<User>lstPayerContact = [select ContactId, name, AccountId FROM User where Username = :username];
        }else{
            payerEmail = mapParameters.get('payer_email');
            List<Contact>lstPayerContact = [select id,name,AccountId from Contact where Email = :payerEmail ];
            }
        List<Contact>lstPayerContact = [select id,name,AccountId from Contact where Email = :payerEmail ];
        // why can the above line not be nested in the else statement above.
        // Error: Compile Error: Variable does not exist: lstPayerContact at line 231 column 20
        if(lstPayerContact  != null && lstPayerContact.size()>0){
            cont = lstPayerContact[0];
            List<Account>lstAccount = [select id,name from Account where id=:cont.AccountId];
            if(lstAccount != null && lstAccount.size()>0)
                accnt = lstAccount[0];
            
        }else{

 

Why can does this not compile?

 

    public void searchAndCreateContact(){
        
        String payerEmail = '';
        if(username != ''){
            payerEmail = username;
            List<User>lstPayerContact = [select ContactId, name, AccountId FROM User where Username = :username];
        }else{
            payerEmail = mapParameters.get('payer_email');
            List<Contact>lstPayerContact = [select id,name,AccountId from Contact where Email = :payerEmail ];
            }

        if(lstPayerContact  != null && lstPayerContact.size()>0){
            cont = lstPayerContact[0];
            List<Account>lstAccount = [select id,name from Account where id=:cont.AccountId];
            if(lstAccount != null && lstAccount.size()>0)
                accnt = lstAccount[0];
            
        }else{

 

Thanks

 

    public void searchAndCreateContact(){                String payerEmail = '';        if(username != ''){            payerEmail = username;            List<User>lstPayerContact = [select ContactId, name, AccountId FROM User where Username = :username];        }else{            payerEmail = mapParameters.get('payer_email');            List<Contact>lstPayerContact = [select id,name,AccountId from Contact where Email = :payerEmail ];            }        List<Contact>lstPayerContact = [select id,name,AccountId from Contact where Email = :payerEmail ];        // why can the above line not be nested in the else statement above.        // Error: Compile Error: Variable does not exist: lstPayerContact at line 231 column 20        if(lstPayerContact  != null && lstPayerContact.size()>0){            cont = lstPayerContact[0];            List<Account>lstAccount = [select id,name from Account where id=:cont.AccountId];            if(lstAccount != null && lstAccount.size()>0)                accnt = lstAccount[0];                    }else{

vbsvbs
RP2 - Read up on variable scope. Depending on the declaration of a variable its scope is limited. In the code snippet above you have declared and initialised the variable lstPayerContact in the if and then in the else block. Hence its scope will be limited to the if/else part of the block. To make it visible to the code below just declare the variable at the top and then initialise as required with the SOQL in the condition. This should fix your error.
AKS018AKS018

Hi RP2,

 

May be you are doing small mistake. i'e variable declaration with spaces at 

List<User> lstPayerContact   and
List<Contact> lstPayerContact

 

I will send you code, please copy it again

 

 public void searchAndCreateContact(){
        
        String payerEmail = '';
        if(username != ''){
            payerEmail = username;
            List<User> lstPayerContact = [select ContactId, name, AccountId FROM User where Username = :username];
        }else{
            payerEmail = mapParameters.get('payer_email');
            List<Contact> lstPayerContact = [select id,name,AccountId from Contact where Email = :payerEmail ];
            }

        if(lstPayerContact  != null && lstPayerContact.size()>0){
            cont = lstPayerContact[0];
            List<Account> lstAccount = [select id,name from Account where id=:cont.AccountId];
            if(lstAccount != null && lstAccount.size()>0)
                accnt = lstAccount[0];
            
        }else{
 public void searchAndCreateContact(){
        
        String payerEmail = '';
        if(username != ''){
            payerEmail = username;
            List<User> lstPayerContact = [select ContactId, name, AccountId FROM User where Username = :username];
        }else{
            payerEmail = mapParameters.get('payer_email');
            List<Contact> lstPayerContact = [select id,name,AccountId from Contact where Email = :payerEmail ];
            }
        List<Contact> lstPayerContact = [select id,name,AccountId from Contact where Email = :payerEmail ];
        // why can the above line not be nested in the else statement above.
        // Error: Compile Error: Variable does not exist: lstPayerContact at line 231 column 20
        if(lstPayerContact  != null && lstPayerContact.size()>0){
            cont = lstPayerContact[0];
            List<Account>lstAccount = [select id,name from Account where id=:cont.AccountId];
            if(lstAccount != null && lstAccount.size()>0)
                accnt = lstAccount[0];
            
        }else{
RP2RP2

This resultes in a comile error.

 

 Error: Compile Error: Variable does not exist: lstPayerContact at line 233 column 20

vbsvbs

RP2 - The issue is to do with variable declaration with smaller scope in the if/else blocks. After having re-read the problem it looks like you are trying to replicate polymorphic behavior from the "lstPayerContact " variable that will hold User/Contact data depending on the condition. This will not work. You either need to declare 2 different list variables or declare a generic SObject List variable and manipulate it accordingly by type casting it later as you use it.

If we look at the code - The only value you use from the queries is AccountId. Can you not declare an Id variable and use this later in the code assuming you actually have no use for 'lstPayerContact ' as a whole?

RP2RP2
We actually need the ContactID and the AccountID. We may not be using the
ContactID currently but should.