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
rohi rohirohi rohi 

select after for loop

Hi,
I'm new to salesforce.
In trigger handler i need to check values based on contact id.

First i need to check the contact id in contacts object then take that contact id and check whether that id is exists in Request custom object along with other fields.If the second query returns empty then i need to add that contact id in Request object.

following is my code 

public static void createProces(List<Contact> oContacts)
 {
 List<Request__c> lsProcess_Request;
  Request__c  oRequest = new  Request__c(); 
 List<Request__c> uRequest = new List<Request__c>();

 for (Contact c : [SELECT Id, Email,Status__c  FROM Contact WHERE Id IN: oContacts] )
        {   
            //check whether active process or not
            lsProcess_Request =[SELECT Active__c ,Member__c,attempt_1__C,attempt_2__C,Attempt_1__c,Attempt___c FROM  Request__c WHERE
                               member__c =: c.Id and Active__c=true and (Attempt_1__c = true OR Attempt_2 __c = true )];
          if(lsProcess_Request == null)
{
               oRequest.Member__c = c.id;             
                oRequest.Attempt_1_Date__c = Date.today();
                oRequest.Attempt_2_Date__c = Date.today()+3;
               lwr.add(oRequest); 
}
           }
}

 Please advise me on this.
SidhantSidhant

Hey,

You are doing SOQL query in a for loop. Do not do this. Ever. Since you are a beginner, make it a practice to recheck your code whenever you are using loops and queries in your class.
Since the records oContacts are coming from the trigger, I omitted your first query and for loop. There might be another simple way to do this, but this is what came from the top of my mind. Try this out, and let me know if I misunderstood the problem.

public static void createProces(List<Contact> oContacts) {
	List<Request__c> lsProcess_Request = new List<Request__c>();
	Request__c  oRequest; 
	Set<Id> setOfmatchFound = new Set<Id>();
	//populate a set to store the Contacts associated with Requests
	for(Request__c reqRecord : [SELECT Active__c, Member__c, attempt_1__C, attempt_2__C, Attempt_1__c, Attempt___c
								FROM Request__c
								WHERE member__c IN: oContacts 
								AND Active__c=true 
								AND (Attempt_1__c = true OR Attempt_2 __c = true )]) {
		setOfmatchFound.add(reqRecord.member__c);
	}
	
	//Traverse all the contacts, if the contact record is not present in the above Set
	//then create a Request record
	for(Contact conRecord : oContacts) {
		if(!setOfmatchFound.contains(conRecord.Id)) {
			//new instance od Request object
			oRequest = new  Request__c(); 
			oRequest.Member__c = conRecord.Id;             
			oRequest.Attempt_1_Date__c = Date.today();
			oRequest.Attempt_2_Date__c = Date.today()+3;
			lsProcess_Request.add(oRequest);
		}
	}
	
	try {
		insert lsProcess_Request;
	} catch(Exception ex) {
		//Do something here
		system.debug('Exception: '+ex);
	}
}

Thanks,

Sidhant