0

I need to update incremental code in a custom field on accounts. So that, If user changes the Type from Prospect to customer then no. should be incremented by 1.

For example:

  • For account1, it will be C1
  • For account2, it will be C2
  • For account3, it will be C3

Whenever user changes the type from Prospect to Customer, It will pick the last no. of previous record and then it will increment. Code will be unique for each account as we need to use the same code for integration purpose.

Samuel De Rycke
  • 9,550
  • 8
  • 45
  • 73
  • This is not a question. – Robin De Bondt Apr 25 '16 at 10:21
  • I think that I understand your question.

    So let's say Account A had its Type changed from Prospect to Customer on the 2nd Of March 2016, then the custom field 'Change Number' (or named however else you like) would be populated with '1'.

    Then Account C had its Type changed from Prospect to Customer on the 3rd of March 2016, 'Change Number' field would be populated with '2'.

    The 'Change Number' value must be unique. The Account cannot go from 'Customer' to 'Prospect', only from 'Prospect' to 'Customer'.

    Is this correct?

    – Andy Hitchings Apr 25 '16 at 10:54
  • Yes andy that's correct.. – Preeti Khanna Apr 25 '16 at 10:55
  • Hi Preeti, welcome to SFSE! Please take the time to visit the [Help] and read How do I ask a good question. If you post what you've written so far and where you're stuck, I'm confident you'll find plenty of people willing to assist you. SFSE is a Q&A forum where we look to help each other by asking and answering questions about real problems or errors with our code. Posting requests to write code for you generally get closed. – crmprogdev Apr 25 '16 at 12:33

1 Answers1

0

Hi dont worry I got You.

Just create a Text field like i created "customautono__c" in account object. Go to devloper console create new trigger on before update event for Account. So whenever your account Type gets updates "customautono__c" field will get a new value.

trigger cincAccount on Account (before update) {  
  for( Account a : Trigger.new ){
    if(String.isBlank(a.customautono__c)){
      a.customautono__c = 'c 1'; 
    }else{
       if( Trigger.oldMap.get( a.id ).Type != Trigger.newMap.get( a.id ).Type ){

      String temp = a.customautono__c;
      a.customautono__c = string.valueof(temp.split(' ')[0])+ ' ' + string.valueof(Integer.valueof(temp.split(' ')[1]) + 1);
        }
      }
    }

   }
  • Hi Amit, Thanks for the reply!I want that C1 to be updated on Account A if user changes Type from PRospect to customer . if user changes Account B from Prospect to customer then C2 should gets updated.But in above code if user changes Account A then C1 gets updated then if user updates Account B then again C1 gets updated while it should be C2 whenever user changes Type from Prospect to customer.Need your help as I got stuck and new to salesforce. Thanks in advance! – Preeti Khanna Apr 26 '16 at 03:08