+ Start a Discussion
sfdclearnsfdclearn 

Adding product as look up field in the Account

we have a workflow rule in the account object that triggers an email alert when 

Account status='new'

But i would like to add the product name in this to workflow rule.

As i can't directly include the product name from the account as each account has multiple opportunities and each opp has multiple products, i am thinking of creating a look up of product in the account(in this case i guess that can only be one product

mapped which can be populated with a mass update)

 

any best solutions for this!

 

Thanks!

 

Madhan Raja MMadhan Raja M

Follow the below steps to populate Product name in your Workflow:

 

1. Create a 'Product Name' text field in Account

2. Create a trigger that should do the following:

     a. Fetch all the Opportunity that are associated with Account

     b. Fetch all the Product that are associated with Opportunity

     c. Fetch all the Product name and update it in the Account's 'Product Name' text field.

3. Include 'Product Name' in your email template.

4. This email template should be triggered when the Account status='new'.

 

Madhan Raja M

sfdclearnsfdclearn

Do you have a sample trigger pls?

sfdclearnsfdclearn

Also is this trigger to be coded in opp.lineitem?

sfdclearnsfdclearn

also this trigger only works in the opportunity product edit save rite , so how about existing records?

Madhan Raja MMadhan Raja M

I don't have a sample code now. This trigger should be written in Account object and should trigger when an Account status = New.

 

Madhan Raja M

sfdclearnsfdclearn


I wrote this trigger. 

 

trigger productname on Account(after insert,after update)
{
   
    
        List<Account> lstAccounts = [Select ProductNameCopy__c from Account];
        List<Id> AccIds =new List<id>();
        for(Account a:lstAccounts)
        {
         AccIds.add(a.Id);
         }
         system.debug(1);
        List<Opportunity> lstopptys = [SELECT Id, Name FROM Opportunity WHERE AccountId IN : AccIds];
        List<Id> oppIds = new List<Id>();
        for(Opportunity o : lstopptys)
        {
           oppIds.add(o.Id);
         }
        system.debug(2);
        List<OpportunityLineItem> lstopptylineitem  = [SELECT Id, PricebookEntry.Product2.Name FROM OpportunityLineItem WHERE OpportunityId IN :oppIds];
        
        for(Account objAccount : trigger.new)
            {
               for(Opportunity objoppty : lstopptys)
               {
                for(opportunitylineitem objopplineitem : lstopptylineitem)
                {
                if(objAccount.AccountStatus__c=='New')
                {
                objAccount.ProductNameCopy__c=objAccount.productNameCopy__c + objopplineitem.PricebookEntry.Product2.Name;
                system.debug(3);
               
                }
                 }
                
         }
          update objAccount;
         }
         }

 

It throws the following error when i edit, save the account record.

 

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger productname caused an unexpected exception, contact your administrator: productname: execution of AfterUpdate caused by: System.FinalException: Record is read-only: Trigger.productname: line 29, column 1

 

Any idea?

 

Thanks!