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
Andrew Morales 1Andrew Morales 1 

Trigger error: System.StringException: Invalid id. External entry point (Creating Junction Records)

Hi All.

I am getting the following error on my trigger:
ERROR: AddOnTrigger: execution of AfterInsertcaused by: System.StringException: Invalid id: 9 External entry point
Any thoughts? Code is below. You can see my Objects are Contact(Standard), AddOn(Custom), Dev_AddOn(CustomJunction)

I am trying to write a trigger that will create the junction records from the Contact object and the AddOn object after the AddOn objects are created and edited. There are custom ID fields that I created as well so I will not be using the 

I am not a developer by trade and I got this far with the code because of the help from someone from the Community. Would appreciate any help in pointing me in the right direction. At this point, based on the error Im getting, I am not sure what next investigatory steps to take to rectify the code/problem.

Thank you very much for your time.
 
trigger AddOnTrigger on AddOn__c (after insert, after edit) {
    List<Dev_AddOn__c> aOBList = new List<Dev_AddOn__c>();
    
    
    Set<String> accIdSet = new Set<String>();
    
    for(AddOn__c obj: Trigger.new) {
        if(obj.ContactID_Values__c != null && obj.ContactID_Values__c != '') {
            List<String> accIdList = obj.ContactID_Values__c.split(',');
            accIdSet.addAll(accIdList);
        }
    }
    
    if(!accIdSet.isEmpty()) {
        Map<Id, Contact> accMap = new Map<Id, Contact>([Select Id, Name from Contact where Id IN: accIdSet]);
        for(AddOn__c obj: Trigger.new) {
            if(obj.ContactID_Values__c != null && obj.ContactID_Values__c != '') {
                List<String> accIdList = obj.ContactID_Values__c.split(',');
                
                for(String accId: accIdList)  {
                    if(accMap.get(accId) != null) {
                        Contact acc = accMap.get(accId);
                        Dev_AddOn__c aOB = new Dev_AddOn__c();
                        aOB.Name = obj.Name+' '+acc.Name;
                        aOB.AddOn__c = obj.Id;
                        aOB.Contact__c = acc.Id; 
                        aOBList.add(aOB);
                    }
                }
            }
        }
        
        if(!aOBList.isEmpty()) {
            insert aOBList;
        }
    }
}



 
Mahesh DMahesh D
Hi Andrew,

Seems Like  the issue lies in the comparison you are doing, you cant compare a Id with a String.

Replace:

 
if(obj.ContactID_Values__c != null && obj.ContactID_Values__c != '') {

with
 
if(obj.ContactID_Values__c != null) {

And also if it is a Id field we can't use the split also, convert it into String and use the Split function.

Regards,
Mahesh
Shashikant SharmaShashikant Sharma
Hi Andrew,

Issue seems that the ID that you are assiging is not a valid Salesforce ID. As you are fetching the ID by splitting the ContactID value it might be possible that it has string that is invalid so you need to validate it before assigning it to lookup fields.

See this : https://www.justinsilver.com/technology/salesforce/salesforce-sobject-id-validation-apex/

It has the code to validate ID Field. Use it in for loop before assigning it to lookup fields like below

for(String accId: accIdList)  {
   if( MyUtilClass.isValidSalesforceId( accId ) ) {
                    if(accMap.get(accId) != null) {
                        Contact acc = accMap.get(accId);
                        Dev_AddOn__c aOB = new Dev_AddOn__c();
                        aOB.Name = obj.Name+' '+acc.Name;
                        aOB.AddOn__c = obj.Id;
                        aOB.Contact__c = acc.Id; 
                        aOBList.add(aOB);
                    }
   }
}

Thanks
Shashikant
ASHOK RAJ R 18ASHOK RAJ R 18
Thanks @mahesh it works,,
RODRIGO DOMINGUESRODRIGO DOMINGUES
Hello this error is for example :  accMAP.get( 'invalid ID' )  - this cause a error of "Invalid ID"

On Your code you get a ID after one SPLIT on this line:    List<String> accIdList = obj.ContactID_Values__c.split(',');

can be then any values on this field ContactID_Values__c after split return blank

Sample  ' 1,2,3, '.split(',')  return array[ '1','2','3', ' ' ]

Sample2 without ultimate comma  ' 1,2,3 '.split(',')  return array[ '1','2','3' ]  - is more correct

One good Practice is verify the map before you call a get method.
ERROR  =  if(accMap.get(accId) != null) { Contact acc = accMap.get(accId);

CORRECT = if(  accMap.containsKEY(accId)  ) { Contact acc = accMap.get(accId);

Posting on 2021 kkk, but the answers is timeless. TKS
 
Suraj Tripathi 47Suraj Tripathi 47
Hi Andrew,

Greetings!

You can not check lookup field to blank.
Please only check not null.
because lookup field is always checking for a null.
If you check, lookup field with blank, then it will occur StringException - External entry point.
So please remove the blank check statement from if.

If you find your Solution then mark this as the best answer. 

Thank you!

Regards,
Suraj Tripathi