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
PallavPallav 

How to use trigger after cloning ??? NEED URGENT HELP

Hi All,
I am facing issue with trigger that I have to develop.

Problem: A newly created record inserts the current user id and username in the owner id and owner name fields natively on save.


Wanted: Apex trigger to keep the current owner id/name the same as existing owner id/name on saving of the record if it does not match the current userId or createdbyId.


Use Case:

1. Open record
2. Change record owner to someone else
3. Save record
4. Clone record

5. Save new record (with the same owner as original pre-cloned record)
6. The new cloned case should still be owned by the previous owner.

Notes: Standard clone code will show the original owner on the owner field. The new record gets updated to the userId/Name on Save.

Thanks in anticipation of your response.
Pallav
jrotensteinjrotenstein
It's a little unclear what you're saying. You seem to be saying that there is existing functionality (either in Apex or Workflow) that sets the Owner fields when a new record is created? And, you want to make sure this value is not overwritten later?

Is this a standard object or a custom object? Or by "natively", do you mean that this is happening without any specific code (meaning it's a standard object)?

It would help if you could explain your situation and problem a bit clearer, perhaps just picking one particular Use Case for the moment.
PallavPallav
Hi,

Thanks for replying. I would certainly explain the situation:

Suppose I create a contact and for that contact I would be the owner of the contact as I am logged in with my credentials. now suppose, I have changed the owner of the Contact to  some "XYZ" user and then clone the contact. Now, with the SFDC functionality when I save the colned contact then it will set me as the owner of the Contact. However, my requirement is to save the owner of the contact as "XYZ" user and not by my name.

This is what is not working in the apex trigger. Please let me know if I can do and is it possible from Apex code.

Thanks in anticipation of your answer.

Thanks and regards
Pallav
jrotensteinjrotenstein
Aha! Thanks for the clarification.

To test this, I took an existing case, set the owner to another person, then hit the Clone button. Initially, the screen showed the original owner (as a read-only field), but upon saving it changed the owner to me. In fact, I had a validation error on the Save due to another field, and noticed that it had changed the Case Owner before actually saving the record.

Therefore, it does not seem possible to access the "before" value of the Owner field. Being a new record, this will fire the "before insert" Trigger, and there aren't any "old" values. This is proven by the fact that the owner field was updated on my screen before the record was even saved.

A workaround would be:
  • Create a new field (eg OldOwner__c)
  • Create a workflow rule (or a before update Trigger) to copy Owner to OldOwner__c whenever Owner changes
  • Create a before insert Trigger that looks at the OldOwner__c field. If it has a value, copy that value to Owner
Thus:
  • If a Case is being created via the 'New' button, the OldOwner__c field will be blank, so there is no need to change Owner
  • If a Case is being created via the 'Clone' button, the OldOwner__c field will contain a value, which the Trigger will copy to Owner
Would that suffice?

PallavPallav
Hi jrotenstein,

Thanks a lot for all the Great help. You have made my day.... I really really appriciate your help and explaination that you have given. God bless you.

Thanks and regards
Pallav
PallavPallav
Hi,

With the great help that you have given I have developed the trigger. However, do you have any idea why, when the OldOwner__c field is made hidden(removed from the page layout) I am able to set the value for the field but not able to retrive the value to put it in the Owner field. Please let me know if you have any clue to that.

Thanks in anticipation of your answer.
Thanks and regards

jrotensteinjrotenstein

Are you saying that:

  • When your OldOwner__c field is NOT on a Page Layout
  • It cannot be accessed by the trigger?
  • And simply making it visible on a Page Layout makes your Trigger work again?
sfdcfoxsfdcfox
With the design of the system (Salesforce) as it is, there is no way to determine what the old values were. You would need to create a Visualforce page and overlay the edit pages with this custom page in order to make this work you expect. If the field is not on the layout in your design, it won't be populated and Salesforce will have no way of knowing what the old record was.


Message Edited by sfdcfox on 06-09-2008 02:29 PM
PallavPallav
Ya very true. I could not even fire SOQL to get the values. Do you have any clue??

Thanks in anticiaption of your answer.

Thanks and regards
Pallav