You need to sign in to do that
Don't have an account?
Andrew 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.
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; } } }
Seems Like the issue lies in the comparison you are doing, you cant compare a Id with a String.
Replace:
with
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
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
Thanks
Shashikant
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
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