Assign the record types using apex code

2,583 views
Skip to first unread message

anil kumar

unread,
Nov 3, 2011, 7:19:37 AM11/3/11
to salesforce professionals
Hi,

I have a problem here,If the status is equal to the prospect,i want to
assign this record to a another record tye
But this trigger giving me error ....

CaseSetupTrigger: execution of BeforeUpdate caused by:
System.NullPointerException: Attempt to de-reference a null object:
Trigger.CaseSetupTrigger: line 9, column 15

Can anybody help me what's the wrong with this code.???

trigger CaseSetupTrigger on Contact(before update){

for(Contact c:Trigger.new)
{
Contact cons = [SELECT LastName FROM Contact where Id
IN :Trigger.new];
RecordType rt = [select Id,name from RecordType where Name
='Duplicate' and SobjectType = 'Contact'];
if(c.status__c=='Prospect')
{
c.RecordType.id='012900000003JZXAA2';
System.debug('id...'+c.RecordType.id);
}



}
}

Mohit Pant

unread,
Nov 3, 2011, 7:27:46 AM11/3/11
to salesforce-p...@googlegroups.com
trigger CaseSetupTrigger on Contact(before update){

       for(Contact c:Trigger.new)
        {
        Contact cons = [SELECT LastName FROM Contact where Id
IN :Trigger.new];
         RecordType rt = [select Id,name from RecordType where Name
='Duplicate' and SobjectType = 'Contact'];
system.debug('record found------------------------'+rt);

if (!rt.isEmpty() )
{

Anil Kumar

unread,
Nov 3, 2011, 7:38:33 AM11/3/11
to salesforce-p...@googlegroups.com
 the error is Attempt to de-reference a null object:Trigger.CaseSetupTrigger: line 9, column 15

Mohit Pant

unread,
Nov 3, 2011, 8:03:58 AM11/3/11
to salesforce-p...@googlegroups.com
dude.............................
u havent received status__c in query itself ..
change query to :   Contact cons = [SELECT id, name , status__c, LastName FROM Contact where Id
IN :Trigger.new];

Anil Kumar

unread,
Nov 3, 2011, 10:16:54 AM11/3/11
to salesforce-p...@googlegroups.com
Now I changed the code as follows 

But the record type is not assigning to the present record .

trigger CaseSetupTrigger on Contact(before insert,before update){


  
       for(Contact c:[select id,name,RecordTypeId,status__c  from contact where id in:Trigger.new])
        {
        
        RecordType rt = [select Id,name from RecordType where Name ='Duplicate1' and SobjectType = 'Contact'];
        system.debug('record found------------------------'+rt);
          if(rt.Id!=null)
          {
        

             if(c.status__c=='Prospect')
             {
             c.RecordTypeid='012900000003JZcAAM';
             System.debug('id...'+c.RecordType.id);
           
             }

  }

 
    }
}

Prajnith

unread,
Nov 3, 2011, 10:33:13 AM11/3/11
to salesforce-p...@googlegroups.com
Update Statement Missing...!


Prajnith
Banglore 

Charles M

unread,
Nov 23, 2011, 12:10:03 AM11/23/11
to salesforce-p...@googlegroups.com

Hi all,

Instead of going for a trigger u can use workflow to update the recordtype field

It works

On 3 Nov 2011 20:03, "Prajnith" <praj...@gmail.com> wrote:

Update Statement Missing...!


Prajnith
Banglore 





On Thu, Nov 3, 2011 at 7:46 PM, Anil Kumar <anilku...@gmail.com> wrote:
>

> Now I changed th...

Reply all
Reply to author
Forward
0 new messages