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
MaxiMizeMaxiMize 

Apex Trigger Skipping Records

I'm hoping to deploy a trigger rather soon that will update a Custom Object Master/Detail relationship by putting the correct Account on the record.  It works on some inserts, and not on others.

 

Here's what should happen:  When the API sends over a collection of up to 200 Ordered Ad records, the trigger should fire (Before Insert) and associate the Ordered Ad with the proper Account.  If no account is in the system, it should assign it to the placeholder account, "Unknown Account".  For reasons I'm not clear about, it isn't associating some records with the right account.  I can manually create a record, and it *always* associates with the right account... On Batch insert, however, it's pretty hit and miss.

 

Would appreciate anyone taking a look at this and see if there's some obvious reason for this strange behaviour.

 

Thanks in advance.

 

 

trigger updateOrderedAdsAccount on Ordered_Ads__c (before insert) 
	
	{
        //Create record set of Account Numbers to tie to Contract Owners
        Set<String> acctnums = new Set<String>();
        MAP<String, Id> orderedAdsAcctRel = new Map<String, Id>();
        for(Ordered_Ads__c a:trigger.new)
        {
        	acctnums.add(a.Admarc_Number__c); 
        }
                
       //Iterate through Accounts and fine matches for ID based on Account Number
        for (Account u:[select id, AccountNumber from Account where AccountNumber != null AND AccountNumber in :acctnums limit 1])
        {
        	String acctnum = u.AccountNumber;
        	orderedAdsAcctRel.put(acctnum, u.id);
        }
           
        //Iterate through Accounts a and update according to user data from Map
        for (Ordered_Ads__c a:trigger.new)
        {
        	if (orderedAdsAcctRel.containsKey(a.Admarc_Number__c))
        	{
        		Id u1 = orderedAdsAcctRel.get(a.Admarc_Number__c);
        		a.Account__c = u1;
        	}
        	else
        	{
        		a.Account__c = '0014000000Efgve';
        	}
        }
    }

 

 

Best Answer chosen by Admin (Salesforce Developers) 
gtindugtindu

Perhaps you should remove the LIMIT 1 from your for loop iterator

 

 

   for (Account u:[select id, AccountNumber from Account where AccountNumber != null AND AccountNumber in :acctnums limit 1])
        {
        	String acctnum = u.AccountNumber;
        	orderedAdsAcctRel.put(acctnum, u.id);
        }

 

 

All Answers

gtindugtindu

Perhaps you should remove the LIMIT 1 from your for loop iterator

 

 

   for (Account u:[select id, AccountNumber from Account where AccountNumber != null AND AccountNumber in :acctnums limit 1])
        {
        	String acctnum = u.AccountNumber;
        	orderedAdsAcctRel.put(acctnum, u.id);
        }

 

 

This was selected as the best answer
MaxiMizeMaxiMize

Yep... I'm embarassed that I missed that... Had put it in there for some testing early on.  Somehow, I got lost in the forest and forgot it was there.

 

Thanks for the help.  1000 points for the solution!  :-)