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
System Administrator 393System Administrator 393 

Setting Values of a LookUp Field

I have a look up called Credit Rating and that is supposed to generate Ids such as AB4, CB5. I am writing an apex class that derives these IDS based on other nuerical fields. However, I am making a string and the string has to be assigned to the Id data type as the field Credit Rating is a look up field with data type Id. I tried to cast it like this but it keeps showing me this error
"System.StringException: Invalid id:  AD6()" , please help.
User-added image
Mrunali GaonkarMrunali Gaonkar

Since Credit Rating is a look up field, you should assign salesforce record id to that id field. 

I am assuming there is a string field X on Credit Rating which will have this values AD6, etc.
I dont know the exact requirement. So there are two possible ways.

1) You already have this  Credit Rating record with AD6 string.
    In this case, you can write a SOQL query after s= 'A' + 'D' ... line. 
List<Credit Rating API name> listCR = [Select id from Credit rating api name where X =: s  limit 1];
and then you can map this id  as follows:
    a.genesis_Credit_Rating__c = listCR[0].Id

2) There is no existing record and you have to create record at runtime and assign the id
In this case, you have to create a new record.
Credit Rating API name objCR = new Credit Rating API name(X=s, ....all the required fields);
insert objCR;
and assign this inserted record id to the a.genesis_Credit_Rating__c.

Hope this helps!