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
Tony Montana 6Tony Montana 6 

Hi , I have below block of code.

if(requestId != null) {
        
               List<GLOBAL_Request__c> requestObj = [select id, business_unit__c, recordtype.developername from GLOBAL_Request__c where id=:requestId AND recordtype.developername = 'Send_Survey' Limit 1];
            if (requestObj != null){        
            List<User> usrRec =[select id, NT_Login_Name__c from User where id=:UserInfo.getUserId() And NT_Login_Name__c != null Limit 1];
                    if(usrRec != null){
                    List<Contact> contactRec = [select id, contactRec.NT_Login_Name__c from contact where NT_Login_Name__c=:usrRec.NT_Login_Name__c ORDER by LastModifiedDate DESC Limit 1];         
                        if (contactRec != null){
                          List<Participant__c> participantRec = [select Id, Name,Survey_Taken_Date__c from Participant__c where Participant__c=:contactRec.Id AND Request__c=:requestObj.Id ORDER by CreatedDate DESC LIMIT 1];
                            if(participantRec != null && participantRec.Survey_Taken_Date__c == null){
                                ParticipantId = participantRec.Id;
                                contactId = contactRec.Id;
                                requestId = requestObj.Id;
                                Participant__c updateSurveyTakenDate = new Participant__c(id = ParticipantId, Survey_Taken_Date__c = System.now());            
                                       upsert updateSurveyTakenDate;  
                            } else if(participantRec != null && participantRec.Survey_Taken_Date__c != null){
                                Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, System.Label.LABS_SF_You_have_already_taken_this_survey));
                                return false;
                            } else {
                                Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, System.Label.LABS_SF_Participant_Not_Part_Of_This_Pool));                    
                                 return false;
                            }
                        }
                    }
            }
        }
  
And I am getting the following error.


1.Variable does not exist: NT_Login_Name__c
2.Variable does not exist: Id
3.Variable does not exist: Survey_Taken_Date__c
4.Variable does not exist: Id
5.Variable does not exist: Survey_Taken_Date__c

Will appreciate your help.

Thanks,
TM
      
Best Answer chosen by Tony Montana 6
Maharajan CMaharajan C
Hi Tony,

Instead of Null check use the IsEmpty function to check the list has data or not:

IsEmpty function will solve the List index out of bounds: 0 issue.
 
if(requestId != null) {

    List<GLOBAL_Request__c> requestObj = [select id, business_unit__c, recordtype.developername from GLOBAL_Request__c where id=:requestId AND recordtype.developername = 'Send_Survey' Limit 1];
	if (!requestObj.isEmpty()){        
	List<User> usrRec =[select id, NT_Login_Name__c from User where id=:UserInfo.getUserId() And NT_Login_Name__c != null Limit 1];
			if(!usrRec.isEmpty()){
			List<Contact> contactRec = [select id, contactRec.NT_Login_Name__c from contact where NT_Login_Name__c=:usrRec[0].NT_Login_Name__c ORDER by LastModifiedDate DESC Limit 1];         
				if (!contactRec.isEmpty()){
				  List<Participant__c> participantRec = [select Id, Name,Survey_Taken_Date__c from Participant__c where Participant__c=:contactRec[0].Id AND Request__c=:requestObj[0].Id ORDER by CreatedDate DESC LIMIT 1];
					if(!participantRec.isEmpty() && participantRec[0].Survey_Taken_Date__c == null){
						ParticipantId = participantRec[0].Id;
						contactId = contactRec[0].Id;
						requestId = requestObj[0].Id;
						Participant__c updateSurveyTakenDate = new Participant__c(id = ParticipantId, Survey_Taken_Date__c = System.now());            
							   upsert updateSurveyTakenDate;  
					} else if(!participantRec.isEmpty() && participantRec[0].Survey_Taken_Date__c != null){
						Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, System.Label.LABS_SF_You_have_already_taken_this_survey));
						return false;
					} else {
						Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, System.Label.LABS_SF_Participant_Not_Part_Of_This_Pool));                    
						 return false;
					}
				}
			}
	}
}

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
HI Tony,

while accessing the record from LIst please specify the array element number to access the data.
for ex:  usrRec.NT_Login_Name__c.  -->  usrRec[0].NT_Login_Name__c

Please try the below code:
 
if(requestId != null) {

    List<GLOBAL_Request__c> requestObj = [select id, business_unit__c, recordtype.developername from GLOBAL_Request__c where id=:requestId AND recordtype.developername = 'Send_Survey' Limit 1];
	if (requestObj != null){        
	List<User> usrRec =[select id, NT_Login_Name__c from User where id=:UserInfo.getUserId() And NT_Login_Name__c != null Limit 1];
			if(usrRec != null){
			List<Contact> contactRec = [select id, contactRec.NT_Login_Name__c from contact where NT_Login_Name__c=:usrRec[0].NT_Login_Name__c ORDER by LastModifiedDate DESC Limit 1];         
				if (contactRec != null){
				  List<Participant__c> participantRec = [select Id, Name,Survey_Taken_Date__c from Participant__c where Participant__c=:contactRec[0].Id AND Request__c=:requestObj[0].Id ORDER by CreatedDate DESC LIMIT 1];
					if(participantRec != null && participantRec[0].Survey_Taken_Date__c == null){
						ParticipantId = participantRec[0].Id;
						contactId = contactRec[0].Id;
						requestId = requestObj[0].Id;
						Participant__c updateSurveyTakenDate = new Participant__c(id = ParticipantId, Survey_Taken_Date__c = System.now());            
							   upsert updateSurveyTakenDate;  
					} else if(participantRec != null && participantRec[0].Survey_Taken_Date__c != null){
						Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, System.Label.LABS_SF_You_have_already_taken_this_survey));
						return false;
					} else {
						Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, System.Label.LABS_SF_Participant_Not_Part_Of_This_Pool));                    
						 return false;
					}
				}
			}
	}
}

Thanks,
Maharajan.C
ravi soniravi soni
try below code.
if(requestId != null) {
        
               List<GLOBAL_Request__c> requestObj = [select id, business_unit__c, recordtype.developername from GLOBAL_Request__c where id=:requestId AND recordtype.developername = 'Send_Survey' Limit 1];
            if (requestObj != null){        
            List<User> usrRec =[select id, NT_Login_Name__c from User where id=:UserInfo.getUserId() And NT_Login_Name__c != null Limit 1];
                    if(usrRec != null){
                    List<Contact> contactRec = [select id, contactRec.NT_Login_Name__c from contact where NT_Login_Name__c=:usrRec[0].NT_Login_Name__c ORDER by LastModifiedDate DESC Limit 1];         
                        if (contactRec != null){
                          List<Participant__c> participantRec = [select Id, Name,Survey_Taken_Date__c from Participant__c where Participant__c=:contactRec[0].Id AND Request__c=:requestObj[0].Id ORDER by CreatedDate DESC LIMIT 1];
                            if(participantRec != null && participantRec[0].Survey_Taken_Date__c == null){
                                ParticipantId = participantRec[0].Id;
                                contactId = contactRec[0].Id;
                                requestId = requestObj[0].Id;
                                Participant__c updateSurveyTakenDate = new Participant__c(id = ParticipantId, Survey_Taken_Date__c = System.now());            
                                       upsert updateSurveyTakenDate;  
                            } else if(participantRec != null && participantRec[0].Survey_Taken_Date__c != null){
                                Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, System.Label.LABS_SF_You_have_already_taken_this_survey));
                                return false;
                            } else {
                                Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, System.Label.LABS_SF_Participant_Not_Part_Of_This_Pool));                    
                                 return false;
                            }
                        }
                    }
            }
        }

I'm sure it will help you.
don't forget to mark it as best answer.
Thank you
 
Tony Montana 6Tony Montana 6
Hello Veer,

thanks for your help. It resolved the syntactical errors but i recieved another error during test.

Error:List index out of bounds: 0

What could be the cause?

Regards
TM
Maharajan CMaharajan C
Hi Tony,

Instead of Null check use the IsEmpty function to check the list has data or not:

IsEmpty function will solve the List index out of bounds: 0 issue.
 
if(requestId != null) {

    List<GLOBAL_Request__c> requestObj = [select id, business_unit__c, recordtype.developername from GLOBAL_Request__c where id=:requestId AND recordtype.developername = 'Send_Survey' Limit 1];
	if (!requestObj.isEmpty()){        
	List<User> usrRec =[select id, NT_Login_Name__c from User where id=:UserInfo.getUserId() And NT_Login_Name__c != null Limit 1];
			if(!usrRec.isEmpty()){
			List<Contact> contactRec = [select id, contactRec.NT_Login_Name__c from contact where NT_Login_Name__c=:usrRec[0].NT_Login_Name__c ORDER by LastModifiedDate DESC Limit 1];         
				if (!contactRec.isEmpty()){
				  List<Participant__c> participantRec = [select Id, Name,Survey_Taken_Date__c from Participant__c where Participant__c=:contactRec[0].Id AND Request__c=:requestObj[0].Id ORDER by CreatedDate DESC LIMIT 1];
					if(!participantRec.isEmpty() && participantRec[0].Survey_Taken_Date__c == null){
						ParticipantId = participantRec[0].Id;
						contactId = contactRec[0].Id;
						requestId = requestObj[0].Id;
						Participant__c updateSurveyTakenDate = new Participant__c(id = ParticipantId, Survey_Taken_Date__c = System.now());            
							   upsert updateSurveyTakenDate;  
					} else if(!participantRec.isEmpty() && participantRec[0].Survey_Taken_Date__c != null){
						Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, System.Label.LABS_SF_You_have_already_taken_this_survey));
						return false;
					} else {
						Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, System.Label.LABS_SF_Participant_Not_Part_Of_This_Pool));                    
						 return false;
					}
				}
			}
	}
}

Thanks,
Maharajan.C
This was selected as the best answer
Tony Montana 6Tony Montana 6
Thanks you all for your help. All the above suggestions helped one way or the other.

Best Regards,
TM
Christine MariaChristine Maria
Thanks you information it's so good for improving my work <3
good luck have fun (https://fun888.co/)
Tony MontanaTony Montana
Christine Maria. GOOD LUCK to you too.