I have an Apex Class:
public with sharing class Lookup
{
public static void findOpportunities(List<Custom_Object__c> records)
{
Set<Id> OppsIds = new Set<Id>();
for (Custom_Object__c record : records) OppsIds.add(record.custom_field__c);
Map<String, Opportunity> Opps = new Map<String, Opportunity>();
for (Opportunity Opp : [
SELECT Id, custom_field_2__c FROM Opportunity
WHERE custom_field_2__c IN : OppsIds
]) Opps.put(Opp.Name, Opp);
for (Custom_Object__c record : records)
if (Opps.containsKey(record.custom_field__c))
record.Opportunity__c = Opps.get(record.custom_field__c).Id;
}
}
And a trigger:
trigger UpdateOpportunityLookup on Custom_Object__c (after insert, after update)
{
if (trigger.isAfter)
{
if (trigger.isInsert)
{
Lookup.findOpportunities(trigger.new);
}
}
}
The OLIs are being sent from another system on this custom object, as related records. There is lookup field from the related records to the Opportunity object that I am trying to populate.
The Class is meant to query if the custom_field__c
on the custom_object__c
has the same value as custom_field_2__c
and populate the lookup with the correct Opportunity.
I have managed to save it successfully, but I am getting an error message:
Apex script unhandled trigger exception by user/organization / caused by: System.StringException: Invalid id: 895
External entry point Trigger.UpdateOpportunityLookup: line 8, column 1
and it stops the OLIs from being sent to SF.
Both fields are Text Data Type and contain numeric numbers.
The custom_field_2__c
on the Opportunity I am querying to have the same value as custom_field__c
is External ID = ticked.