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
siree gantisiree ganti 

sObject list question

Hi All,
How can I update a field value for a particular sObject in a List of sObjects? I'm trying to update the status of a task to 'Completed' if the corresponding opportunity status is set to 'Closed Won' by the user.
Thanks much!
 
sandeep sankhlasandeep sankhla
Hi siree,

Please refer the below link for same
 
list<Sobject> lst = new list<Sobject>();
Set<Id> setOpptId = new set<Id>();
	
	for(Opportunity objOppt : [Select Id from Opportunity where StageName = :'Closed Won'])
	{
		setOpptId.add(objOppt.Id);
	}
	
	for(Task objTask : [Select Id, Status from Task where WhatId IN:setOpptId ])
	{
		objTask.Status = 'Completed';
		lst.add(objTask);
	}
	
	update lst;

You can first collect all opptIds where stage is closed won adn then you can qsuery all Taks and update there status as Completed.

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer