You need to sign in to do that
Don't have an account?

How do I fill a lookup relationship field?
I'm having trouble with the following. I have a custom object and in that custom object I have a product manager field. The field needs to be filled with the product manager's name/ID from the user object. I have successfully retrieved the user record but I am unsure how to fill the custom object's product manager field. The code is below. The u1.ID does contain the correct ID for the product manager and I do successfully get the user record. The problem is how do I correctly fill the product_manager_1__c field so it maintains the lookup relationship to the user record. Product_manager_1__C is a lookup field to the user object.
If (PM1 != null ){
User u1 = [Select ID, Name
From User
Where Name = :PM1];
system.debug('pm1 ID = ' + u1.id);
If (u1.Id != '')sr.Product_Manager_1__c = u1.ID;
Update sr;
} //End PM1 if
If (PM1 != null ){
User u1 = [Select ID, Name
From User
Where Name = :PM1];
system.debug('pm1 ID = ' + u1.id);
If (u1.Id != '')sr.Product_Manager_1__c = u1.ID;
Update sr;
} //End PM1 if
If (u1.Id != '')sr.Product_Manager_1__c = u1.ID;
changed it to:
If (u1 != null)sr.Product_Manager_1__c = u1.ID;
All Answers
system.debug('pm1 ID = ' + u1.id);
If (u1.Id != '')sr.Product_Manager_1__c = u1.ID;
Update sr;
Are you getting any error messages in the code?
trigger SR_Set_Approvers on SR_Products__c (After insert, After Update, After Delete) {
// This trigger sets all the product managers from the Stocking Request records
// With the data from the SR - Product Details records
// Loop all product records that have changed
for (SR_Products__c prod : Trigger.new) {
system.debug('product name1 = ' + prod.Stocking_Request__c);
// Get all the product records related to the SR
SR_Products__c[] allproducts = [SELECT product_manager__c, PM_Field__C,
stocking_request__c
FROM SR_Products__c
WHERE stocking_request__c = :Prod.stocking_request__c
];
String Pm1 = '';
String Pm2 = '';
String Pm3 = '';
String Pm4 = '';
String Pm5 = '';
String Pm6 = '';
String Pm7 = '';
String Pm8 = '';
//Set the PM variables with the PM from the SR-Products records
for(SR_Products__c p : allproducts) {
If (p.PM_field__c == 'PM1')
pm1 = p.product_manager__c;
If (p.PM_field__c == 'PM2')
pm2 = p.product_manager__c;
If (p.PM_field__c == 'PM3')
pm3 = p.product_manager__c;
If (p.PM_field__c == 'PM4')
pm4 = p.product_manager__c;
If (p.PM_field__c == 'PM5')
pm5 = p.product_manager__c;
If (p.PM_field__c == 'PM6')
pm6 = p.product_manager__c;
If (p.PM_field__c == 'PM7')
pm7 = p.product_manager__c;
If (p.PM_field__c == 'PM8')
pm8 = p.product_manager__c;
} //end loop products
// Get the Stocking Request record
system.debug('product name = ' + prod.Stocking_Request__c);
Stocking_Request__c sr = [Select ID, Name,
Product_Manager_1__c, Product_Manager_2__c,
Product_Manager_3__c, Product_Manager_4__c,
Product_Manager_5__c, Product_Manager_6__c,
Product_Manager_7__c, Product_Manager_8__c
From Stocking_Request__c
Where ID = :prod.Stocking_Request__c];
// Reset all PM fields to blank to start out with
sr.Product_Manager_1__c = null;
sr.Product_Manager_2__c = null;
sr.Product_Manager_3__c = null;
sr.Product_Manager_4__c = null;
sr.Product_Manager_5__c = null;
sr.Product_Manager_6__c = null;
sr.Product_Manager_7__c = null;
sr.Product_Manager_8__c = null;
// Get the user record IDs for the PM variable fields
// And set the PM fields in the stocking request record
system.debug('pm1 = ' + Pm1 + ''+ Pm2 + ''+ Pm3 + ''+ Pm4 + ''+ Pm5 + ''+ Pm6 + ''+ Pm7 + ''+ Pm8 + '');
If (PM1 != null ){
User u1 = [Select ID, Name
From User
Where Name = :PM1];
system.debug('pm1 ID using u1.id = ' + u1.id);
If (u1.Id != '')sr.Product_Manager_1__c = u1.ID;
Update sr;
system.debug('WE FINISHED!!!!! ');
} //End PM1 if
/*
Stock Requests (Contains product_manager_1__c field which is a lookup field to the user object.)
SR - Products (Contains a string field called product_manager__C with a name in it.)
The code is suppose to take the product_manager__C field from the SR - Products child objects and lookup that name in the user object and then fill in the product_manager_1__C field in the stocking requests object maintaining the user object relationship. (There are actually 8 fields but just trying to get the first one right.)
11:46:49.151 (151829162)|VARIABLE_ASSIGNMENT|[67]|u1|{"serId":1,"value":{"Name":"Aaron Haas","Id":"005d0000001T1b2AAC"}}|0x6aaff94b
11:46:49.151 (151838581)|STATEMENT_EXECUTE|[70]
11:46:49.151 (151846501)|HEAP_ALLOCATE|[70]|Bytes:22
11:46:49.151 (151900235)|SYSTEM_METHOD_ENTRY|[70]|String.valueOf(Object)
11:46:49.151 (151930464)|HEAP_ALLOCATE|[70]|Bytes:18
11:46:49.151 (151946003)|SYSTEM_METHOD_EXIT|[70]|String.valueOf(Object)
11:46:49.151 (151957332)|HEAP_ALLOCATE|[70]|Bytes:40
11:46:49.151 (151972714)|SYSTEM_METHOD_ENTRY|[70]|System.debug(ANY)
11:46:49.151 (151981303)|USER_DEBUG|[70]|DEBUG|pm1 ID using u1.id = 005d0000001T1b2AAC
11:46:49.151 (151988973)|SYSTEM_METHOD_EXIT|[70]|System.debug(ANY)
11:46:49.152 (152195733)|HEAP_ALLOCATE|[71]|Bytes:16
11:46:49.152 (152362042)|FATAL_ERROR|System.StringException: Invalid id:
Trigger.SR_Set_Approvers: line 71, column 1
11:46:49.152 (152403141)|FATAL_ERROR|System.StringException: Invalid id:
If (u1.Id != '')sr.Product_Manager_1__c = u1.ID;
changed it to:
If (u1 != null)sr.Product_Manager_1__c = u1.ID;