+ Start a Discussion
SFDC Coder 8SFDC Coder 8 

Auto number based on picklist value

Hi All,
I have one custom as Student__c. I have one custom picklist as Status__c and values for Status__c are New,Joined. Default value for Status__c is New.
I have one more field as Total_Students__c (Number Data type).
After creating first record, if Status__c is changed to Joined then I want to auto fill Total_Students__c = 1.
After creating second record, if Status__c is changed to Joined then I want to auto fill Total_Students__c = 2.
Like this I want auto number fill for  Total_Students__c when ever Status__c is changed to Joined.

Please suggest me how can I do this?

Thanks in Advance

 
Best Answer chosen by SFDC Coder 8
Laxman Vattam 26Laxman Vattam 26
I couldnt see any options to save Total_Students__c ​with config. You can achieve it thru trigger as below
trigger StudentTrigger on Student__c(before insert, before update) { 
             if(Trigger.old[0] != 'Joined' && Trigger.new[0] == 'Joined'){
                   integer Count = [Select Count() from Student__c where Status__c = 'Joined'];
                   Total_Students__c = Count +1;
              }
 }

 

All Answers

Laxman Vattam 26Laxman Vattam 26
I couldnt see any options to save Total_Students__c ​with config. You can achieve it thru trigger as below
trigger StudentTrigger on Student__c(before insert, before update) { 
             if(Trigger.old[0] != 'Joined' && Trigger.new[0] == 'Joined'){
                   integer Count = [Select Count() from Student__c where Status__c = 'Joined'];
                   Total_Students__c = Count +1;
              }
 }

 
This was selected as the best answer
FearNoneFearNone
SFDC Coder 8,

What will happen if the status is changed from 'Joined' → 'New'/Other Status?
I suggest you create two custom objects, Master(ex. Room__c) and Child (Student__c)
then set Room__c.Total_Students__c field to Roll-Up Summary.

Good Luck!