function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
susheel1susheel1 

Need help to write a trigger to auto populate fields

      Need help to write a trigger to pre-populate 3 look fields. 2 of these fields come from the contacts and 1 is coming from the user. We need to deploy this by this Friday. Your help would be appreciated. I am new to programming
Message Edited by susheel1 on 08-19-2009 01:03 PM
Caleb_SidelCaleb_Sidel

A before insert trigger will let you update the record with your pre-populated values, but without knowing how the objects are related there's no easy code snippet.

 

Note that a trigger operates after the user clicks save so it's not so much a "pre-population" as a default value. To pre-populate data you'll have to hack the URL or use a visualforce page. 

 

Here's a before trigger 

 

trigger OpportunityTrigger on Opportunity (before insert) {

 

 

for(Opportunity oppty : Trigger.new) {

oppty.field1 = value;

                oppty.field2 = value;

                oppty.field3 = value; 

}

 

Good luck,

Caleb 

susheel1susheel1

Hi Caleb,

 

      Thanks for your help.

 

As you said I want the trigger to populate the field after the user clicks on the save button. I have 4 Fields. Plant,  Quality Lead (User), Plant Manager and ISC Director (both Contacts).  The Plant Manager and ISC Director need to receive email alerts.

 

Depending upon the plant I select the other 3 fields, which are lookup fields should be populated and I have the mapping for all the plants

for a Plant A, I know that Quality Lead should be X , Plant Manager should be Y and ISC Director should be Z

 

 

Now how do I start, I am new to this

Susheel

CaptainObviousCaptainObvious

I'm fairly new to Apex & Triggers myself, but here's the approach I would take:

 

First, put the 'mapping' into a new object.. call it "Plant Reference Table". All that is needed on this object are the Plant, Quality Lead, Plant Manager, and ISC Director fields (assuming Plant is unique). In this way, if anyone changes territory/leaves the company/etc, all you have to do is update the reference table and not the trigger!

 

The trigger would look something like this:

 

trigger populateLookups on CustomObject__c (after insert) { Set<String> Plants = new Set<String>{}; List<CustomObject__c> RecordsToUpdate = new List<CustomObject__c>(); for(CustomObject__c newRecord:Trigger.new){ if(newRecord.Plant__c!=null) { Plants.add(newRecord.plant__c); } } if(Plants.size() > 0){ //Retrieve Fields from the Plant Reference Table: Map<String,Plant_Reference_Table__c> PRTFields = new Map<String,Plant_Reference_Table__c>([ Select Plant__c, Quality_Lead__c, Plant_Manager__c, ISC_Director__c From Plant_Reference_Table__c Where Plant__c in:Plants]); //This is so we can look up the plant by a String: Map<String,Id> prtLocator = new Map<String,Id>(); for(Plant_Reference_Table__c pRt :[Select Id,Plant__c From Plant_Reference_Table__c Where Plant__c in:Plants]) { prtLocator.put(pRt.Plant__c, pRt.id); } //Loop through all the new records: for(CustomObject__c currentRecord : Trigger.new){ String plant = currentRecord.plant__c; Id prtId = prtLocator.get(plant); //Here's where the record gets 'populated': CustomObject__c rec2update = new CustomObject__c ( id = currentRecord.id, quality_lead__c = PRTFields.get(prtId).quality_lead__c, plant_manager__c = PRTFields.get(prtId).plant_manager__c, isc_director__c = PRTFields.get(prtId).isc_director__c ); RecordsToUpdate.add(rec2update); } update RecordsToUpdate; } }

 

CustomObject__c is the object you're trying to populate.

 

Hope that helps.

susheel1susheel1

Hi,

 

 

    Thanks for all the help, You have made things much simpler for me I will work on this trigger as you suggested.

 

 

Regards,

 

Susheel 

Caleb_SidelCaleb_Sidel

Try doing a before insert trigger instead of after as you can't call update from an after trigger on the object for which the trigger is written.

 

The below code should fail because you can't update CustomObject__c from a Trigger on CustomObject__c

 

tigger populateLookups on CustomObject__c (before insert) { Set<String> Plants = new Set<String>{}; List<CustomObject__c> RecordsToUpdate = new List<CustomObject__c>(); 

     //do stuff

update RecordsToUpdate; 

}

 

But you can eaily just do it before and there's no need to "update" anything as when the save completes the fields you've updated in the before trigger will be saved

 

 trigger populateLookups on CustomObject__c (before insert) {

Set<String> Plants = new Set<String>{}; for(CustomObject__c newRecord:Trigger.new){ if(newRecord.Plant__c!=null) { Plants.add(newRecord.plant__c); } } if(Plants.size() > 0){ //Retrieve Fields from the Plant Reference Table: Map<String,Plant_Reference_Table__c> PRTFields = new Map<String,Plant_Reference_Table__c>([ Select Plant__c, Quality_Lead__c, Plant_Manager__c, ISC_Director__c From Plant_Reference_Table__c Where Plant__c in:Plants]); //This is so we can look up the plant by a String: Map<String,Id> prtLocator = new Map<String,Id>(); for(Plant_Reference_Table__c pRt :[Select Id,Plant__c From Plant_Reference_Table__c Where Plant__c in:Plants]) { prtLocator.put(pRt.Plant__c, pRt.id); } //Loop through all the new records: for(CustomObject__c currentRecord : Trigger.new){ String plant = currentRecord.plant__c; Id prtId = prtLocator.get(plant); //Here's where the record gets 'populated': currentRecord.quality_lead__c = PRTFields.get(prtId).quality_lead__c; currentRecord.plant_manager__c = PRTFields.get(prtId).plant_manager__c; currentRecord.isc_director__c = PRTFields.get(prtId).isc_director__c; } }}