+ Start a Discussion
cnaranjocnaranjo 

SOQL and assigning values

Hello all,

 

I was wondering if this is possible with APEX code.

 

trigger NewCarEntry on Object (before insert) {
List<Object> newEntry = [SELECT id, Name FROM Object WHERE Color__c = 'Red'AND Model__c ='Seat'  LIMIT 1000];

//this is trigger and the list newEntry contains a number of items based Color__c and Model__c

for (Object o: Trigger.new) {
  if(*/here is where I want to know how to look in the newEntry list and if it finds a record that match the criteria in that list, to update a number of fields on that new record/*

 

I was wondering if this is possible. Create a list (SQOL Query)  based on a couple of fields and then use the IF statement to select those records that match the criteria on the list and update a number of fields on those records.

Can anyone help me with this?

 

Regards,

 

Carlos

Best Answer chosen by Admin (Salesforce Developers) 
Santosh KumbarSantosh Kumbar

Use of MAP<Key, Value> might help you.

 

trigger NewCarEntry on Object (before insert) {
map<string,object> newEntryMap = new map<name,object>();
//for example Name is taken as criteria 
for(Object obj:[SELECT id, Name FROM Object WHERE Color__c = 'Red'AND Model__c ='Seat' LIMIT 1000]){
newEntryMap.put(obj.Name, obj);
}
for (Object o: Trigger.new) {
if( newEntryMap.get(o.Name) != null) //
//newEntryMap.get(o.Name) - returns the name matching object record from newEntryMap.
}

 

Regards

San

www.santoshkumbar.com

All Answers

Santosh KumbarSantosh Kumbar

Use of MAP<Key, Value> might help you.

 

trigger NewCarEntry on Object (before insert) {
map<string,object> newEntryMap = new map<name,object>();
//for example Name is taken as criteria 
for(Object obj:[SELECT id, Name FROM Object WHERE Color__c = 'Red'AND Model__c ='Seat' LIMIT 1000]){
newEntryMap.put(obj.Name, obj);
}
for (Object o: Trigger.new) {
if( newEntryMap.get(o.Name) != null) //
//newEntryMap.get(o.Name) - returns the name matching object record from newEntryMap.
}

 

Regards

San

www.santoshkumbar.com

This was selected as the best answer
ViasurViasur

Thank you!

 

I will try that.

 

 

Regards,

 

 

Carlos