6

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;

}

  • It appears that your Sales_Order__c field isn't a lookup, and thus isn't an ID data type. Change you make to using strings or integers as the key to match the data type. – ca_peterson May 16 '13 at 19:50
  • yes sales_Order__c field isn't a lookup, so i tried using the OpportunityId field, however, no luck!! Please check my code in the following answer. – abc May 17 '13 at 19:43
7

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 >>.

  • 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: – abc May 17 '13 at 19:13
  • Can you please add error messages? Maps cant contain any kind of information at both indexes and values, as long as keys are unique. It's unlikely that you HAVE to use Id data type as a key. – André Medeiros May 20 '13 at 16:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.