You need to sign in to do that
Don't have an account?
DustinLH
User Lookup Field to Change Owner
Does anyone have any code that takes a custom user lookup field and changes the owner of the record to match and vice versa (changing owner updates user lookup field)?
I have a use case where I need to force the user to identify the owner of the record during the creation of the record and not rely on their memory to change the owner after the fact.
I think this should just about cover your request... Change object names and field names to suit your environment.
All Answers
It may help to know a little more about your use case here. I guess what I don't understand is...if the user is settig a User lookup field...why don't they also just change the owner?
Otherwise, this can be done easily with a validation rule (record cannot be saved until User lookup field matches record Owner field) or with a simple Apex trigger. Once I know a little more detail, I can assist with the trigger if that's the correct option.
The custom object record (Referral) the user is creating is always going to be re-assigned to another user. We have cases where they forget to change the owner, since you have to do it after the record is created. Since the owner is not changed, the record never gets addressed by the user it was supposed to go to and customer service is impacted.
The custom field (Assigned) would be required and force the user to identify the user they wish to assign the record to. I cannot use a validation rule, because they cannot change the owner during the creation of the record. You cannot change owners in edit mode, only from viewing the record.
My hope was to have a trigger that would change the owner for them when they populate the field. To avoid confusion, or other issues, I would want the trigger to also update the field if the owner was changed the normal way just so that it is always in sync with the owner field.
Does that make sense? If not let me know.
I think this should just about cover your request... Change object names and field names to suit your environment.
Thanks sfdcfox! I think we are close, but I am getting the following error:
Error: Compile Error: Method does not exist or incorrect signature: [LIST<Referral__c>].get(Id) at line 6 column 23
Here is the code:
Had a typo... I updated the original post to change old to oldMap. This should work.
sfdcfox
This works great! Thank you so much for the help! If you have a second, can you look at my test class? I am only getting 36% coverage for the trigger and am not sure what to do with it.
You tested only the insert trigger portion. Your entire test class should look more like this:
Note that our code does assume that Assigned__c will always be populated (it'll error otherwise). Ordinarily this won't happen, but it could if you forgot to map a field on an import, for example.
sfdcfox
I am getting this error with that test:
Error: Compile Error: Method does not exist or incorrect signature: system.assertEquals(Id) at line 16 column 5
Thanks again for all of your help. Hopefully we are close!
Updated that last post, as well. I wasn't appearantly paying attention.
sfdcfox
It looks like it is getting a user id when it shouldn't be. This is the message I am seeing when I run the test and it fails:
System.AssertException: Assertion Failed: Expected: null, Actual: 00580000001k6g1AAAClass.testReferralAssignOwner.test: line 11, column 1
The 00580000001k6g1AAA is a user id.
sfdcfox
I just wanted to follow up and see if you had any suggestions as to why the test would be expecting a null in that result but getting an id instead. Please let me know if you can assist. Thanks again for your help!
I'm getting that same error when running the test method. Were you ever able to figure this out?
I had to start a new post since I didn't get a response from sfdcfox. Here is the link to that new thread:
http://boards.developerforce.com/t5/Apex-Code-Development/User-Lookup-Owner-Change-Trigger-Modification/td-p/395819
Here is the code that I ended up using (from that same thread):
That worked great,
Can you help me modify my test script though? I continue to receive this error, which I noticed you referenced above:
System.AssertException: Assertion Failed: Expected: null, Actual: 005400000016MvaAAE
I am not great at writing test code yet, but here is the link to the thread about the test class troubles I had:
http://boards.developerforce.com/t5/Apex-Code-Development/system-assertEquals-Failure-on-Test-Class/m-p/395793#M70993
And here is the code that I finally ended up with for my test class: