I want to insert some records, however i am not able to insert the records. I am getting the exception:
11:13:27.559
(1559754000)|SYSTEM_METHOD_ENTRY|[57]|MAP>.containsKey(Object)
11:13:27.559
(1559819000)|EXCEPTION_THROWN|[EXTERNAL]|System.StringException:
Invalid id: 10356253
Please help what is going wrong. Here is my code.
trigger OrderHistory on Sales_Order__c (after insert)
{
String s2 = String.valueOf(Trigger.New[0].Name);
String s1 = s2.mid(2,8);
List < OpportunityLineItem > o1 = [Select Sales_Order__c, OpportunityId, ID_Opp_Product_18__c from OpportunityLineItem where Sales_Order__c in : s1];
//Map with Key: Sales_Order__c and Value: List of OpportunityLineItem
Map< ID, List< OpportunityLineItem >> map_records = new Map< ID , List< OpportunityLineItem >>();
List < OpportunityLineItem > tempo1 = new List< OpportunityLineItem >();
for(OpportunityLineItem o : o1)
{
if(map_records.containsKey(o.Sales_Order__c))
{
tempo1 = map_records.get(o.Sales_Order__c);
tempo1.add(o);
map_records.put(o.Sales_Order__c , tempo1);
}
else
{
tempo1 = new List< OpportunityLineItem >();
tempo1.add(o);
map_records.put(o.Sales_Order__c , tempo1);
}
}
List< OpportunityLineItem > o1TBU = new List< OpportunityLineItem >();
for (Sales_Order__c so : Trigger.new)
{
String s3 = String.valueOf(so.Name);
String s4 = s2.mid(2,8);
if(map_records.containsKey(s4))
{
for(OpportunityLineItem o2 : map_records.get(s4))
{
if(o1!=null)
{
o2.Order_History__c = so.Id;
o1TBU.add(o2);
}
}
}
}
update o1TBU;
}
Update
I tried using Map< String, List< OpportunityLineItem >
> however it is giving that mapping cannot be done over string, it needs an id.
Secondly, yes sales_Order__c
field isn't a lookup, so i tried using the OpportunityId field, but i am getting this in my debug logs:
1
4:48:08.776
(776937000)|SYSTEM_METHOD_ENTRY|[82]|MAP>.get(Object)
14:48:08.777
(777416000)|EXCEPTION_THROWN|[EXTERNAL]|System.StringException:
Invalid id: 10356253 14:48:08.777
(777603000)|SYSTEM_METHOD_EXIT|[82]|MAP>.get(Object)
14:48:08.777 (777724000)|FATAL_ERROR|System.StringException: Invalid
id: 10356253
Here is my updated code:
trigger OrderHistory on Sales_Order__c (after insert)
{
String s2 = String.valueOf(Trigger.New[0].Name);
String s1 = s2.mid(2,8);
List < OpportunityLineItem > o1 = [Select Sales_Order__c,OpportunityId,ID_Opp_Product_18__c from OpportunityLineItem where Sales_Order__c =: s1];
System.Debug (s1);
//Map with Key: Sales_Order__c and Value: List of OpportunityLineItem
Map<ID, List<OpportunityLineItem>> OpportunityLineItemRecord = new Map<ID, List<OpportunityLineItem>>();
List < OpportunityLineItem > tempo1 = new List< OpportunityLineItem >();
for(OpportunityLineItem o : o1)
{
//System.Debug (o.Sales_Order__c);
if(OpportunityLineItemRecord.containsKey(o.ID_Opp_Product_18__c))
{
tempo1 = OpportunityLineItemRecord.get(o.ID_Opp_Product_18__c);
system.debug( tempo1);
tempo1.add(o);
OpportunityLineItemRecord.put(o.ID_Opp_Product_18__c , tempo1);
}
else
{
tempo1 = new List< OpportunityLineItem >();
tempo1.add(o);
OpportunityLineItemRecord.put(o.ID_Opp_Product_18__c , tempo1);
}
}
List< OpportunityLineItem > o1TBU = new List< OpportunityLineItem >();
for (Sales_Order__c so : Trigger.new)
{
String s3 = String.valueOf(so.Name);
String s4 = s2.mid(2,8);
// if(OpportunityLineItemRecord.containsKey(so.id))
// {
for(OpportunityLineItem o2 : OpportunityLineItemRecord.get(s4))
{
if(o1!=null)
{
o2.Order_History__c = so.Id;
System.debug( o2.Order_History__c);
o1TBU.add(o2);
}
}
}
// }
update o1TBU;
}
Best Answer
The Id Object can only contain a valid SalesForce Id value (15/18ch) of an SObject. Therefore, you cannot store an arbitrary String on it.
Try changing your map declaration to
Map< String, List< OpportunityLineItem >>
.