+ Start a Discussion
PrazPraz 

System.StringException: Invalid id

I am passing this url

 

/apex/PosEntriesFilterPageExcel?id={!Account.Id}&soldBy={!utilityObject.soldBy__c}

 

 

and I am trying catch in the controller in the following way

 

 

String accountId=ApexPages.currentPage().getParameters().get('Id');
this.utilityObject.soldByValue__c=ApexPages.currentPage().getParameters().get('soldBy');

 

It is giving error in the second line

 

 

Yet I am receiving such exception. Any help will be appreciated

benwrigleybenwrigley

without fully knowing your object structure it's hard to say if this is right, but my guess would be that:

 

you are passing in the value of utilityObject.soldBy__c and trying to set it on  utilityObject.soldByValue__c.

 

Do these two different fields hold references to the same Object Type? Once looks like it might be a userid and the other decimal maybe?

 

 

PrazPraz

They are different instances in two different classes..ultityobject is being used for some utility purposes

mtbclimbermtbclimber

The exception is pretty clear. You are trying to assign a string to an ID typed property so Apex attempts a conversion. If the conversion fails this exception is thrown.

 

What value is being returned by this statement in your code?

 

 

ApexPages.currentPage().getParameters().get('soldBy');

 

 

PrazPraz

yeah it's a string value...can't we get any string or date type value in this?

PrazPraz

Can't we send anything other than Id?

mtbclimbermtbclimber

You can not assign an invalid ID to an ID field - either the primary key or a foreign key (relationship field). For example you can't set the AccountID field value on Contact to "Acme Corp" and expect the system to resolve it for you automatically.

 

If you want to establish a relationship with something that is not, in fact an ID (as it appears you want to do here) you have two choices:

 

  1. Query the respective object using your string value and get the related object ID
  2. Establish an External ID field on the related object and use foreign key upsert to establish the relationship.

In the case of the accountID example above if you had an external id field on account you could use something like "Acme Corp" to uniquely identify the related account without having to query for it.

 

If you're interested in exploring option 2, check out the API doc on the upsert operation here . And then the corresponding doc in the Apex Developer guide here.

 

Hope that helps.

PrazPraz

While solving  I found another peculiar problem

 

/apex/PosEntriesFilterPageExcel?id={!Account.Id}&soldBy={!utilityObject.soldBy__c}

Here is the code I am passing as usual

 

 

 

Here I am receiving.. and printing in debug

 

 

if(ApexPages.currentPage().getParameters().get('soldBy')!=null || ApexPages.currentPage().getParameters().get('soldBy')!='' || ApexPages.currentPage().getParameters().get('soldBy')!='null')
                
            else
                //this.utilityObject.soldByValue__c = null;
                this.soldBy = null;

system.debug(Logginglevel.INFO,'SoldBy value:#'+ApexPages.currentPage().getParameters().get('soldBy')+'#');
system.debug(Logginglevel.INFO,'SoldBy assigned value:#'+this.soldBy+'#');

 

 

 

ye the code is not going into this if logic or dynamic soql is getting malformed even though soldBy value is '' or null

ThukkaramThukkaram

If any of the passed id is null then one may get the above mentioned error. 

NithutsNithuts
Hai ,
just started learning coding .here is my coding .before update my custom customer object .I need to create new transaction  record and assign the old customer name to transaction name and i have lookup relationship b/w them .I am getting  below error .pls help me

Apex trigger updatecust caused an unexpected exception, contact your administrator: updatecust: execution of BeforeUpdate caused by: System.StringException: Invalid id: bibithaganesh: Trigger.updatecust: line 9, column 1

trigger updatecust on customer__c (before update) {
    list<transaction__c> test=new list<transaction__c>();
    list<customer__c> cust=new list<customer__c>();
    for( customer__c c:trigger.old)
    {
       
        transaction__c t=new transaction__c();
        t.name=c.name ;
        t.customer__c=c.name;
               test.add(t);
               
        
    }
   
    insert test;(do we need this line .. for before update?)
    
    
}