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
Chitral ChaddaChitral Chadda 

trigger to populate field for a particular picklist value

how do i Trigger on account (update)
if SLA__c  (which is a custom field in account , is  a picklist )  ....when its value is =' gold '
then
-count the number of phone numbers (in phone field )  in contacts (standard object )  and populate the total count of phone number (like 1,2 or 3) in count__c (custom object ) in account.

Best Answer chosen by Chitral Chadda
Ravi NarayananRavi Narayanan
trigger updateAccount on Account (before Update) {

List<Contact> Contactlist=[select id,name,phone,accountid from contact where accountID In :trigger.new AND phone!=NULL];
map<id,integer> countMap=new map<id,integer>();
integer i=1;
for(contact c:contactlist)
{
       if(countmap.containskey(c.accountid))
       {
           i=countmap.get(c.accountid)+1;
           countmap.put(c.accountid,i);
       }
       else
       {
              countMap.put(c.accountid,i);
       }
}

for(account a:trigger.new)
{
try{
        if(countmap.containskey(a.id))
            a.count__c=countMap.get(a.id);
   }
   catch(exception e)
   {
  
   }
}
}

All Answers

Vidhyasagaran MuralidharanVidhyasagaran Muralidharan
Trigger accountupdate on account(before update)
{
For(Account a : Trigger.new)
{
List<Account> a1 =[Select ID,Name from Account where accountname=:a.accountname];
If(a1.size>0)
{
If(a.SLA__C='gold')
{
List<Contact> c=[Select phonenumber from contact where accountname=:a.accountname];
If(c.size()>0)
{
a.count__c=c.size();
\\other fields to update
}
}
}
If(a1.size == 0)
{
\\No account exists
}
}
*Like this code will work like
if you have account name,SLA__C,Count__c as fields in account object.
if you try to update a record in account and you entered a account name and selected
picklist as gold,then this code will count no.of phone numbers of contacts and display the count in count__c field
Chitral ChaddaChitral Chadda
no its not working
Vidhyasagaran MuralidharanVidhyasagaran Muralidharan
what error you are getting?
Chitral ChaddaChitral Chadda
trigger accountupdate on Account (before update)
{
   for(account a :trigger.new)
   {
       list<account> a1=[select id,name from account where Name=:a.Name];  //  field label is acoount name  and field name  is Name
       if(a1.size>0)
       {
           if(a.SLA__c=='gold')
           {
               list<contact> c =[select phone from contact where AccountName=:a.AccountName];
               if(c.size>0)
               {
                   a.count__c=c.size();
               }
               }
           }
     
   if(a1.size==0)
   {
       //no account
     
    }     
}
     
     
}


//list<contact> c =[select phone from contact where AccountName=:a.AccountName];

here field label is accountname and field name is : account    (in contact object)
error: Invalid field AccountName for SObject Account


could you please tell me whats is the use of this name=:a.name
Vidhyasagaran MuralidharanVidhyasagaran Muralidharan
Try this
trigger accountupdate on Account (before update)
{
for(account a :trigger.new)
{
list<account> a1=[select id,name from account where Name=:a.Name]; 
if(a1.size>0)
{
if(a.SLA__c=='gold')
{
list<contact> c =[select phone from contact where Account=:a.Name];
if(c.size>0)
{
a.count__c=c.size();
}
}
if(a1.size==0)
{
//no account
}    
}    
}
Chitral ChaddaChitral Chadda
// list<contact> c =[select phone from contact where Account=:a.Name];
No such column 'Account' on entity 'Contact'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names.
 





Vidhyasagaran MuralidharanVidhyasagaran Muralidharan
Like you are using standard fileds of account and contact right?.you need say whether you are using standard field or custom field.
Standard fields:
Name is field name for account object.
Accounr is fieldname for account lookup field in conatct object

ist<contact> c =[select phone from contact where Account=:a.Name]; this will take the phone number from contact object for particular account name
Chitral ChaddaChitral Chadda
yes they are standard field :
name in account object
account in contact object 

why is the error coming then
// list<contact> c =[select phone from contact where Account=:a.Name];
No such column 'Account' on entity 'Contact'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names.
Vidhyasagaran MuralidharanVidhyasagaran Muralidharan
try this
trigger accountupdate on Account (before update)
{
for(account a :trigger.new)
{
list<account> a1=[select id,name from account where Name=:a.Name];
if(a1.size>0)
{
if(a.SLA__c=='gold')
{
contact c1 = new contact();
list<contact> c =[select c1.phone from contact where c1.Account=:a.Name];
if(c.size>0)
{
a.count__c=c.size();
}
}
if(a1.size==0)
{
//no account
}   
}   
}
Chitral ChaddaChitral Chadda
error
Didn't understand relationship 'c1' in field path. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names.
Vidhyasagaran MuralidharanVidhyasagaran Muralidharan
trigger accountupdate on Account (before update)
{
for(account a :trigger.new)
{
list<account> a1=[select id,name from account where Name=:a.Name];
if(a1.size>0)
{
if(a.SLA__c=='gold')
{
list<contact> c =[select phone from contact where Contact.Account=:a.Name];
if(c.size>0)
{
a.count__c=c.size();
}
}
if(a1.size==0)
{
//no account
}   
}   
}

just check this code and update me
Ravi NarayananRavi Narayanan
trigger updateAccount on Account (before Update) {

List<Contact> Contactlist=[select id,name,phone,accountid from contact where accountID In :trigger.new AND phone!=NULL];
map<id,integer> countMap=new map<id,integer>();
integer i=1;
for(contact c:contactlist)
{
       if(countmap.containskey(c.accountid))
       {
           i=countmap.get(c.accountid)+1;
           countmap.put(c.accountid,i);
       }
       else
       {
              countMap.put(c.accountid,i);
       }
}

for(account a:trigger.new)
{
try{
        if(countmap.containskey(a.id))
            a.count__c=countMap.get(a.id);
   }
   catch(exception e)
   {
  
   }
}
}
This was selected as the best answer
Chitral ChaddaChitral Chadda
why do we have to use map in case of update trigger to avoid recursion and for what all reason ?
like this is the code final:

trigger accountupdate on Account (before update)
{
   map<Id,account> accIdSet=new map<Id,account>();
  list<account> a = [select id , name ,SLA__c,(select phone from contacts where phone!=null) from account where id in : trigger.newMap.keyset() ];
  for(account aa :a )accIdSet.put(aa.Id,aa);
  system.debug('a.size() : '+ a.size());
      for(account acc : trigger.new)
         {
           if(accIdSet.keyset().contains(acc.Id)){
           system.debug('acc.SLA__c : '+ acc.SLA__c);
             if(accIdSet.get(acc.Id)!=NULL )
             {
              acc.count__c =accIdSet.get(acc.Id).contacts.size();
             }
           }
         }
Ravi NarayananRavi Narayanan
map is being used to find the number of contacts with phone number for a particualr account. it will have accountid and the total number of contacts that have for that account
Ravi NarayananRavi Narayanan
there are no chances of recursion in above trigger. 
Ravi NarayananRavi Narayanan
if the above code works, mark it as best answer. 
Darren Smith 48Darren Smith 48
I am new to coding and I know this can be done with a workflow rule, but would like to see what it looks like when coded. 
On the opportunity there is a picklist field called Review__c. Values are Queued, Reviewed and Completed. When queued is selected, the TimeStamp__c (date/Time field) needs to populate. How would the syntax look? 
Thanks veyr much