+ Start a Discussion
anvesh@force.comanvesh@force.com 

User Field value is not assigned ?

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);
}

}
bob_buzzardbob_buzzard
The problem here is that you are changing the records that you retrieved from the database via your query. You need to change the records in Trigger.new.

There's another issue here though - when you are inserting records, they won't have an id so the query won't return any values for the before insert case.
anvesh@force.comanvesh@force.com
I had taken list instead of Trigger.new because from the __r.id getting null value. So soql the querry to get the value
bob_buzzardbob_buzzard
Yes, the relationships will be null so you have to query, however that will only work for records that are already in the database.  For inserts, you only have the records you are passed in the trigger.

For updates, you can retrieve the related records, but you need to update the associated record from the trigger. You can get this by retrieving from trigger.newMap based on the record id.