+ Start a Discussion

Trigger not getting fired , help !!

Trigger not getting fired , using an object from managed package.

Trying to update fields in shipper record after insert from the fields of Sales order record.

here is the code, 

trigger UpdateContactInfo on rstk__soship__c (after insert) {
    System.debug('********trigger is called');
    rstk__soship__c[] shipments = Trigger.new;
    rstk__sohdr__c[] orders = null;
    for(rstk__soship__c shipment:shipments){
        orders = [Select    rstk__sohdr_contact__c, rstk__sohdr_conemail__c,rstk__sohdr_conphone__c 
                  from rstk__sohdr__c 
                  where rstk__sohdr_order__c =: shipment.rstk__soship_order__c ];
        System.debug('Here in the for loop shipment ordernumber '+shipment.rstk__soship_order__c);
        if(orders.size() > 0){
            System.debug('Found the order order name :'+orders[0].rstk__sohdr_contact__c+
                         'email: '+orders[0].rstk__sohdr_conemail__c+
                         'Phone :'+orders[0].rstk__sohdr_conphone__c);
            shipment.rstk__soship_contact__c = orders[0].rstk__sohdr_contact__c;
            shipment.rstk__soship_email__c = orders[0].rstk__sohdr_conemail__c;
            shipment.rstk__soship_phone__c = orders[0].rstk__sohdr_conphone__c;


Any suggestion would be very much appreciated. Thanks,
Magesh Mani YadavMagesh Mani Yadav

Hi Niyathi,

I am not sure why the trigger is not executed but i could see that in the for loop you were doing soql query.

Just try to make that query outside of the for loop and check if it works.

Hi Niyathi,

The trigger should fire after insert, however there is no dml statment that is done to actually update the record.
Hi - If this trigger is written for before insert, then no need to have explicit update dml statement. The same code will work if the trigger is for before insert and rstk__soship_order__c value is populated in before insert. However, since the trigger is on afterinsert, the updates will not happen unless the shipment variable is added to a list and update dml statement is explicitly mentioned for that list. But having another update on the same object can have recursive trigger issues. So its better to try this with before insert trigger. And as per other posts, best practice is to avoid SOQL query inside for loop.Thanks.
Also please confirm that the trigger is active.
The reason it is not working is -
1. AfterUpdate is being used and no DML in trigger hence no updated. any case where the trigger object is being updated always use before trigger unless we need the ID of new record.
Here is the updated code.
//use before insert context
//trigger UpdateContactInfo on rstk__soship__c (after insert) {
trigger UpdateContactInfo on rstk__soship__c (before insert) {
    System.debug('********trigger is called');
    //type casting required here
    //rstk__soship__c[] shipments = Trigger.new;
    List<rstk__soship__c> shipments = (List<rstk__soship__c>) Trigger.new;
    rstk__sohdr__c[] orders = null;
    //map to store orders, always use maps in all such cases. Tthis will make sure that you do not have
    //write logic inside loops and performace is enhanced.
    Map<String, rstk__sohdr__c> mapShippingOrders = new Map<String, rstk__sohdr__c>();
    //put all the rstk__soship_order__c in a set so that you can query orders against it.
    for (rstk__soship__c shipment : shipments) {
        //put null values for now
        mapShippingOrders.put(shipment.rstk__soship_order__c, null);
    for (rstk__sohdr__c orders : [Select rstk__sohdr_contact__c, rstk__sohdr_conemail__c, rstk__sohdr_conphone__c
                                  from rstk__sohdr__c
                                  where rstk__sohdr_order__c = : mapShippingOrders.keySet()]) {
        mapShippingOrders.put(orders.id, orders);
    //as the context is before insert, no need to explicitly mention Update DML
    for (rstk__soship__c shipment : shipments) {
        //this will fail if trigger has more than 100 records being processed.
        /*orders = [Select rstk__sohdr_contact__c, rstk__sohdr_conemail__c,rstk__sohdr_conphone__c
                  from rstk__sohdr__c
                  where rstk__sohdr_order__c =: shipment.rstk__soship_order__c ];
        System.debug('Here in the for loop shipment ordernumber ' + shipment.rstk__soship_order__c);
        //Added map method to check if data exists
        //if(orders.size() > 0){
        if (NULL != mapShippingOrders && mapShippingOrders.containsKey(shipment.rstk__soship_order__c)) {
            System.debug('Found the order order name :' + mapShippingOrders.get(shipment).rstk__sohdr_contact__c +
                         'email: ' + mapShippingOrders.get(shipment).rstk__sohdr_conemail__c +
                         'Phone :' + mapShippingOrders.get(shipment).rstk__sohdr_conphone__c);
            shipment.rstk__soship_contact__c = mapShippingOrders.get(shipment).rstk__sohdr_contact__c;
            shipment.rstk__soship_email__c = mapShippingOrders.get(shipment).rstk__sohdr_conemail__c;
            shipment.rstk__soship_phone__c = mapShippingOrders.get(shipment).rstk__sohdr_conphone__c;