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

updation failed an old values using trigger?

i have a picklist   "New_Brands_c" having some values on Bottle__c object...when we select a value from this picklist  Quantity field value should updated so  i written trigger like this.....it  works properly  for new values when i created a new record but   when i change this picklist value from existing record the quantity field is not  updated y?  means it not  works  with old values. i  tried with trigger.old still not working...please give me solution.

 

 

 

 

trigger Populatepicklistvalue on Bottle__c (before insert) {

for(Bottle__c b:trigger.new){
if(b.New_Brands__c=='kingfisher'){
b.Quantity__c=303;
}
else
if(b.New_Brands__c=='AirLines'){
b.Quantity__c=503;
}
else
if(b.New_Brands__c=='Fishyard'){
b.Quantity__c=703;
}
else
if(b.New_Brands__c=='BombayStack'){
b.Quantity__c=903;
}
}

}

Best Answer chosen by Admin (Salesforce Developers) 
Avidev9Avidev9

anvesh!

I guess you need to read about the trigger.old and trigger.new . The concept that you are having update = trigger.old and insert = trigger.new , is totally wrong.

 

Trigger.new holds all the new values that are being inserted/upated in the DB. So incase we are inserting records in DB the trigger.new will hold the values from the record. Incase we are doing an update , trigger new holds the values that are being updated and currently entered by user.

 

 

Now events : before insert : means trigger will fire before insertion of an records

                        before update : means trigger will fire before updation

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_context_variables.htm

 

So your trigger should work by just adding a event

 

trigger Populatepicklistvalue on Bottle__c(before insert,before update) {
    for (Bottle__c b: trigger.new) {
        if (b.New_Brands__c == 'kingfisher') {
            b.Quantity__c = 303;
        } else
        if (b.New_Brands__c == 'AirLines') {
            b.Quantity__c = 503;
        } else
        if (b.New_Brands__c == 'Fishyard') {
            b.Quantity__c = 703;
        } else
        if (b.New_Brands__c == 'BombayStack') {
            b.Quantity__c = 903;
        }
    }
}

 

 

 

All Answers

Satish_SFDCSatish_SFDC
Hi,
Firstly you have used just the before insert event on the trigger, which means the trigger code runs just during before insert and not during before Update.
So apart from using before insert, also use before update.

Also you mentioned that you used the Trigger.old variable. You cannot update the trigger.old context variable as it is readonly. You will have to update any values in the Trigger.new context variable.

Hope this helps.

Regards,
Satish Kumar
Please mark my answer as a solution if it was helpful so it is available to others as a proper solution.
If you felt I went above and beyond, please give me Kudos by clicking on the star icon.
Vinita_SFDCVinita_SFDC

Hello Anvesh,

 

You are using 'insert', please Note that trigger.old (returns a list of the old versions of the sObject records.) is only available in update and delete triggers.

Avidev9Avidev9

anvesh!

I guess you need to read about the trigger.old and trigger.new . The concept that you are having update = trigger.old and insert = trigger.new , is totally wrong.

 

Trigger.new holds all the new values that are being inserted/upated in the DB. So incase we are inserting records in DB the trigger.new will hold the values from the record. Incase we are doing an update , trigger new holds the values that are being updated and currently entered by user.

 

 

Now events : before insert : means trigger will fire before insertion of an records

                        before update : means trigger will fire before updation

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_context_variables.htm

 

So your trigger should work by just adding a event

 

trigger Populatepicklistvalue on Bottle__c(before insert,before update) {
    for (Bottle__c b: trigger.new) {
        if (b.New_Brands__c == 'kingfisher') {
            b.Quantity__c = 303;
        } else
        if (b.New_Brands__c == 'AirLines') {
            b.Quantity__c = 503;
        } else
        if (b.New_Brands__c == 'Fishyard') {
            b.Quantity__c = 703;
        } else
        if (b.New_Brands__c == 'BombayStack') {
            b.Quantity__c = 903;
        }
    }
}

 

 

 

This was selected as the best answer