You need to sign in to do that
Don't have an account?
Chitral 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.
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.
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
{
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
{
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
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
}
}
}
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.
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
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.
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
}
}
}
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.
{
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
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)
{
}
}
}
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();
}
}
}
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