• Kyle Barenberg
  • NEWBIE
  • 0 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies
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.