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
stollmeyerastollmeyera 

API Trigger only firing once when updating multiple records on a VF page

I have a VF page that allows my users to add and edit contacts and associate them with an Account all from one area.  One of the fields under that screen is a simple checkbox called "Map?".  If they click that button and save the page, a trigger fires in SF to create an XML file and send it over an API to create a profile in a third party software.  

 

The problem I am having is if a user has more than one contact on the VF page, whether they be adding two contact or working with two existing contacts (or any combination of that), the trigger only fires for one of the contacts.  Ex: I go to my VF page and I have two existing contacts.  I populate some required fields and click the "Map?" button for those two contacts.  I then click an Add New button and add a row for a brand new contact, while also clicking the "Map" button.  When I save the page, the two existing contacts are updated with "Map?" equal to true.  Additionally, the third contact is added and updated with "Map?" equal to true.

 

At this point I have a trigger in SF that, upon "Map?" equaling true, is supposed to populate an XML file and send it over an API to the third party program.  Even though all three contacts are updated with "Map?" equal to true, the trigger is only firing once.  It isn't erroring for the other two, it is just not firing at all for them...

 

I would be more than happy to provide the code, but I'd rather spare the space if anyone has some quick advice.  Thank you in advance.

Best Answer chosen by Admin (Salesforce Developers) 
SuperfellSuperfell

I'd guess that your trigger assumes it only gets one row, where-as it'll actually get all the rows (upto some limit) in one call, so make sure your trigger is looping over the trigger.new array, and not just processing trigger.new[0].

All Answers

SuperfellSuperfell

I'd guess that your trigger assumes it only gets one row, where-as it'll actually get all the rows (upto some limit) in one call, so make sure your trigger is looping over the trigger.new array, and not just processing trigger.new[0].

This was selected as the best answer
stollmeyerastollmeyera

Thanks for the reply, Simon.

 

From what I can tell, my trigger should run on all records (I am still a beginner, though).  I have pasted it below for reference.  

 

trigger CreateAccountOwners on Contact (after update, after insert) {
 
        List<Id> contactIds = new List<Id>();
        for (Contact c : Trigger.new){
        System.debug('bhak'+c.Create_Profile__c);
            // If the MAP Quote is submitted, add ID to list
            if((c.Create_Profile__c== True && Trigger.oldMap.get(c.Id).Create_Profile__c== false) || (Trigger.isInsert && c.Create_Profile__c == true))
                contactIds.add(c.ID);
        }
        
        if (contactIDs.isEmpty() == false){
            
            List<Contact> contactList= [Select c.Id From Contact c Where c.Id IN :contactIDs];
            UtilsContacts.createAccountUsingXML(contactIDs);
        }
}

 

I am having issues destinguising whether the issue is with the trigger or perhaps with the UtilsContacts class.

Andy BoettcherAndy Boettcher

stollmeyera,

 

You're right - your code (as I see it) is bulkified and should run with all records within a triggered action.  I would start by popping in some more System.Debugs both in this trigger and in your related class to see where the breakdown is.

 

Things like:

System.Debug('STARTING PROCESS 1');

(process 1)

System.Debug('ENDING PROCESS 1');

 

System.Debug('STARTING PROCESS 2');

(process 2)

System.Debug('ENDING PROCESS 2');

 

As they say in the banking industry - "Follow the money".  Follow your debug statements and you'll find out what the deal is.

 

-Andy


stollmeyerastollmeyera

Thanks for following up, techman.  My trigger is actually calling out to a class, which is only referencing the first row (shown below), just like SimonF had mentioned.  

 

public static void createAccountUsingXML(List<Id> contactIDs) {
        Document xmlDocument = getXMLDocument();
        if (xmlDocument != null) {
            String xmlBody = xmlDocument.Body.toString();
            System.debug('Raw Xml Document------------>'+ xmlBody);
            List<Contact> contactList= [Select FirstName,LastName,ID,Phone,Email,Question_Answer__c,AccountID,RoleID__c,SecondaryRoleID__c,Role__c,Secondary_Role__c From Contact where Id in :contactIDs];
            if (contactList != null && contactList.size() > 0) {
                Contact cont = contactList.get(0);

 


Unfortunately this class only works for a single record, meaning I am kind of out of luck.