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
ynrynr 

Trigger to add text inthe field.

I got two objects 'Promotion' and 'Feedback'. Promotion has got a field called "isChanged" and Feedback has got a field called "Feedback body" which is of text area. The two objects are related by Id (Promotion Field) and promotion Id (Feedback Field)

 

I would like to write a trigger in such a way that whenever users changes the status of promotion to "Closed", there must be a predefined feedback text generated in the Feedback Body field.

 

The code which I have written is below. But this is not helping though am not getting any errors.

trigger StatusChange on Promotion (after update) 
 {
  for(Promotion i : Trigger.new)
  
  {
   if(i.isChanged == true)
    {
     String str = i.Id;
      Feedback ic = [Select FeedbackBody from Feedback where PromotionId = :str];
       ic.FeedbackBody = 'Staus has been changed';

    }
  }
 }
sandersensandersen

I see a couple things. First, I would explicitly look for the Status field changing. do this by gettting the before and after values and comparing that specific field. Use trigger.oldMap to find the corresponding record from the before action.

 

You also are putting the Id in a string variable. There is a primitive data type called Id, so I would use that.

 

You've got your SOQL statement in your for loop. This isn't a best practice as it will cause governor limit problems if someone updates a bunch of Promotions via the dataloader. Gather all the Promotions into a list or a map, and then act on them outside the for loop.

 

Steve 

KrishnadasKrishnadas

Hi, 

Apart from what Steve has suggested I do not see any line to update the FeedBack record after assigning value to its field. This could be the reason that it does not update the field in Feedback object.

 
Thanks
Krishnadas
Steadfast Global
www.steadfastglobal.com