You need to sign in to do that
Don't have an account?
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{
Hi RP2,
May be you are doing small mistake. i'e variable declaration with spaces at
I will send you code, please copy it again
This resultes in a comile error.
Error: Compile Error: Variable does not exist: lstPayerContact at line 233 column 20
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?
ContactID currently but should.