+ Start a Discussion
sss pppsss ppp 

Apex for loop with SOQL query

I want to update price to 100 of all Invoice Number whose status is 'open'.
Please help me write code.
Medhya MahajanMedhya Mahajan
If you do not have any existing records, you can go ahead an simple write a workflow with a field update. 

In case, you have existing records, 
  • Use the below script ( Run in excecute Anonymous ) to update existing records
  • Then create a workflow meeting the criteria.
List<Invoice_Number > invoiceNumberList = [SELECT Id,Status,Price FROM Invoice_Number WHERE Status =: 'Open'];
List<Invoice_Number > listInvoiceToUpdate = new List<Invoice_Number >();
for(iInvoice_Number  in : invoiceNumberList){
	in.Price = 100;

	system.debug ('+++++' + listInvoiceToUpdate);
	update listInvoiceToUpdate;

You can first comment out the update statement, check the debug and if the records are correct uncomment ant execute the script.

Hope this helps.

Medhya Mahajan