You need to sign in to do that
Don't have an account?
Simple Trigger Question
Hi Community,
I am new to Salesforce and using APEX. I am completely self-taught too. With that said, I am trying to learn triggers and cannot find an exact example of how to achieve what I am working on. My problem is that when a Contact updates their information through my Visualforce page, Salesforce will overwright fields with the new data they inputted - which is great, except for the 'Description' field. I want the new 'Description' to append to whatever they previously had written in that field. Can anyone help me?
The pseudo code would be something like:
trigger UpdateDescription on Contact (before update) {
Contact.Description = Contact.OldDescription + " " + Contact.NewDescription;
}
Thank you in advance for your help!
You could do like
Thanks,
Hello,
I implemented Pranab's code that he last posted - however, I have a problem. When I delete the Description field on the contact profile page, it won't delete the field! I think it is appending the old description to a blank description (because I highlight all the text in the textbox and hit delete, then save). How can I get around this?
Thank you!
Lars, thank you for your help! Unfortunately, this is giving me a problem. Everytime someone edits anything on the Contact profile and hits 'Save', the Description is duplicated and appended to itself with an extra timestamp. How can I fix this? Thank you so much!
In all the code adove and previous samples people have submitted Description = Old Description + new line + Time plus New Description. The issue is that Old and New will be the same if the user edits the record but doesn't change the description. If you truly want to solve this where it works like an audit trail you're going to have to take a different approach where you try to parse out only what got appended and stick a timestamp in front of just the new /removedportion. In the case above I remove adding "oldDesc" from the second to last line before the break
Like I said, it depends on where you stick the Time and new lines as to how it looks at the end.
Hi Lars,
Thank you so much! That code works when I edit anything on the Contact profile page and appends correctly. However, I am using a VisualForce page to collect Contact data. They fill out a Description textbox and that gets updated into the Contact's Description field. For some reason, when the data comes from my VisualForce page, it completely overwrites the Description field. Why is this? Can the trigger fix this? Ideally, I want the data from my VisualForce page's Description field to append onto my Contact's Description field.
Thank you so much Lars!
Hi Lars,
Could anyone provide any additional insights to my issue described above? Thank you.
Thank you in advance!
https://developer.salesforce.com/docs/atlas.en-us.200.0.pages.meta/pages/pages_controller_extension.htm
Without seeing the source it is hard to say. If it all works in the standard Contact page but acts differently through your custom visualforce page then my guess is the issue is with the VF page/controller code.
Hi Lars,
I agree, and these were my initial thoughts too. As you can see in my controller, I am extending the standard contact controller - however, I am still lost at how/where/why it is not functioning correctly when updating through my VF Page/Controller. Additionally, I've added the code for my controller and VF page. Please let me know what you think, I've looked everything multiple times and can't find the bug! Thank you so much for your support, Lars! You've been great!
My controller:
My VF Page:
Hello Community,
Any thoughts? The logic seems correct to me - but I am new and perhaps there is something I am not accounting for. Any help would be appreciated!
Thank you!