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
Craig Coates JWCraig Coates JW 

Invoking Apex from Process Builder - simultaneous threads?

Good evening SF Hive Mind

I find myself at a loss and in need of guidance.

I have the below invocable class which is fired by a Process in PB. It's in my sandbox on a delay of 2 mins after Email Message creation.

When a single email arrives, it works fine. But if two or more arrive in quick succession, it only fires for the first record and ignores the rest and I cannot figure out why. Are there any restrictions on how many processes can fire at once? Is there anything else why it would ignore the following records?
 
public class jwOrderAttachmentInvocable {
    @InvocableMethod
    public static void jwOrderPDFCloning(List<EmailMessage> emails) {

        Set<String> CaseIds = new Set<String>();
		Set<String> OrdIds = new Set<String>();
        //Set<String> MessageIds = new Set<String>();

        for (EmailMessage email: emails) {

                CaseIds.add(email.ParentId);
                //MessageIds.add(email.Id);

                System.debug('The CaseId is ' + email.ParentId);
            	system.debug('The MessageId is '+email.Id);
            	//system.debug('The MessageIds Set is '+MessageIds);
            


		if (!CaseIds.isEmpty()) {

                // find the Opportunity to which the Case relates
                Case op = [Select Id, PDF_Order_Number__c from Case where Id in: CaseIds]; {
                    if (op.PDF_Order_Number__c != null) {
                        OrdIds.add(op.PDF_Order_Number__c + '%');
                    }
                }
                if (!OrdIds.isEmpty()) {
				

            Set < String > OrdId = new Set < String > ();
            Order ord = [Select Id From Order Where ERP_Order_Number__c like: OrdIds ORDER BY LastModifiedDate DESC NULLS LAST LIMIT 1]; {
                OrdId.add(ord.Id);
            }
            System.debug('The OrderId is' + ' ' + ord.Id);
            if (!OrdId.isEmpty()) {

                List < Attachment > existing = [Select Id, ParentId, NAme, OwnerId, Body from Attachment where ParentId = :email.Id];
            system.debug('existing.size - ' + existing.size());
			
			if(!existing.IsEmpty()){
			
                Map < String, Attachment > MapOrderRefToId = new Map < String, Attachment > ();

                for (Attachment att : existing){
                MapOrderRefToId.putAll(existing);
                Integer attsize=MapOrderRefToId.size();
                system.debug('The Map size is '+attsize);
                system.debug('MapOrderRefToId keyset = ' + MapOrderRefToId.keySet());
            system.debug('MapOrderRefToId values = ' + MapOrderRefToId.values());
            	}
                List < ContentVersion > cvs = new List < ContentVersion > ();
                for (Attachment e: existing) {
                    if (MapOrderRefToId.ContainsKey(e.Id)) {

                        ContentVersion cv = new ContentVersion();
						cv.ContentLocation = 'S';
                        cv.PathOnClient = MapOrderRefToId.get(e.Id).Name;
                        cv.Origin = 'H';
                        cv.OwnerId = MapOrderRefToId.get(e.Id).OwnerId;
                        cv.Title = MapOrderRefToId.get(e.Id).Name;
                        cv.VersionData = MapOrderRefToId.get(e.Id).Body;
                        cvs.add(cv);
                    }
                    Database.SaveResult[] cvlist = Database.insert(cvs, false);

                    Set < Id > convsId = new Set < Id > ();

                    for (Database.SaveResult conv: cvlist) {
                        if (conv.IsSuccess()) {
                            convsId.add(conv.getId());
                            System.debug('Successfully inserted Content Version. CV ID: ' + conv.getId());
                        }

                        List < ContentVersion > ConVerList = [Select Id, ContentDocumentId from ContentVersion WHERE Id =: convsId];
                        List < ContentDocumentLink > cdList = new List < ContentDocumentLink>();
                    		for (ContentVersion con: ConVerList) {
                        		ContentDocumentLink cd = new ContentDocumentLink();
                        		cd.LinkedEntityId = ord.Id;
                        		cd.ContentDocumentId = [SELECT ContentDocumentId FROM ContentVersion WHERE Id in :ConVerList].ContentDocumentId;
                        		cd.ShareType = 'V';
                        		cd.Visibility = 'AllUsers';
                                cdList.add(cd);
								insert cdList;
							}
                    }
                }
            }
            //List < Case > Casestodelete = [Select Id from Case where Id in: CaseIds];
            //delete Casestodelete;
			}
        }
    }
        }}}