Problem statement: External system is creating a custom object record and placing an attachment to the same record via composite REST API. After Insert trigger code that runs immediately for further processing the record is unable to fetch the attachments using SOQL at times (the behavior is inconsistent i.e., sometime the SOQL return the attachment, while other times it doesn't).
Question: Is the availability to attachment object records to SOQL query to be assumed as asynchronous in nature? (i.e. SOQL query needs to be run with 1 or 2 seconds of delay)
Let's assume that the external system is executing the following composite request:
{
"compositeRequest": [
{
"method": "POST",
"url": "/services/data/v52.0/sobjects/MyCustomObject__c",
"referenceId": "myRef1",
"body": {
"Field1__c": "Value1"
}
},
{
"method": "POST",
"url": "/services/data/v52.0/sobjects/Attachment",
"referenceId": "myRef2",
"body": {
"ParentId": "@{myRef1.id}",
"Body": "<base64 content here>",
"Name": "MyFile.json"
}
}
]
}
Apex After Insert trigger code that follows immediately would execute something as shown below:
Set<Id> idSet = new Set<Id>();
for(MyCustomObject__c obj: Trigger.new){
idSet.add(obj.Id);
}
List<Attachment> files = [SELECT id, Body FROM Attachment WHERE ParentId IN :idSet]);
The SOQL query in the above apex code doesn't fetch the attachments consistently. Sometimes, it returns nothing. I tried to execute this piece of code in async mode via future call, but I was able to observe the same behavior there as well.
I tried to mimic a delay of 1500ms in the apex code and then observed that the SOQL returns results consistently. I couldn't find any official documentation to support my doubts about attachments availability to be queried being asynchronous in nature.
Has anybody experienced similar problem? I'm not ruling out the possibility of Salesforce infrastructure problem or some problem to do with latest release.