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
karol.freebergkarol.freeberg 

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
Best Answer chosen by karol.freeberg
karol.freebergkarol.freeberg
OK, it was the IF statement. Cannot compare u1.id != ""!

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

TejTej
what kind of error are you getting?
karol.freebergkarol.freeberg
The error is: System.StringException: Invalid id: But I know that the u1.ID is valid because of the debug log.
Sonam_SFDCSonam_SFDC
The code looks correct: 

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?
karol.freebergkarol.freeberg
No error messages in the code. Below is the complete code. (Well as it is now anyway.) 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 = ' + u1.id); If (u1.Id != '')sr.Product_Manager_1__c = u1.Name; Update sr; system.debug('WE FINISHED!!!!! '); } //End PM1 if /* If (PM2 != null ){ User u2 = [Select ID, Name From User Where Name = :PM2]; If (u2.Id != '')sr.Product_Manager_2__c = u2.id; } //End PM2 if If (PM3 != null ){ User u3 = [Select ID, Name From User Where Name = :PM3]; If (u3.Id != '')sr.Product_Manager_3__c = u3.id; } //End PM3 if If (PM4 != null ){ User u4 = [Select ID, Name From User Where Name = :PM4]; If (u4.Id != '')sr.Product_Manager_4__c = u4.id; } //End PM4 if If (PM5 != null ){ User u5 = [Select ID, Name From User Where Name = :PM5]; If (u5.Id != '')sr.Product_Manager_5__c = u5.id; } //End PM5 if If (PM6 != null ){ User u6 = [Select ID, Name From User Where Name = :PM6]; If (u6.Id != '')sr.Product_Manager_6__c = u6.id; } //End PM6 if If (PM7 != null ){ User u7 = [Select ID, Name From User Where Name = :PM7]; If (u7.Id != '')sr.Product_Manager_7__c = u7.id; } //End PM7 if If (PM8 != null ){ User u8 = [Select ID, Name From User Where Name = :PM8]; If (u8.Id != '')sr.Product_Manager_8__c = u8.id; } //End PM8 if */ } // End for looping of changed product records }
karol.freebergkarol.freeberg
Gees, a readable version of the code. Note that the sr.product_manager_1__C field is a lookup relationship to the user but the pm1 = p.product_manager__c; field is only the user name, not a lookup relationship.

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
/*
karol.freebergkarol.freeberg
This is the heirachy:

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.)
karol.freebergkarol.freeberg
Here is my debug code at the point the error occurrs: (Note: it is a valid ID.)

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:
karol.freebergkarol.freeberg
OK, it was the IF statement. Cannot compare u1.id != ""!

If (u1.Id != '')sr.Product_Manager_1__c = u1.ID;

changed it to:

If (u1 != null)sr.Product_Manager_1__c = u1.ID;
This was selected as the best answer