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
Sakti Sahoo 5Sakti Sahoo 5 

Issue on getting User Name in custom field.

Hi 

I have a requirement where to display User Name in Assigned To custom field available in my custom object.

My object is Incident__c. I have a field called Assigned_To__c, having lookup relationship with User object. I am fillingup the form and trying to save it. But its not getting save. In debug log, its showing following


(32921549)|FATAL_ERROR|System.StringException: Invalid id: Sakti Sahoo Class.IncGetParmAdminClass.SaveRecord: line 64, column 1 07:56:38.032 (32931039)|CODE_UNIT_FINISHED|IncGetParmAdminClass invoke(SaveRecord)


Page:
*********


<apex:page standardController="Incident__c" extensions="IncGetParmAdminClass" sidebar="false" showHeader="false">

<apex:form >

          <apex:pageBlock title="Please Review and Submit" mode="edit">

               <apex:pageBlockButtons location="bottom">               
                       <apex:commandButton value="Submit" action='{!SaveRecord}'/>               
               </apex:pageBlockButtons>

         <apex:pageBlockSection columns="2" title="Incident Details" showHeader="true" rendered="true" >
                 <apex:outputText value="Assigned To:  {!Assigned_To_Preview}"> </apex:outputText>
         </apex:pageBlockSection>
          
          </apex:pageBlock>
</apex:form>
</apex:page>


Apex Class:
**************


public class IncGetParmAdminClass
{
    
    public Incident__c param {get; set;}

    public String Assigned_To_Preview {get;set;}
  
    public ApexPages.StandardController standard_Controller;
    public IncGetParmAdminClass(ApexPages.StandardController standard_Controller)
    {
        param = new Incident__c();

        Assigned_To_Preview =   ApexPages.currentPage().getParameters().get('vAssigned_To');
        // Here vAssigned_To, I am getting it from another page URL

    }   
       
    
   public PageReference SaveRecord()
   {
      param.Assigned_To__c = Assigned_To_Preview;
      insert param;   
      return NULL;
    }
 
}




Thanks In advance.
SAKTI
Best Answer chosen by Sakti Sahoo 5
Praveen KHariPraveen KHari
Replace severecord Method with this code
 
public PageReference SaveRecord(){
    Id AssignedUserId =[Select Id From User Where Name =:Assigned_To_Preview LIMIT 1].Id;
    param.Assigned_To__c = AssignedUserId;
    insert param;       
    return null;
}

 

All Answers

Praveen KHariPraveen KHari
 
Hi Sakti,

As you have mentioned Assigned_To__c is Lookup relationship with User object and you are passing User Name to this filed while saving. You should [pass User ID while saving not user name. Either you pass user id from the other page and retrieve user details in IncGetParmAdminClass constructor. Or Query for user object by the name and use user object Id while saving Incident__c  object
 
-Praveen K Hari
Sakti Sahoo 5Sakti Sahoo 5
Thanks Praveen for your first response.
As per your suggestion, can you please help me out how to pass User Id during saving.
In my UI, the Assigned To filed is having lookup with user field and I have to get this user id dynamically before saving. Also one more thing while querying, I dont have the record id of my incident object.
Praveen KHariPraveen KHari
Replace severecord Method with this code
 
public PageReference SaveRecord(){
    Id AssignedUserId =[Select Id From User Where Name =:Assigned_To_Preview LIMIT 1].Id;
    param.Assigned_To__c = AssignedUserId;
    insert param;       
    return null;
}

 
This was selected as the best answer
Sakti Sahoo 5Sakti Sahoo 5
Thanks a lot Praveen . It works for me.
Just addition to one doubt over here. Whenever I want to query dinamically to fetch the field value, the I have to use this syntax like Select Id From User Where Name =:Assigned_To_Preview LIMIT 1.  Means the =: is for dynamic condition.

And what when I have to use Database.getQueryLocator(Select statement). In one of my case I am using this to fetch a set of records to display in my list view.

Could you please give me an idea of useing these two type of query syntax.

Thanks in advance.