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
Kyle BarenbergKyle Barenberg 

Trigger error: System.StringException: Invalid id because of older CRM data

Hello,

I have a trigger that is failing on integration from our database because of an older CRM Id. The field was re-purposed for SFDC uses but still contains the old CRM data. When an older record with one of these Id's is integrated over, it cannot match the Id to a valid record. The older Id's start with "AUBA-" so I attempted to say only records starting with '500'. However it's still erroring on the old Id number.

Here is the full error: "CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY:OrderTrigger: execution of BeforeInsertcaused by: System.StringException: Invalid id: AUBA-51M7HJExternal entry pointTrigger.OrderTrigger: line 12, column 1:--"

Can someone help me find where I'm going wrong here, and how I can allow the old Id to be passed through without firing off the Trigger?

Trigger:
trigger OrderTrigger on Order (before insert) {
    List<String> orderRequestIds = new List<String>();
    for (Order o : Trigger.new) {
        if (o.CRM_Sales_Order_Number__c != null && !o.CRM_Sales_Order_Number__c.startsWith('500')){
            orderRequestIds.add(o.CRM_Sales_Order_Number__c);
        }
    }
    if (Trigger.isBefore) {
        if (Trigger.isInsert) {
            OrderTriggerHandler.orderInsertHandling(Trigger.new, orderRequestIds);
        }
    }
}
Class:
public with sharing class OrderTriggerHandler {
    public static void orderInsertHandling(List<Order> scope, List<String> orderRequestIds) {
        List<Quota__c> rbmQuotas = [SELECT Id, Sales_Office__c, Quota_Date__c FROM Quota__c WHERE Active__c = true AND RecordType.DeveloperName = 'RBM_Quota'];
        List<Quota__c> salesGroupQuotas = [SELECT Id, Sgrp__c, Quota_Date__c FROM Quota__c WHERE Active__c = true AND RecordType.DeveloperName = 'ISC_Direct_Quota'];
        Map<Id,Case> orderRequests = new Map<Id, Case>([SELECT Id, CreatedById, CreatedBy.Region__c FROM Case WHERE Id IN :orderRequestIds]);


        for (Order o : scope) {
            Case relatedOrderRequest = orderRequests.get(o.CRM_Sales_Order_Number__c);
            if(relatedOrderRequest != null) {
                o.Related_Inquiry__c = relatedOrderRequest.Id;
                o.OwnerId = relatedOrderRequest.CreatedById;
            }

            for (Quota__c q : salesGroupQuotas) {
                if (q.Sgrp__c == o.Sales_Group__c && q.Quota_Date__c.month() == o.EffectiveDate.month() && q.Quota_Date__c.year() == o.EffectiveDate.year()) {
                    o.Sales_Group_Quota__c = q.Id;  
                }
            }

            /* for (Quota__c q : rbmQuotas) {
                if(relatedOrderRequest != null) {
                    if (q.Region__c == relatedOrderRequest.CreatedBy.Region__c && q.Quota_Date__c.month() == o.EffectiveDate.month() && q.Quota_Date__c.year() == o.EffectiveDate.year()) {
                        o.Quota__c = q.Id;
                    }
                }
            } */
            for (Quota__c q : rbmQuotas) {
                if (q.Sales_Office__c == o.Sales_Office__c && q.Quota_Date__c.month() == o.EffectiveDate.month() && q.Quota_Date__c.year() == o.EffectiveDate.year()) {
                    o.Quota__c = q.Id;
                }
            }
        }
    }
}

Thank you.
 
AnudeepAnudeep (Salesforce Developers) 

Can you add a debug statement in your code 
 
if (o.CRM_Sales_Order_Number__c != null && !o.CRM_Sales_Order_Number__c.startsWith('500')){
            orderRequestIds.add(o.CRM_Sales_Order_Number__c);
System.debug('orderRequestIds are' + orderRequestIds);
        }

And also after this line
 
o.Related_Inquiry__c = relatedOrderRequest.Id; 
System.debug('relatedOrderRequest is' + relatedOrderRequest.Id);

As far as I know this error occurs if you ID that you are assiging is not a valid Salesforce ID

The solution will be to validate the id in your class
 
// maybe a valid Account.Id, maybe not?
String theId = '001xxxxxxxxxxxx'; 
 
if ( MyUtilClass.isValidSalesforceID( theId, Account.class ) ){
    // do something if it is valid
}

See this blog for sample code

Let me know if this helps, if it does, please close the query by marking this answer as best. It may help others in the community. Thank You!
Maharajan CMaharajan C
Hi Kyle,

1. Based on my understanding you have to restrict the Old Id's to enter the trigger. But based on below you are restricting the valid salesforce Id's by using Not condition.
if (o.CRM_Sales_Order_Number__c != null && !o.CRM_Sales_Order_Number__c.startsWith('500'))

Remove the not from second and condtion if you want to allow only salesforce id's
if (o.CRM_Sales_Order_Number__c != null && o.CRM_Sales_Order_Number__c.startsWith('500'))

2. Put the debug log with finest mode for Apex Class then read the log carefully and find the error trace. 

Thanks,
Maharajan.C