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
venkatasubhashkvenkatasubhashk 

How to update a field in Apex from a Relationship Query

I have a Query like this ...

 

 

for(opportunity opp1 :opp){

ProcessInstance p= [SELECT Id, (SELECT Id, StepStatus,Comments FROM Steps)FROM ProcessInstance  WHERE Status='Approved' and  TargetObjectId=:opp1.id limit 1];

 

opp1.comments = p.comments; 

 

//This is not working

 

i want to get the comments from the Query to custom field

 

}

 

any ideas please!!

 

bob_buzzardbob_buzzard

The ProcessInstance object doesn't have a field called 'comments', so you won't be able to assign that value to the opp1.comments field.

 

The Steps within the process do have comments, but there may be many Steps associated with a ProcessInstance.

 

Thus you probably want to produce a concatenated set of comments for the opportunity, something like:

 

 

for(opportunity opp1 :opp){
ProcessInstance p= [SELECT Id, (SELECT Id, StepStatus,Comments FROM Steps)FROM ProcessInstance  WHERE Status='Approved' and  TargetObjectId=:opp1.id limit 1];

String commentStr='';
for (Step st : p.Steps)
{
   if (st.Comments!=null)
   {
      commentStr+=st.Comments+'\n';
   }
}
opp1.comments = commentStr; 
 
}