You need to sign in to do that
Don't have an account?
AlphaP
Assign Record Owner to Custom Object based on matching a data element to User object
trigger CEWSowner on PSC_CEWS__c (before insert) { PSC_CEWS__c [] CEWSList = trigger.new; List <PSC_CEWS__c> updatedListToInsert= new List <PSC_CEWS__c>(); for(PSC_CEWS__c CEWS : CEWSList){ List<User> lookupCEWSUser = [select ID from User where CAI__c =: CEWS.CAI__c Limit 1]; if(lookupCEWSUser.size() > 0){ CEWS.Ownerid = lookupCEWSuser[0].Id; updatedListToInsert.add(CEWS); } } if(updatedListToInsert.size() > 0) { insert updatedListToInsert; } }
So I have a custom object 'PSC_CEWS__c' that is upserted/inserted with a unique identifier for each user 'CAI__c.' Before insert, i want to match the CAI__c to the correct User in Salesforce, and make that person the record owner. So far, this actually does nothing. I have something typed wrong, but have looked at it long enough and am thinking I'm missing something real basic here.
Any thoughts?
That makes this trigger a whole lot simpler...
All Answers
First off, if the trigger were actually doing something all it would do is create duplicate PSC_CEWS since you are inserting more PSC_CEWS records. If I understand your business requirements you have a CAI__c field on PSC_CEWS__c which corresponsondes to a CAI__c field on user which you want to use to set the owner of the PSC_CEWS record. The trigger you want is something like this:
Your understanding is 100% correct! Thanks.
I'm having trouble with this line:
if(user!=null) obj.Ownerid = u.ID;
I get this error:
That should be
I see. I thought it might be a bracketing issue for the loops. I tried that out with 25 test records that have CAIs that map to the User object and still ended up with ownership of all 25 records. It compiled and saved, but didn't assign ownership based on the CAI.
Anything else I could try?
Does it matter that CAI__c is a lookup field on the PSC_CEWS obj? If ownership assigns properly, this could just be a text field.
Can you put some system.debugs in there to see what is going on? Check what the query in the for loop is returning.
Since PSC_CEWS.CAI__c is a lookup field it contains a user id. Does User.CAI__c also contain a user id?
Sure thing - I'll get those in later tonight and see what gets returned.
Right - the PSC_CEWS.CAI__c contains a unqiue, 5 character code for each agent. That lookup field is matching to the appropriate User (via the User.CAI__c field) and returning the name. I wasn't sure if that might be goofing this up.
Thanks for checking back and I'll post the results of the debug. I have to run to a meeting before I can send another test file to Salesforce.
Am i using System.debug right? I've used assertions to test some basic code, but not this, Thanks!
You're using system.debug correctly. However, if PSC_CEWS.CAI__c is a lookup field like you said earlier it contains an 18 digit salesforce id (for the user object).
I think your issue is that the CAI__c field on PSC_CEWS does not match the values you have in the CAI__c field on the user object.
The CAI__c lookup field on the PSC_CEWS object does display the correct name of the agent for the corresponding user record. Is that how lookup fields work when working with them in Apex? They house the ID for the record that is related and not the data element that is displayed (in this case, the user name)?
Wow. The debug logs answered my own question. I had been banging my head against a wall for the last week - it thought the lookup field held the value for CAI since that was the data element connecting the two records together.
Here's what I did for my test. I set the value of CAI__c on the file I upserted to the PSC_CEWS object. I set the value of all 28 records to 9HKDV which belongs to User ID 00530000005dL5c. Generally, there would be around 15 different values and a couple might return more than once - which is fine. I double checked the data in the objects before import. Now, three of those records had a null value in an unreated field (a date field) that requires a value. So I was expecting 25 succesful imports and 3 failures (a blank date field means the survey wasn't completed and I don't want to import).
Now this time, I ended up with a null pointer exception that hasn't happened before"
CEWSowner: execution of BeforeInsert
caused by: System.NullPointerException: Attempt to de-reference a null object
Trigger.CEWSowner: line 21, column 46
Debug Log:
23.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
18:03:51.154 (154710000)|EXECUTION_STARTED
18:03:51.154 (154749000)|CODE_UNIT_STARTED|[EXTERNAL]|TRIGGERS
18:03:51.154 (154767000)|CODE_UNIT_STARTED|[EXTERNAL]|01qV00000004ElC|CEWSowner on PSC_CEWS trigger event BeforeInsert for [new, new, new, new, new, new, new, new, new, new, new, new, new, new, new, new, new, new, new, new, new, new, new, new, new, new, new, new]
18:03:51.155 (155515000)|SYSTEM_METHOD_ENTRY|[6]|SET.add(ANY)
18:03:51.155 (155560000)|SYSTEM_METHOD_EXIT|[6]|SET.add(ANY)
18:03:51.155 (155572000)|SYSTEM_METHOD_ENTRY|[6]|SET.add(ANY)
18:03:51.155 (155581000)|SYSTEM_METHOD_EXIT|[6]|SET.add(ANY)
18:03:51.155 (155588000)|SYSTEM_METHOD_ENTRY|[6]|SET.add(ANY)
18:03:51.155 (155595000)|SYSTEM_METHOD_EXIT|[6]|SET.add(ANY)
18:03:51.155 (155602000)|SYSTEM_METHOD_ENTRY|[6]|SET.add(ANY)
18:03:51.155 (155608000)|SYSTEM_METHOD_EXIT|[6]|SET.add(ANY)
18:03:51.155 (155615000)|SYSTEM_METHOD_ENTRY|[6]|SET.add(ANY)
18:03:51.155 (155622000)|SYSTEM_METHOD_EXIT|[6]|SET.add(ANY)
18:03:51.155 (155628000)|SYSTEM_METHOD_ENTRY|[6]|SET.add(ANY)
18:03:51.155 (155635000)|SYSTEM_METHOD_EXIT|[6]|SET.add(ANY)
18:03:51.155 (155641000)|SYSTEM_METHOD_ENTRY|[6]|SET.add(ANY)
18:03:51.155 (155648000)|SYSTEM_METHOD_EXIT|[6]|SET.add(ANY)
18:03:51.155 (155654000)|SYSTEM_METHOD_ENTRY|[6]|SET.add(ANY)
18:03:51.155 (155661000)|SYSTEM_METHOD_EXIT|[6]|SET.add(ANY)
18:03:51.155 (155667000)|SYSTEM_METHOD_ENTRY|[6]|SET.add(ANY)
18:03:51.155 (155674000)|SYSTEM_METHOD_EXIT|[6]|SET.add(ANY)
18:03:51.155 (155681000)|SYSTEM_METHOD_ENTRY|[6]|SET.add(ANY)
18:03:51.155 (155688000)|SYSTEM_METHOD_EXIT|[6]|SET.add(ANY)
18:03:51.155 (155695000)|SYSTEM_METHOD_ENTRY|[6]|SET.add(ANY)
18:03:51.155 (155701000)|SYSTEM_METHOD_EXIT|[6]|SET.add(ANY)
18:03:51.155 (155708000)|SYSTEM_METHOD_ENTRY|[6]|SET.add(ANY)
18:03:51.155 (155715000)|SYSTEM_METHOD_EXIT|[6]|SET.add(ANY)
18:03:51.155 (155721000)|SYSTEM_METHOD_ENTRY|[6]|SET.add(ANY)
18:03:51.155 (155728000)|SYSTEM_METHOD_EXIT|[6]|SET.add(ANY)
18:03:51.155 (155734000)|SYSTEM_METHOD_ENTRY|[6]|SET.add(ANY)
18:03:51.155 (155741000)|SYSTEM_METHOD_EXIT|[6]|SET.add(ANY)
18:03:51.155 (155747000)|SYSTEM_METHOD_ENTRY|[6]|SET.add(ANY)
18:03:51.155 (155754000)|SYSTEM_METHOD_EXIT|[6]|SET.add(ANY)
18:03:51.155 (155760000)|SYSTEM_METHOD_ENTRY|[6]|SET.add(ANY)
18:03:51.155 (155767000)|SYSTEM_METHOD_EXIT|[6]|SET.add(ANY)
18:03:51.155 (155774000)|SYSTEM_METHOD_ENTRY|[6]|SET.add(ANY)
18:03:51.155 (155780000)|SYSTEM_METHOD_EXIT|[6]|SET.add(ANY)
18:03:51.155 (155787000)|SYSTEM_METHOD_ENTRY|[6]|SET.add(ANY)
18:03:51.155 (155794000)|SYSTEM_METHOD_EXIT|[6]|SET.add(ANY)
18:03:51.155 (155800000)|SYSTEM_METHOD_ENTRY|[6]|SET.add(ANY)
18:03:51.155 (155807000)|SYSTEM_METHOD_EXIT|[6]|SET.add(ANY)
18:03:51.155 (155813000)|SYSTEM_METHOD_ENTRY|[6]|SET.add(ANY)
18:03:51.155 (155820000)|SYSTEM_METHOD_EXIT|[6]|SET.add(ANY)
18:03:51.155 (155827000)|SYSTEM_METHOD_ENTRY|[6]|SET.add(ANY)
18:03:51.155 (155833000)|SYSTEM_METHOD_EXIT|[6]|SET.add(ANY)
18:03:51.155 (155840000)|SYSTEM_METHOD_ENTRY|[6]|SET.add(ANY)
18:03:51.155 (155847000)|SYSTEM_METHOD_EXIT|[6]|SET.add(ANY)
18:03:51.155 (155853000)|SYSTEM_METHOD_ENTRY|[6]|SET.add(ANY)
18:03:51.155 (155860000)|SYSTEM_METHOD_EXIT|[6]|SET.add(ANY)
18:03:51.155 (155866000)|SYSTEM_METHOD_ENTRY|[6]|SET.add(ANY)
18:03:51.155 (155873000)|SYSTEM_METHOD_EXIT|[6]|SET.add(ANY)
18:03:51.155 (155879000)|SYSTEM_METHOD_ENTRY|[6]|SET.add(ANY)
18:03:51.155 (155886000)|SYSTEM_METHOD_EXIT|[6]|SET.add(ANY)
18:03:51.155 (155893000)|SYSTEM_METHOD_ENTRY|[6]|SET.add(ANY)
18:03:51.155 (155900000)|SYSTEM_METHOD_EXIT|[6]|SET.add(ANY)
18:03:51.155 (155906000)|SYSTEM_METHOD_ENTRY|[6]|SET.add(ANY)
18:03:51.155 (155913000)|SYSTEM_METHOD_EXIT|[6]|SET.add(ANY)
18:03:51.155 (155919000)|SYSTEM_METHOD_ENTRY|[6]|SET.add(ANY)
18:03:51.155 (155926000)|SYSTEM_METHOD_EXIT|[6]|SET.add(ANY)
18:03:51.155 (155950000)|SOQL_EXECUTE_BEGIN|[11]|Aggregations:0|select CAI__c, ID from user where CAI__c in :CAIs
18:03:51.163 (163182000)|SOQL_EXECUTE_END|[11]|Rows:0
18:03:51.163 (163235000)|SYSTEM_METHOD_ENTRY|[13]|System.debug(ANY)
18:03:51.163 (163265000)|USER_DEBUG|[13]|DEBUG|This is tha value for CAIs: {00530000005dL5cAAE}
18:03:51.163 (163274000)|SYSTEM_METHOD_EXIT|[13]|System.debug(ANY)
18:03:51.163 (163297000)|SYSTEM_METHOD_ENTRY|[16]|MAP.get(ANY)
18:03:51.163 (163316000)|SYSTEM_METHOD_EXIT|[16]|MAP.get(ANY)
18:03:51.163 (163330000)|SYSTEM_METHOD_ENTRY|[17]|System.debug(ANY)
18:03:51.163 (163338000)|USER_DEBUG|[17]|DEBUG|This is the value for for obj.CAI__c: 00530000005dL5cAAE
18:03:51.163 (163343000)|SYSTEM_METHOD_EXIT|[17]|System.debug(ANY)
18:03:51.163 (163353000)|SYSTEM_METHOD_ENTRY|[21]|System.debug(ANY)
18:03:51.163 (163389000)|EXCEPTION_THROWN|[21]|System.NullPointerException: Attempt to de-reference a null object
18:03:51.163 (163532000)|SYSTEM_METHOD_EXIT|[21]|System.debug(ANY)
18:03:51.163 (163573000)|FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object
Trigger.CEWSowner: line 21, column 46
18:03:51.852 (163607000)|CUMULATIVE_LIMIT_USAGE
18:03:51.852|LIMIT_USAGE_FOR_NS|(default)|
Number of SOQL queries: 1 out of 100
Number of query rows: 0 out of 50000
Number of SOSL queries: 0 out of 20
Number of DML statements: 0 out of 150
Number of DML rows: 0 out of 10000
Number of script statements: 35 out of 200000
Maximum heap size: 0 out of 3000000
Number of callouts: 0 out of 10
Number of Email Invocations: 0 out of 10
Number of fields describes: 0 out of 100
Number of record type describes: 0 out of 100
Number of child relationships describes: 0 out of 100
Number of picklist describes: 0 out of 100
Number of future calls: 0 out of 10
18:03:51.852 (163607000)|CUMULATIVE_LIMIT_USAGE_END
18:03:51.163 (163698000)|CODE_UNIT_FINISHED|CEWSowner on PSC_CEWS trigger event BeforeInsert for [new, new, new, new, new, new, new, new, new, new, new, new, new, new, new, new, new, new, new, new, new, new, new, new, new, new, new, new]
A lookup (or master-detail) always contains the salesforce id of the record you are looking up to. It is basically the same thing as a foreign key in a database. The reason you're getting a nullpointer exception on line 21 is because u is null and you are trying to system.debug u.id.
This may be a dumb question but is the PSC_CEWS.CAI__c user the same user you are trying to make the owner?
That makes this trigger a whole lot simpler...
Ha! It sure does! Thanks for the help! I think if I had realized that fields that lookup to another object actually hold the ID, i might have been ablke to get farther along on my own. I made this WAY too complicated. ;p
Also, using system.debug will help me in future efforts if I need to write something simple in the future. Thanks so much!