You need to sign in to do that
Don't have an account?

APEX Trigger and SOQL Help
I have an Id for a record I am getting from a trigger and I want to compare it to an Id in my SOQL Where clause. What I am doing wrong
trigger UpdateTest on Test__c (before insert) { List<Test__c> testList = new List<Test__c>(); System.debug(Trigger.New); ID accNo; for(Test__c a : Trigger.New) { accNo = a.Account__c; } testList = [SELECT Id, Status__c, Account__c FROM Test__c WHERE Account__c = accNo]; List<Test__c> updateList = new List<Test__c>(); for(Test__c contract : testList){ contract.Status__c = 'Past'; updateList.add(contract); } update updateList; }
Please try the below code. I hope it works.
In your code when you are using the accNo to assign the value of a.Account__c in the for each loop,there will be only one value in the accNo variable. So i have declared a set<id> so that it could hold all the values in the loop and in the soql query i am fetching all the rows for which Account__c in accNo.
I hope its helpful.
trigger UpdateTest on Test__c (before insert) {
List<Test__c> testList = new List<Test__c>();
System.debug(Trigger.New);
//ID accNo;
set<id> accNo=new set<id>();
for(Test__c a : Trigger.New) {
//accNo = a.Account__c;
accNo.add(a.Account__c);
}
testList = [SELECT Id, Status__c, Account__c FROM Test__c WHERE Account__c in :accNo];
List<Test__c> updateList = new List<Test__c>();
for(Test__c contract : testList){
contract.Status__c = 'Past';
updateList.add(contract);
}
update updateList;
}
First of all in "before Insert" Trigger you do not need to update your List.It updates new changes automatically.
Second you need to use bind operator (:) in your query to access accNo.
So you just need to make changes in your testList and rest will happen itself.
Try the below code:
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
I am new to salesforce.
I have a query in your answer, in the for loop you are using to assigning the accNo with a.Account__c, how is it possible with the id field to be assigned multpile values in an iteration and use it in the soql query .
I'm actually confused weather the variable type ID is iterable ?? Because its being assigned with values of a.Account__c in the for each loop.
So please help me understand this.
Below is my updated answer for the question, but i haven't noticed the update statement,your point about update statement is correct.
Please correct me if i'm wrong.
Please follow the given below code with the help of these you can solve your problem, it may be helpful to you.
Follow the below code :
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha
Combining some of the things from the previous answers. So as "mahesh padigela 1" replied, you need to store the Account__c IDs into a set and use the set in the query. But you also need to have the update DML operation in there because you are updating other records (not the ones in Trigger.New). Generally it is better to do DML operations on other objects only after the insert. Thus I would formulate the code like this.
Regards,
Miika
@Sheldon Callahan 16,
Please try the above solution given by Miika, and mark the question as solved if your query is clear to keep the community clean :) .
Regards,
Mahesh