+ Start a Discussion
MikeyOMikeyO 

Account Trigger

Hello,
I have a number field on my account object that if greater than 1, I want it to change the account type to "Account Active"
Account Active is currently an option under the picklist field = Type.

What I am currently doing manually is running a report that shows customers with number of subscriptions. If I see one that has 1 or more subscription I have to go and change the account type to "Active Customer" currently it just stays as "Active Prospect" which is the default field.
Is this possible to automate with a trigger?
thank you-
pconpcon
Yes, this is something you can easily automate with a trigger.  You'll just want to write a before trigger that looks at the number field and and update the tye on the account.
MikeyOMikeyO
Thank you pcon.
I'll try to learn how to write triggers, I'm kinda new to the whole developer side of SFDC.
Any suggestions as to good material to read/study in order to learn how to write triggers?
pconpcon
I've written a blog post the break it down [1] and there are some very good resources over at sfdc99.com [2]

If this answers your question, please choose this (or another answer) as the best answer so that it can help others and so that it is remove from the unanswered queue.

[1] http://blog.deadlypenguin.com/blog/2014/07/23/intro-to-apex-auto-converting-leads-in-a-trigger/
[2] http://www.sfdc99.com/beginner-tutorials/
MikeyOMikeyO
Great, I've been checking out SFDC99 by David Liu and I'll most definitely check out your Blog.
Thank you again for your help!
Ravi Dutt SharmaRavi Dutt Sharma
Hey Mikey,
Something like this should work for your case
trigger ChangeType on Account (before update) {   
   for(Account acc:Trigger.new){
       if(acc.Number_of_Subscriptions__c>1 && acc.Type!='Client'){
           acc.Type='Client';
       }
   }
}

 
Samadhan Sakhale 3Samadhan Sakhale 3
Hey MikeyO,
there is two sites to learn how to trigger and to write test class
http://www.sfdc99.com/
http://www.salesforcetutorial.com/
 if you like this websites give a thumb for answer
thanks,
Sam
MikeyOMikeyO
@Ravi
I tried what you gave me with a few substitutions to match my fields, but its not working. Any thoughts??


trigger ChangeType on Account (before update) {  
   for(Account acc:Trigger.new){
       if(acc.Active_Subscriptions__c>1 && acc.Type!='Customer - Active'){
           acc.Type='Customer - Active';
       }
   }
}
Ravi Dutt SharmaRavi Dutt Sharma
Ideally it should work, I cannot see anything wrong in it. Just check few things:
1) 'Customer - Active' - is this value present in Type picklist
2) Is your trigger active?
3) Add one more event - (before insert,before update)
 
Amit Chaudhary 8Amit Chaudhary 8
Hi Mikey

Please try below code :-


trigger ChangeType on Account (before update,before insert) 
{  
   for(Account acc:Trigger.new)
    {
       if(acc.Active_Subscriptions__c>1 && acc.Type!='Customer - Active')
       {
           acc.Type='Customer - Active';
       }
   }
}

NOTE :- Make sure Type picklist should contain "Customer - Active" picklist value