+ Start a Discussion
Cris9931Cris9931 

Data Loader - Trigger problem

Hi, I created a trigger to populate a field on the Work Order Object.

SVMXC__SVMX_Event__c  = ServiceMax Event Object
SVMXC__Service_Order__c = Work Order Object

My trigger:

trigger secondTechnicianValidation on SVMXC__SVMX_Event__c (after update, after insert) {
    
    //store the Work Order id
    Set<ID> wo = new Set<ID>();
    
    for(SVMXC__SVMX_Event__c svmxEv : Trigger.New)
    {
        wo.add(svmxEv.SVMXC__Service_Order__c);

    }
    
    System.debug('woOrderId is: '+ wo);
    
    
    //-----------------Store the ServiceMaxEvents related to the current Work Order--------------------------------------//
    List<SVMXC__SVMX_Event__c> allEventsForAWo = [Select SVMXC__Service_Order__c, SVMXC__Technician__r.SVMXC__Salesforce_User__r.Email from SVMXC__SVMX_Event__c where SVMXC__Service_Order__c =: wo];
    System.debug('ServiceMax Events related to this Work Order are:  '+ allEventsForAWo );
    
    
    //----------------Store the emails of Technicians from these ServiceMax Events --------------------------------------//
    Set<String> emailTechniciansServiceEvents = new Set<String>();
    for(SVMXC__SVMX_Event__c smvxEvent : allEventsForAWo)
    {
         emailTechniciansServiceEvents.add(smvxEvent.SVMXC__Technician__r.SVMXC__Salesforce_User__r.Email);
    }
    System.debug('Emails of the technicians are: '+ emailTechniciansServiceEvents );
    
    // ---------------store the Work Order object which it needs to be updated with the emails of the technicians --------//
    List<SVMXC__Service_Order__c> storeWorkOrders  = new List<SVMXC__Service_Order__c>();
    for(Id woId : wo)
    {
        SVMXC__Service_Order__c order = new SVMXC__Service_Order__c();
        order.id = woId;
        storeWorkOrders.add(order);
    }
    System.debug('storeWorkOrders' + storeWorkOrders);
    
    //-------------------loop through each work order -----------------------------------------------//
    List<SVMXC__Service_Order__c> woToUpdate= new List<SVMXC__Service_Order__c>();
    for(SVMXC__Service_Order__c wos : storeWorkOrders){
         wos.TechniciansEmail__c = String.join(new List<String>(emailTechniciansServiceEvents), ', ');
         woToUpdate.add(wos);
    }
    System.debug('woToUpdate' + woToUpdate);
    
    //-------------------update the list-----------------------//
    if(!woToUpdate.IsEmpty()){
        update woToUpdate;
    }
}


Everything works fine with a single record when I edit the Service Max event... but... when I use data loader to mass update it doesnt work correctly.

This is my debug with a single record:

User-added image

Everything is fine as you can see.. Any idea why with data loader is not working correctly?

ShirishaShirisha (Salesforce Developers) 
Hi Cristian,

Greetings!

Can you please confirm,if you get any error while mass updating the records with the dataloader.Also,I would suggest you to bulkify the code as suggested in the below blog.

https://force-base.com/2016/02/03/how-to-bulkify-trigger-in-salesforce-step-by-step-guide/

Kindly mark it as best answer if it helps so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
Cris9931Cris9931
Hi, I don't get any error. The problem is that this field :
wos.TechniciansEmail__c

User-added image

I tried to explain here what I want. Basically for each work order we can have one or more servicemax events, these servicemax events have technicians which is a lookup to the User object.

Unfortunatelly in my case when I update multiple records in same time(data loader) this behavior is not right. 

What I get is for each work order is:

wos.TechniciansEmail__c = technician1@sf.com, technician2@sf.com, technician3@sf.com
and this is false as you can see in my picture, should not be the same technicians for each work order....