+ Start a Discussion
KitpithKitpith 

Error System.StringException: Invalid id: on String to Lookup Converion

Hello,

Could anybody help. 

I have one field where I am populating the Value from User Table and saving inside one lookup field. 

In the controller when saving this value in Lookup Field its giving the error as :

"Error System.StringException: Invalid id:"

 

Regards

Kit

jungleeejungleee

Hi,

 

Can you please paste that code snippet which is causing the issue?

 

-ಸಮಿರ್

vishal@forcevishal@force

Code please.

gbu.varungbu.varun

You are passing id of other object of look up field. You should pass id of related Look up object record

AnushaAnusha

You're assigning a empty string ('') to lookup field,  be sure that before assigning you need to check for null & empty string

KitpithKitpith

VF Code 

<apex:inputText id="fl_Tools_Tool_21" label="hello" value="{!lname}"/> 

 

the above line is responsible for the String to lookup conversion....

 

========================================

 

<apex:page standardController="User" extensions="toolExtension">
<apex:outputText id="NbLoginTool" value="21" rendered="true"/>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" >

function callonlookupone(val)
{
alert('check'+val.value);
callpicklistmethodone(val.value);

}

function callonlookup(val)
{
alert('check'+val.value);
callpicklistmethod(val.value);

}
function removeTool(idGet, idBut, idRem, fldTool, nbMax)
{
$('[id $="lTool22"]').css('display','none');
$('[id $="fl_Tools_Tool_22"]')="";
}
function loadLoginTool(idGet, idBut, fldTool, nbMax)
{
$('[id $="lTool22"]').css('display','block');
}
</script>
<apex:form id="FrmTools">
<apex:pageBlock >
<apex:pageBlockSection id="UserInfo" title="UserInformation">

 

</apex:pageBlockSection>

 

<br></br>
<br></br>
<apex:pageBlockSection id="BSRadio" title="Tools Request" columns="1">
<apex:selectRadio id="ToolsOption" value="{!ToolsOption}" layout="pageDirection" border="1" onclick="sesradio()">
<TABLE BORDER="1" style="width: 100%;">
<apex:selectOption itemValue="0" itemLabel="I have lost my login / password or my account is blocked" />
<apex:selectOption itemValue="1" itemLabel="Access rights for a new tool" />
<apex:selectOption itemValue="2" itemLabel="New plant ERP Access" />
<apex:selectOption itemValue="2" itemLabel="New Purchaser Group (SAP)" />

<script type="text/javascript">
function sesradio()
{
alert("He");
var myradios = document.getElementsByName('{!$Component.ToolsOption}');
for(i = 0; i < myradios.length; i++)
if(myradios[i].checked) {
alert('Selected value is: ' + myradios[i].value);
//LoginTool
if(myradios[i].value=="0") {
$('[id $="LoginTool"]').css('display','block');

}
else
{
$('[id $="LoginTool"]').css('display','none');
}
}}
</script>
</TABLE>
</apex:selectRadio>
</apex:pageBlockSection>

</apex:pageBlockSection>
<div id='LoginTool' style='display:none'>
<TABLE WIDTH="100%" BORDER="1" CELLSPACING="0" CELLPADDING="2">

<tr id='lTool21' >
<TD VALIGN="Top" COLSPAN="2" ><br></br>
<apex:selectList id="tools1" value="{!selectedTool}" size="1" title="tools" onchange="callonlookup(this)">
<apex:selectOptions value="{!tools}" ></apex:selectOptions>
<apex:actionFunction name="callpicklistmethod" reRender="lpanel" action="{!changepicklistvalue}">
<apex:param name="selectedTool" value="{!selectedTool}" assignTo="{!selectedTool}"/>
</apex:actionFunction></apex:selectList> </TD>
<apex:outputPanel id="lpanel" >
<!-- <TD VALIGN="Top" COLSPAN="2" ><br></br> -->
<apex:inputText id="fl_Tools_Tool_21" label="hello" value="{!lname}"/>


</TABLE>
</div>
<apex:pageBlockSection id="NbTool1" >
</apex:pageBlockSection>


<apex:pageBlockSection id="lbutton" >
<apex:commandButton action="{!saveData}" value="Submit"/>
<apex:commandButton action="{!CancelSave}" value="close" />
</apex:pageBlockSection>

 

</apex:pageBlock>

</apex:form>
</apex:page>

=======================================================================================

 

Apex Controller 

 

======================================================================================

 

public class toolExtension {
public RecordedTools__c Recordtools; //User sobject
public SavedRequest__c savedrequest ; // Request Object
public User userData;
public String selectedTool {get;set;}
public String selectedToolone {get;set;}
public String lname{get;set;}
public String lnameone{get;set;}
private Blob fileBody;

public String Manager{get;set;}

public String Name{get;set;}
// Public String titleDisplayRadio{get;set;}
public string ToolsOption {get;set;}

public String lookupvalue{get;set;}

public PageReference saveData ()
{
System.debug('Coming for Saving Data');
savedrequest=new SavedRequest__c();
savedrequest.UserName__c=userData.Name;
savedrequest.Manager__c=String.valueOf(userData.Manager);
savedrequest.fl_Tools_Tool_21__c=lname;  // This is the line which is sending data from String to Lookup Field.
System.debug('SelectedTool...'+selectedTool);
System.debug('ToolData..'+ToolsOption);
savedrequest.ToolsOption__c=ToolsOption;///data auto population
if(lnameone!='')
{
savedrequest.fl_Tools_Tool_22__c=lnameone;
}
insert savedrequest;
System.debug('Saved Data.....');
Pagereference pg = new Pagereference('/' +savedrequest.id);
pg.setredirect(true);
return pg;
}
public toolExtension(ApexPages.StandardController stdController) {
userData = (User)stdController.getrecord();
}

public void changepicklistvalueone()
{
System.debug('lookup val**'+selectedToolone);
ToolResponsible__c toolres = [SELECT p.User__c FROM ToolResponsible__c p where p.DTools__c =:selectedToolone];
User user=[SELECT p.Name FROM User p where p.id =:toolres.User__c];
//System.debug('Val***'+toolres.User__c);
System.debug('Name***'+user.Name);
lnameone = user.Name;
//System.debug "Values"+name;
// System.debug('Name'+name);
//String nametool='Kamal Pandey';
//Tool_21='Kamal Pandey';
System.debug('Name'+lname);
// return lname;
}


public void changepicklistvalue()
{
System.debug('lookup val**'+selectedTool);
System.debug('lookup val1'+ToolsOption);
ToolResponsible__c toolres = [SELECT p.User__c FROM ToolResponsible__c p where p.DTools__c =:selectedTool];
User user=[SELECT p.Name FROM User p where p.id =:toolres.User__c];
//System.debug('Val***'+toolres.User__c);
System.debug('Name***'+user.Name);
lname = user.Name;
//System.debug "Values"+name;
// System.debug('Name'+name);
//String nametool='Kamal Pandey';
//Tool_21='Kamal Pandey';
System.debug('Name'+lname);
// return lname;
}

}

========================================================================================

 

gbu.varungbu.varun

Hi Kitpitch,

 

You are using fl_Tools_Tool_21__C as a text field. First of all initialize memory to lname = '' and then test are you entering a valid id in this Textfield.

KitpithKitpith

Hello Varun,

I checked in apex code the Value is coming properly, not blank. only the conversion of string to Lookup is required.

The field is having the Name value like. Kit Pith (User Name)

Regards

Kamal

 

vishal@forcevishal@force

As per the code on Visualforce,

 

<apex:inputText id="fl_Tools_Tool_21" label="hello" value="{!lname}"/>

 

Here the user would enter a "String", something like "Kamal". 

 

But on the controller side, you can't assign any string to a look-up field, it has to be a record Id only.

 

For example, I have an Account - Name: Account1, Id = abcdefgh

Now, the lookup field to Account should have abcdefgh and not Account1.

 

Is this a lookup to Contact? 

Harshavardhan Sarrabu 4Harshavardhan Sarrabu 4

Hi kitpith did you found any solution for this because i am also facing the same issue. For me its contact object