0

I have created a trigger.. contact is lookup. and i am assigning contact obj user value to current object.debug logs showing the value existed. but it is not assigned in record field.?

trigger NIGO_update on NIGOs__c(before insert,before update){

     List<NIGOs__c> NgList = [select id,name,Contact_Name__c,Product_Type__c,Contact_Name__r.Annuity_Territory_Wholesaler__r.id,Contact_Name__r.Annuity_Territory_Wholesaler__c,Contact_Name__r.Forecare_Territory_Wholesaler__r.id from NIGOs__c  where id in:Trigger.new];


     for(NIGOs__c ng:NgList){
        if(ng.Product_Type__c != 'Forecare'){
            if(ng.Contact_Name__c != Null){

                **ng.External_Wholesaler__c** = ng.Contact_Name__r.Annuity_Territory_Wholesaler__r.id;

            }
        }
        **System.debug('Ext1'+ng.External_Wholesaler__c);**
    }

}
4
  • Try debugging the fields where your conditions exist to make sure they match and/or are not null. Also before insert will not have an Id yet so you will not return the related records.
    – Jenny B
    Oct 30, 2015 at 11:26
  • check that Contact_Name__r. Annuity_Territory_Wholesaler__c != null
    – Ashwani
    Oct 30, 2015 at 11:56
  • yes , i have verified with valid policy record. and the value is shown in debug logs also Oct 30, 2015 at 12:09
  • System debug is outside of all if conditions. Please put debug inside conditions and see what you get.
    – Ashwani
    Oct 30, 2015 at 12:21

1 Answer 1

1

You are doing two things wrong:

Querying records that do not exist yet in the before insert

The query will never return any records when its a before insert because the NIGOs__c records have not yet been persisted to the database.

Updating the wrong record instances

When using before triggers you can update the records from Trigger.new without the need to do an update. This is not what you are doing. You are doing a SOQL query to get the NgList list and then updating those instances rather than the ones from Trigger.new. You could do something like the following to get the correct record to update

NIGOs__c nigoFromTrigger = Trigger.newMap.get(ng.Id);
nigoFromTrigger.External_Wholesaler__c = ...

but keep in mind that Id values are not populated in before insert triggers.

2
  • Thanks, but i am getting error like 'Trigger' variable is not existed from the line Trigger.newMap.get(ng.id)...I had taken list instead of Trigger.new because from the __r.id getting null value. So soql the querry to get the value Oct 30, 2015 at 12:53
  • I have updated the code snippet in my answer
    – BarCotter
    Oct 30, 2015 at 12:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .