5

OK team, got a very odd one. If I specify a value in the field 'UnitPrice__c' when I insert a record I get the error: "UNKNOWN_EXCEPTION, invalid parameter value: []: " if I set it to a null value then there is no error. For reference the code which creates the record is below:

public static PurchaseOrderItem__c createPurchaseOrderItemFromOpportunityLineItem(OpportunityLineItem oppItem)
{
    system.debug('Creating Purchase Order Item for Opp Item: ' + oppItem);
    PurchaseOrderItem__c poItem = new PurchaseOrderItem__c();
    poItem.ProductId__c = oppItem.Product2Id;
    poItem.Quantity__c = oppItem.Quantity;
    poItem.UnitPrice__c = oppItem.UnitPrice;
    poItem.TaxRate__c = 20.00;
    poItem.OpportunityLineItemId__c = oppItem.Id;
    poItem.OpportunityId__c = oppItem.OpportunityId;
    system.debug('Returning Purchase Order Item: ' + poItem);
    return poItem;
}

So, the problem is with that field right? Well, I have deleted the field, re-created it, searched the entire codebase (workflows too) and removed all references to the field in case there was a workflow or other activity firing off from that field. I retried that, but still the same error.

I then disabled all triggers on that object and retried, same error.

I have even moved the method into the class which does the rest of the work, but to no avail.

So, there seems to be something very odd going on behind the scenes but Google and Sf.com support have come up with nothing, so I'm throwing it out there.

Can you help please?

Full class code below:

private static void createLinkPOItemsToOpportunityLineItems(Map<Id,OpportunityLineItem> oppItemMap,Map<Id,OpportunityLineItem> oldOppItemMap)
    {
        Map<Id,PurchaseOrderItem__c> poItemMap = purchaseOrderItem_handler.getPurchaseOrderItemsFromOpportunityLineItem(oppItemMap.values());
        system.debug(System.LoggingLevel.DEBUG,'Retrieved ' + poItemMap.size() + ' Purchase Order Items from the DB for the Opportunity Line Items');

        PurchaseOrderItem__c[] newPOItems = new PurchaseOrderItem__c[]{};
        Id[] poItemsToDelete = new Id[]{};

        boolean updateExistingPOItems = false;

        //move poItems to right PO, only if the PO has changed
        for(OpportunityLineItem oppItem:oppItemMap.values())
        {            
            //only process won line items
            if(oppItem.isWon__c)
            {                                
                system.debug(System.LoggingLevel.DEBUG,'Looking at Opp Item: '+ oppItem.Description);

                //if the Purchase Order Id is not the same as it used to be and there is a PO Item then we need to move the PO Item
                if(oppItem.PurchaseOrderId__c != oldOppItemMap.get(oppItem.Id).PurchaseOrderId__c && oppItem.SupplierId__c != null && oppItem.PurchaseOrderItemId__c != null)
                {    
                    system.debug(System.LoggingLevel.DEBUG,'Opp Item: '+ oppItem.Description + ' is linked Supplier: ' + oppItem.SupplierId__c + ' and has been moved to the PO: ' + oppItem.PurchaseOrderId__c);

                    //this check prevents opp items which have yet to be linked to a PO Item from being processed.
                    if(poItemMap.containsKey(oppItem.PurchaseOrderItemId__c))
                    {
                        system.debug(System.LoggingLevel.DEBUG,'POItem ' + oppItem.PurchaseOrderItemId__c + ' is linked to PO: ' + poItemMap.get(oppItem.PurchaseOrderItemId__c).PurchaseOrderId__c);
                        updateExistingPOItems = true;

                        poItemMap.get(oppItem.PurchaseOrderItemId__c).PurchaseOrderId__c = oppItem.PurchaseOrderId__c;
                    }
                }
                //if the Opp Item is not linked to PO Item we'll need to check there isn't already one or create one
                else if(oppItem.PurchaseOrderItemId__c == null && oppItem.SupplierId__c != null)
                {
                    //double check that there isn't a record still in the database.
                    for(PurchaseOrderItem__c poItem:poItemMap.values())
                    {
                        if(oppItem.Id == poItem.OpportunityLineItemId__c)
                        {
                            system.debug(System.LoggingLevel.DEBUG,'Opp Item: '+ oppItem.Description + ' was previously linked to PO Item: ' + poItem.Name + '.  We are linking it back to the existing PO Item');
                            oppItem.PurchaseOrderItemId__c = poItem.Id;
                        }
                    }

                    if(oppItem.PurchaseOrderItemId__c == null)
                    {
                        system.debug(System.LoggingLevel.DEBUG,'Opp Item: '+ oppItem.Description + ' was not previously linked to a PO Item.  We are creating a new PO Item');
                        //create a PO Item from an Opportunity Item,
                        PurchaseOrderItem__c poItem = purchaseOrderItem_handler.createPurchaseOrderItemFromOpportunityLineItem(oppItem);
                        poItem.PurchaseOrderId__c = oppItem.PurchaseOrderId__c;


                        for(Case c:triggerControls.caseOpportunities)
                        {
                            if(oppItem.OpportunityId == c.OpportunityId__c)
                            {
                                poItem.CaseId__c = c.Id;
                            }                        
                        }

                        newPOItems.add(poItem);
                    }
                }                
                //if there is a PO Item, but no supplier and no PO then delete the PO Item
                else if(oppItem.PurchaseOrderItemId__c != null && oppItem.SupplierId__c == null && oppItem.PurchaseOrderId__c == null)
                {
                    system.debug(System.LoggingLevel.DEBUG,'Opp Item: '+ oppItem.Description + ' is no longer linked to a Supplier or a PO so we are deleting it.');
                    poItemsToDelete.add(oppItem.PurchaseOrderItemId__c);
                }
            }
        }

        //updating PO Items with the new values of the opp items
        for(OpportunityLineItem oppItem:oppItemMap.values())
        {
            if(poItemMap.containsKey(oppItem.PurchaseOrderItemId__c))
            {
                PurchaseOrderItem__c poItem = poItemMap.get(oppItem.PurchaseOrderItemId__c);
                if(oppItem.Quantity != poItem.Quantity__c)
                {
                    system.debug(System.LoggingLevel.DEBUG,'Purchase Order Item: ' + poItem.Quantity__c + ' has changed.');
                    updateExistingPOItems = true;
                    poItem.Quantity__c = oppItem.Quantity;
                }
            }
        }

        if(updateExistingPOItems)
        {
            system.debug(System.LoggingLevel.INFO,'Existing PO Items have been updated.  Adding them to the list to be updated');
            newPOItems.addAll(poItemMap.values());
        }

        if(newPOItems.size() > 0)
        {
        system.debug('$$$');
            system.debug(System.LoggingLevel.INFO,'Inserting '+newPOItems.size()+'New PO Items, Updating Existing PO Items');
            system.debug(System.LoggingLevel.DEBUG,newPOItems);
            system.debug('####' + newPOItems);
            upsert newPOItems;
            system.debug('&&&' + newPOItems);
        }

        if(poItemsToDelete.size() > 0)
        {
            system.debug(System.LoggingLevel.DEBUG,'Deleting PO Items');
            database.delete(poItemsToDelete);
        }

        for(PurchaseOrderItem__c poItem:newPOItems)
        {
            if(oppItemMap.containsKey(poItem.OpportunityLineItemId__c))
            {
                system.debug(System.LoggingLevel.INFO,'Linking Opp Item: '+ oppItemMap.get(poItem.OpportunityLineItemId__c).Description + ' to the PO: ' + poItem.Name);
                oppItemMap.get(poItem.OpportunityLineItemId__c).PurchaseOrderItemId__c = poItem.Id;
            }
        }
    }
6

The issue was caused by the fact poItem is a child in a Master-Detail relationship and earlier in the code I was setting the CurrencyISOCode of the parent to NULL, which cascades to the child. As there was no CurrencyISOCode when I tried to set the Currency field's value I received an error.

0
2

Mixing decimal and currency values has caused some unexpected errors for me in the past. And I see that OpportunityLineItem.UnitPrice is a currency field.

There is a small chance your problem is similar. Try this work-around:

poItem.UnitPrice__c = oppItem.UnitPrice != null
        ? Decimal.valueOf('' + oppItem.UnitPrice)
        : null;
1
  • Thanks Keith, I tried that but no joy. Still getting same error. It's very odd. Issue has been escalated to Salesforce dev support. Thanks for your help. Will post resolution once I have one.
    – Weeden
    Nov 8 '14 at 18:20

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.