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
SushupsiSushupsi 

System.StringException: Invalid id:

:smileysad: Hi all,

 

I am facing a problem regarding storing id in a lookup field.

 

I have to store contact ids in to lookup fields (via customised lookup) , my code is as follows :

 

Constructor is as follows : Public class class1{ public FinalCustomContact(Id acc) { newContact = ''; Id id = ApexPages.currentpage().getParameters().get('id'); rObj = (id == null) ? new caps__c():[SELECT accountname__c,name,central_contact__c from caps__c]; num = ApexPages.currentPage().getParameters().get('num'); contact = new List<contact>(); this.accname = acc; contact = [Select id,Name,LastName,Email from contact where contact.Account.id = :acc]; this.varname = null; this.idid = null; if(contact.size()==0){ msg1='No records were found based on your criteria'; } else{ } system.debug('--------------'+rObj.Central_Contact__c); //here the contact lookup displays null. } } My class from where im calling the above method which belongs to Class1: public class clss2{ ------- some code ----- Public class1 fObj{get; set;} public PageReference accountsel() { this.accname = cObj.accountname__c; system.debug('Account Name---:'+ cObj.accountname__c); fObj = new FinalCustomContact(cObj.accountname__c); return null; } if(fObj!=null && fObj.rObj != null){ system.debug('-------------'+'InsideCONTS'+fObj.rObj.Central_Contact__c); //here it displays empty. if(fObj.rObj.Central_Contact__c!='' ){ cObj.Central_Contact__c = fObj.rObj.Central_Contact__c; //here error is thrown saying Id is invalid. } } }

 

Urgent help is needed please help us on this issue. I have tried many workarounds but couldnt succeed

werewolfwerewolf

Are you actually passing an ID to that method, like "0033000000WDHZN"?  There's not enough information here to tell for sure, but my guess is that you are either trying to pass a name like "John Smith" into an Id variable or you're passing an empty string into an Id variable.  Neither are valid IDs -- only something like "0033000000WDHZN" is.

 

So check what you're passing in.

werewolfwerewolf

Also:

 

rObj = (id == null) ? new caps__c():[SELECT accountname__c,name,central_contact__c from caps__c]; 

That doesn't look right at all.  You're saying that if the ID is not null then you want to select _all_ the rows from caps_c?  There had better be only one row in caps_c or else your rObj object won't contain anything.  That may be your problem.

 

Try maybe  

rObj = (id == null) ? new caps__c():[SELECT accountname__c,name,central_contact__c from caps__c where Id=:id]; 

 

SushupsiSushupsi

I have been customising the Lookup for contacts : I have a constraint of populating contacts for an account which is chosen on my page. My page is as follows : This directly belongs to the class2: <apex:outputLabel for="Account Name" style="text-align:left;">Account Name</apex:outputLabel>&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; <apex:inputfield id="acclookup" value="{!cObj.AccountName__c}"/> <apex:commandButton value="ok" action="{!accountsel}" rerender="false"/> -------------- All the Backend processing is taking place in class1 and values are returned to class2 ----------------------- fObj is an instance of class1 which is instantiated on click of above 'ok' Command button. <apex:inputText id="theLookupText" value="{!fObj.assignvalue}"/>

 

IN THE INPUT HIDDEN I AM SAVING THE ID RETURNED FROM THE CLASS1 STOREVALUE METHOD

<apex:inputHidden id="CentralContactField" value="{!fObj.rObj.Central_Contact__c}"/> <apex:commandLink action="{!fObj.refreshlist}" rerender="searchPanel,theText,tblConts"> <apex:image id="lookupabc" url="{!$Resource.SearchImage2}" height="25" width="25" onclick="openpopUpwindow('{!$Component.lookupPanel}','{!$Component.lookup}')"/> </apex:commandLink> <!-- popup for lookupIcon--> <apex:outputPanel id="outerPanel"> <apex:outputPanel id="lookupPanel" style="background-color:White; border:1px solid black; width:600px; display:none;"> <apex:actionRegion > <apex:outputLabel ><b>Search</b></apex:outputlabel>&nbsp;&nbsp; <apex:inputText id="theText" value="{!fObj.search}" onkeypress="return handleKeyPress(event,'{!$Component.btn}');"/> <apex:commandbutton id="btn" value="Go!" action="{!fObj.go}" rerender="searchPanel"/>&nbsp; <apex:commandbutton value="New" action="{!fObj.new1}" rerender="searchPanel"/> <apex:commandButton id="cancelBtn" value="Cancel" onclick="return hidepopupwindow('{!$Component.lookupPanel}')" action="{!fObj.hidehide}"/> <apex:pageBlock id="searchPanel"> <apex:messages /> <apex:pageBlockSection title="Contacts"> <apex:outputPanel rendered="false" rendered="{!fObj.ren}"> <apex:pageBlockTable value="{!fObj.contact}" var="c" id="tblConts"> <apex:column headervalue="name" colspan="2"> Here in the page block table i am having a command link for the name of the contact which is retrieved from the controller class1. On clicking of this link the id is populated into the LOOKup field central_contact__c; <apex:commandLink id="cmdlink" action="{!fObj.storevalue}" value="{!c.Name}" rerender="theText,CentralContactField,theLookupText,theLookupText1" oncomplete="return hidepopupwindow('{!$Component.lookupPanel}')"> <apex:param value="{!c.id}" name="contid"/> <apex:param value="{!c.Name}" name="contName"/> </apex:commandLink> </apex:column> <apex:column headervalue="Email"colspan="2"> <apex:outputText value="{!c.Email}"></apex:outputText> </apex:column> </apex:pageBlockTable> </apex:outputpanel> </apex:pageBlockSection> </apex:pageblock>

 

 

The Controller part is as follows :

 

 

public class class2{ Public Id accname{get;set;} Public FinalCustomContact fObj{get; set;} public PageReference accountsel() { this.accname = cObj.accountname__c; system.debug('Account Name---:'+ cObj.accountname__c); /*Instantiating the class1 reference*/ -------here the parameterised constructor is invoked------------ fObj = new FinalCustomContact(cObj.accountname__c); return null; } ---------------IN CLASS2 I NEED TO SAVE THE VALUES RETURNED FROM CLASS ONE IN THE BACKEND FIELDS USING FOLLOWING METHOD-------------- Public void Conts() { -----------HERE i reference the value to be stored in the Centrel_Contact__C lookup---------- --------------HERE THE INVALID STRING ERROR IS THROWN REFERENCING TO fObj.rObj.Central_Contact__c!='' COMPARISION---------- ----------WHEN I CHECK IT PASSES ID ONLY AND NOT A STRING....---------- if(fObj!=null && fObj.rObj != null && fObj.rObj.Central_Contact__c!=''&&fObj.rObj.Central_Contact__c!= null){ cObj.Central_Contact__c = fObj.rObj.Central_Contact__c; } else{ cObj.Central_Contact__c = null; } } }

 

 

 

THE PROCESSING IS TAKING PLACE IN CLASS1 AS FOLLOWS:

 

Public class Class1 { public List<contact> contact{get; set;} Public Id accname; Public String textfieldid; Public String varname; Public String newContact{public get; public set;} public FinalCustomContact() { newContact = ''; Id id = ApexPages.currentpage().getParameters().get('id'); rObj = (id == null) ? new caps__c():[SELECT accountname__c,name,central_contact__c from caps__c where Id=:id]; num = ApexPages.currentPage().getParameters().get('num'); contact = new List<contact>(); ren = true; ren1 = false; ren2 = false; this.varname = null; this.idid = null; } ------------------------HERE ONCE THE ACCOUNT IS CHOSEN IN THE PAGE AND THE ACCOUNTSEL METHOD IS RUN IN CLASS2 THIS CONSTRUCTOR GETS INVOKED----------- public FinalCustomContact(Id acc) { newContact = ''; Id id = ApexPages.currentpage().getParameters().get('id'); rObj = (id == null) ? new caps__c():[SELECT accountname__c,name,central_contact__c from caps__c where id=:id]; num = ApexPages.currentPage().getParameters().get('num'); contact = new List<contact>(); this.accname = acc; contact = [Select id,Name,LastName,Email from contact where contact.Account.id = :acc]; this.varname = null; this.idid = null; if(contact.size()==0){ msg1='No records were found based on your criteria'; } else{ } -------------THIS IS DISPLAYED AS NULL SINCE ITS AN ID AND NOT A STRING------------------------------ system.debug('--------------'+rObj.Central_Contact__c); } /*Returning the Search Results to Table*/ public List<contact> getcontact(){ return contact; } Public id getaccname(){ return accname; } Public void setaccname(Id acc){ this.accname = acc; } Public Id giveawayid; Public Id idid; Public Id getdisplay(){ return giveawayid; } --------THIS METHOD RUNS WHEN THE COMMAND LINKED NAME IS CHOSEN TO POPULATE VALUE IN LOOK UP---------- public void storevalue() { giveawayid = ApexPages.currentPage().getParameters().get('contid'); search = ApexPages.currentpage().getParameters().get('contName'); system.debug('ID-----------------------'+giveawayid); newContact = giveawayid; system.debug('NewCONTACT-----------------------'+newContact); this.varname = search; this.idid = giveawayid; ---------HERE THE IS IS BEING ASSIGNED TO THE LOOKUP FIELD WHICH IS LATER REFERENCED IN CLASS2------------ rObj.Central_Contact__c = giveawayid; } public Id getididid(){ return idid; } public void setididid(Id srch){ this.idid=srch; } Public String getassignvalue(){ return varname; } Public void setassignvalue(String name){ this.varname = name; } } THE PROBLEM ARISES WHEN A VALUE IS NOT CHOSEN FROM THE LOOK UP AND I RUN THE VOID CONTS METHOD FROM CLASS2 WHICH IN TURN TRIES TO CHECK IF ID IS NOT NULL , BUT UNFORTUNATELY IT THROWS ERROR SAYING IT AS INVALID ID.... COULD YOU PLEASE HELP ME FINDOUT WHY THIS IS ARISING , FOR THE BEST OF MY KNOWLEDGE IM NOT EXPLICITLY ASSIGNING ANY VALUE WHEN THE CONTACT IS NOT CHOSEN FROM THE LOOKUP AT THAT TIME HOW DO I REFERENCE THE VALUE FROM THE CLASS1 ?

 

ANY HELP IS APPRECIATED...HOPE I FIND A WAY SOON.....
SushupsiSushupsi
FinalCustomContact is my class1 but i have forgotten to change its name as class1
JAckerman09JAckerman09

Hi Werewolf,

 

I know this is an old thread, but I am running into a similar issue, and it seems like you might know the answer.

 

I am in a similar situation, except that I want to pass an empty string to a lookup field in my test class (it must be blank in order to pass a validation test). How can I pass an empty string to the lookup field without running into

System.StringException: Invalid id:

 

 

Thanks in advance for any insight you can offer!