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
StefanStefan 

Apex Trigger to check picklist field and Record Type

 I'm complete beginner to Apex code and would appreciate some help.

 

I would like to refine the below code to:

  1. Trigger only whenPicklist field Contact.Status__c changes from 'Active' to 'Inactive'
  2. Change record type from Active to Inactive

 

trigger InactiveContact on Contact (before insert, before update) { private Contact[] newContact = Trigger.new; newContact[0].Original_Account__c = newContact[0].Accountid; newContact[0].Original_Owner__c = newContact[0].OwnerId; newContact[0].Original_Country_ID__c = newContact[0].Country__c; }

 

Also is there anywhere I can find examples of apex triggers.

 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

Here you go:

 

 

// full version

Id inactiveAccountId;

List<Account> inactiveList=[select id, Name from Account where name ='INACTIVE CONTACTS'];

if (!inactiveList.isEmpty())

{

inactiveAccountId=inactiveList[0].id;

}

 

// or this can be reduced to

Id inactiveAccountId=[select id, Name from Account where name ='INACTIVE CONTACTS'][0].id;

 

 

 

Message Edited by bob_buzzard on 01-21-2010 12:16 PM

All Answers

bob_buzzardbob_buzzard

Your trigger will fire in all cases - that's the idea of triggers. 

 

You can't pick up changes to fields on insert - there was no old version to compare against.

 

Record types are stored in a different table, so you'll need to look up the id of the type that you want to set.

 

Then you'll need to iterate trigger.old and trigger.new in order and check the value of the Status__c field.  If it has changed in the way that you are looking for, set the recordtypeid field to the id of the type you captured earlier.

 

bob_buzzardbob_buzzard

Here's an example put together from one of my projects - there might be the odd typo, but hopefully will be of use:

 

 

trigger InactiveContact on Contact (before update) { { // Get ID for Inactive Record Type Id theRT=Schema.SObjectType.Contact.getRecordTypeInfosByName().get('Inactive').getRecordTypeId(); Integer count=0 List<Id> projIds=new List<Id>(); for (Id theId : trigger.oldMap.keySet()) { System.debug('############ Contact id = ' + theId); Contact oldCont=trigger.oldMap.get(theId); Contact newCont=trigger.newMap.get(theId); String oldStatus=oldCont.Status__c; String newStatus=newCont.Status__c; System.debug('############ Old status = ' + oldStatus + ', new status = ' + newStatus); if ( (null!=oldStatus) && ('Active'==oldStatus) && (null!=newStatus) && ('Inactive'==newStatus) ) { newCont.RecordTypeId=theRT; } } }

 

 

 

StefanStefan

Thanks Bob for the help.

 

This is still beyond my Apex experience.

 

To keep this simple, how about if I change the code to just trigger on updates and ignore changing the record type which I can deal with using workflow.

 

So what I need to insert in the below is an if statement which only updates the fields if Picklist field "Status" has changed it's value from "Active" to "Inactive"

 

 

trigger InactiveContact on Contact (before update) {
private Contact[] newContact = Trigger.new;
newContact[0].Original_Account__c = newContact[0].Accountid;
newContact[0].Original_Owner__c = newContact[0].OwnerId;
newContact[0].Original_Country_ID__c = newContact[0].Country__c;
}

 


 

bob_buzzardbob_buzzard
Did you see my example above?  We might be cross posting!
StefanStefan

bob_buzzard wrote:
Did you see my example above?  We might be cross posting!

Hi Bob, your code example was far too complex for me.  I'm finding Apex code tough going as the documentation assumes Java knowledge. I am willing to learn Apex but I have no need to learn Java. 

 

Anyway I had another go my code trying to keep it simple and with considerable trial and error in our sandbox. I got the below to work.

 

Couple of questions:

 

1. How do I change the line  so it will work in production

c.Accountid = '001T000000H5bAY';

 2. Does this code look OK?

 

trigger InactiveContact3 on Contact (before insert, before update) { for (Contact c: Trigger.new) { if(c.Status__c == 'Inactive'&& c.RecordTypeId == '012400000009cPl')//c.Inactive__c != true) { c.Original_Account__c = c.Accountid; c.Original_Owner__c = c.OwnerId; c.Original_Country_ID__c = c.Country__c; // Need to amend the next line so it works in Production c.Accountid = '001T000000H5bAY'; // Recort Type id's seem to be same in Sandbox and production c.RecordTypeId = '01240000000UOua'; } else if ( c.Status__c == 'active' && c.RecordTypeId == '01240000000UOua') //c.Inactive__c == true) { c.RecordTypeId = '012400000009cPl'; c.Accountid = c.Original_Account__c; } } }

 


 

bob_buzzardbob_buzzard

Not sure what you are doing with question 1 - you are changing the account id, but I don't know where your hardcoded account id comes from.

 

I reckon your code will work - if you are happy with hardcoding values then stick with it!

 

BTW all object ids (not just record types) will be identical in sandbox and production.  So if the hardcoded account id is present in production, it should work.

StefanStefan

bob_buzzard wrote:

Not sure what you are doing with question 1 - you are changing the account id, but I don't know where your hardcoded account id comes from.


The hard coded account id is a dummy account 'INACTIVECONTACTS' which I am using to store contacts who have left the original account but where we still want to retain a link to a related inactive contacts list.

 


bob_buzzard wrote: 

BTW all object ids (not just record types) will be identical in sandbox and production.  So if the hardcoded account id is present in production, it should work.


 

Our Enterprise org only has a free Sandbox which when I refresh is empty of data. The record type ids are the same but for accounts I have to manually input them in our Sandbox. Unless I am missing something the ID for account 'INACTIVE CONTACTS' will be different in Production and Sandbox.

 

I would welcome any help which tells me how to code retrieving the Account ID for Account Name 'INACTIVE CONTACTS'

P.S. I wish there was an APEX Code for dummies book (a book which did not assume prior programing knowlege).  (I hjave Basic, SQL and VBA but not Java)
Message Edited by Stefan on 01-21-2010 08:12 PM
bob_buzzardbob_buzzard

Here you go:

 

 

// full version

Id inactiveAccountId;

List<Account> inactiveList=[select id, Name from Account where name ='INACTIVE CONTACTS'];

if (!inactiveList.isEmpty())

{

inactiveAccountId=inactiveList[0].id;

}

 

// or this can be reduced to

Id inactiveAccountId=[select id, Name from Account where name ='INACTIVE CONTACTS'][0].id;

 

 

 

Message Edited by bob_buzzard on 01-21-2010 12:16 PM
This was selected as the best answer
StefanStefan

Thanks Bob, that works perfectly.

 

I have revised my code below

 

 

trigger InactiveContact4 on Contact (before insert, before update) { Id inactiveAccountId=[select id, Name from Account where name ='INACTIVE CONTACTS'][0].id; for (Contact c: Trigger.new) { if(c.Status__c == 'Inactive'&& c.RecordTypeId == '012400000009cPl') { c.Original_Account__c = c.Accountid; c.Original_Owner__c = c.OwnerId; c.Original_Country_ID__c = c.Country__c; c.Accountid = inactiveAccountId; c.RecordTypeId = '01240000000UOua'; } else if ( c.Status__c == 'active' && c.RecordTypeId == '01240000000UOua') { c.RecordTypeId = '012400000009cPl'; c.Accountid = c.Original_Account__c; } } }