+ Start a Discussion

how to pass other objects values in trigger

Hi All,

I have requirement like below

1) I have trigger on case  ( Already existing)

2) based on business( custom field) that is there on case object....hard coding the record ids of product line id.

for example;

if case business==appliances
hardcoding the product line id on case object 
like case.prodcutlineid==09bfhdsgw1

Like this for 10 businesses hard coding is happening...

I want to get rid of this hard coding....with program logic

like select the values(product line id's) from product line object based on the value of business received from case object...and pass it to case.productline(get(id)).......some thing like this to avoid hardcoding......so that it will pick up the product line id from prodcut line object based on the business ...

can you please help to get this done thru code...


Adnubis LLCAdnubis LLC
You are not able to pass other object values directly into a trigger but you are able to query for additional information in a trigger. That being said, you need to be careful when querying from inside a trigger because you are limited to 100 Queries and if you include a trigger in a for loop you can kill your process rather quickly. I will provide two links at the end of this response that explain governor limits and trigger practices a bit more. To answer your question though you would do something like this.

set<String> businesses = new set<String>();
for(Case c : trigger.new) {

list<ProductLine> productLines = [SELECT id,Business FROM ProductLine WHERe Business in :businesses];
map<String,ProductLine> productLineMap = new map<String,ProductLine>();
for(ProductLine pl : productLines){

for(Case c : trigger.new) {
    c.productline = productLineMap.get(c.business);

This assumes that each business is associated to only one productline. Some adjustments would be made if you needed to bring back multiple product lines for a single business. If you need more help just let me know. Also, providing some more code might help.