+ Start a Discussion
BeatofHeartBeatofHeart 

Trying to update with record types on trigger

Dear all,

 

i am having some problem with my trigger. 

 

So, I have custom object called Request.  When the picklist value "Atteneded " is stored in the Request  object's I want it to update Contact checkbox.  It works correctly for the Contact object.  I want to enhance the following code only for few records on contact record types .its updating on everything. 

 

 

------------------------------- 

// Update Contact field: "Visited" to TRUE when Attendance Status record is Update to Attended.

trigger UpdateVisitedCheckbox on Request__c (after insert, after update) {


map< id, contact > contacts = new map< id, contact >();

// Create trigger for new or selectedAttendance Status record
for( Request__c record:trigger.new) 

//if((Recordtype == 'Prospective_Student' or 'Student')
{
if(record.Attendance_Status__c == 'Attended')

// Update checkbox field on the Contact record to TRUE
contacts.put(record.contact__c, new contact(id=record.contact__c, Visited__c = TRUE)); 
}

update contacts.values();

 

 

Any help will be appriciated. 

 

jvan001jvan001

You have to query Salesforce for the RecordType Id and then you can just use the variable in the If Statement you have commented in your code.

 

Use that line of code below to query the RecordType Id.

 

Id rtId = [select Id,name from RecordType where name='Label of the record type' and SObjectType='Case' limit 1].Id;

 

Make sure you use the LABEL of the RecordType and the correct SObject the RecordType is attributed to.

 

Ex:

 

Id rtId = [SELECT Id FROM RecordType WHERE Name = 'Student' and SObjectType ='Request__c' LIMIT 1].Id;
Id rtId2 = [SELECT Id FROM RecordType WHERE Name = 'Prospective Student' and SObjectType = 'Request__c' LIMIT 1].Id;

if(record.RecordTypeId == rtId || record.RecordType == rtId2)
{
     //code goes here
}

 

I haven't used the RecordType in the way you are, so it could be that you did not specify the object for which the RecordType is associated. Instead of RecordType = 'Student', try record.RecordType = 'Student'. I'm not completely sure on this, since I haven't used it that way before.

 

Hope that helps!

BeatofHeartBeatofHeart

Hi I am sorry.. for some reason when i givnen record type it is not taking , am i doing some thing wrong?

trigger UpdateVisitedCheckbox on Visit_Request__c (after insert, after update)
{
 
 

              map< id, contact > contacts = new map< id, contact >();
             Id rtId = [SELECT Id FROM RecordType WHERE Name = 'Student' and SObjectType ='contact' LIMIT 1].Id;
             Id rtId2 = [SELECT Id FROM RecordType WHERE Name = 'Student (PS)' and SObjectType ='contact' LIMIT 1].Id;
            


           
              for( Visit_Request__c record:trigger.new)     
              {

                   if(record.Attendance_Status__c == 'Attended')
 
                   if(rtId == '012A0000000k0t9IAA'|| rtid2 == '012A0000000k0tAIAQ')
                      {
                        
                         contacts.put(record.contact__c, new contact(id=record.contact__c, Visited__c = TRUE));     
                        }
               }

 
                   update contacts.values(); 

 Am i doing something wrong here? it should work only for 2 record types.. but its working for all record types.

jvan001jvan001

You can try putting { } on your first if-statement, but I don't think your nested if-statement will work (depending on what you want it to do).

 

for(Visit_Request__c record:trigger.new)     
{
     if(record.Attendance_Status__c == 'Attended')
     {
          if(rtId == '012A0000000k0t9IAA'|| rtid2 == '012A0000000k0tAIAQ')
          {
               contacts.put(record.contact__c, new contact(id=record.contact__c, Visited__c = TRUE));     
          }
     }

}

 OR

 

The code below assumes you want the trigger to execute when the record type of the object is either 'Student' or 'Student (PS)'.

 

for(Visit_Request__c record:trigger.new)     
{
     if(record.Attendance_Status__c == 'Attended')
     {
          if(record.RecordTypeId == rtId || record.RecordTypeId == rtid2)
          {
               contacts.put(record.contact__c, new contact(id=record.contact__c, Visited__c = TRUE));     
          }
     }

}

 

You should try and stay away from hard coding the RecordTypeId's in your code.

 

The revised code will run when:

1. A Visit_Request__c object is created or updated.

2. When the Attendance_Status__c equals Attended

3. When the RecordType of the Visit_Request__c object is either Student or Student (PS).

 

You should be comparing the object's RecordType Id with the RecordType Id you're querying.

 

GL