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
QuanchiQuanchi 

Trigger Help

Hello,   We just rolled out Salesforce, so my team is learning the system.

 

We have a custom object that lists multiple records connected to the Account object.  I need a trigger that updates a specific field on the Account object when certain criteria is met for a record on the child object.

 

So.....example

 

Object:   Account

Custom field:  Favorite ice cream

 

Child object:  List of favorite foods

custom fields: type of food, flavor, active

 

If type of food = ice cream and active = TRUE, then account.favorite_ice_cream = flavor

 

Hope I explained this right.......Any help would be appreciated.

 

Thanks in advance.

Shashikant SharmaShashikant Sharma

Here is your trigger 

trigger updateAccount on FavouriteFood__c (after insert, after update)
{
 Set<ID> setAccountID = new Set<ID>();
Map<ID , ID>     mapAccountFood = new Map<ID , ID> ();
for(FavouriteFood__c  ffood : trigger.new)
     {
               if(ffood.Account__c != null)
               {
                       setAccountID.add(ffood.Account__c);//Here we are cooleting all account ids
                       mapAccountFood.put(ffood.Account__c , ffood.id);//a map of foodId & accid 
               }
     }
List<Account> accToUpdate = [Select favorite_ice_cream From Account where id in:setAccountID];
for(Account a : accToUpdate )
{
  FavouriteFood__c  faivoriteFoodForAccount   = trigger.newMap.get(a.id);//retrived favorite food
  a.favorite_ice_cream  = faivoriteFoodForAccount.flavor;//assigned flavor
}

//updated account
update accToUpdate;

}

 I hope it expalins you

QuanchiQuanchi

I will give that a try.

 

Here is some more information:

 

Object:Account 
Account:Walmart 
Favorite_flavor_of_icecream__cVanilla 
   
   
   
Custom Object on Account:List_of_favorite_Foods 
   
Active__cType_of_food__cflavor__c
TRUEPizzaPepperoni
FALSEPizzaSauage
TRUEIce CreamVanilla
TRUEFruitApple

 

 

 

If List_of_favorite_Foods.Active__C = TRUE and List_of_favorite_Foods.Type_of_food__c = 'Ice Cream' then copy the value in List_of_favorite_Foods.flavor_c to account.Favorite_flavor_of_icecream__c

Shashikant SharmaShashikant Sharma

will wait for your results, please let me know if any issues.

QuanchiQuanchi

Almost got it.....how do I say:

 

If List_of_favorite_Foods.Active__C = TRUE and List_of_favorite_Foods.Type_of_food__c = 'Ice Cream'