+ Start a Discussion
Todd B.Todd B. 

Issue with Recordtypes

Hello All, 

I am trying to write a trigger for a certain Task record type
 
trigger Update_SD_Member_Enrollment_Progress_Status on Task (after update) {
/*
Automatically update the "Enrollment Progress Status" field, based upon the "Status" field 
of the last Member Task
*/

// Tasks that meet criteria, and new tasks to create
Task[] qualifiedTasks = new Task[0], newTasks = new Task[0];

Date dt=DateTime.now().addDays(1).date();
Date rt=DateTime.now().addDays(1).date();
    
// Map of SD Members
   Map<Id,SD_Member__c> members = new Map<Id,SD_Member__c>();

// Find qualifying tasks
for(Task record:Trigger.new)
     if(Record.RecordType.DeveloperName =='Member Outreach' &&
           record.isclosed == True)
           qualifiedtasks.add(record);
    
    // Obtain member ID values
    for(Task record:qualifiedtasks)
        members.put(record.whatid,null);
   
    // If there are any members to query, do so.
    if(!members.isempty())
        members.putall([select id, Enrollment_Progress_Status__c from SD_Member__c where id in :members.keyset()]);


 // For each qualifying task, check the current Enrollment_Progress_Status__c from SD_Member__c and assign
 // to the strStatus variable.  That way if the Task Status does not meet the critera below, the status 
 // will reamin it's current value 
for(Task record:qualifiedtasks) {
    String strStatus = members.get(record.whatid).Enrollment_Progress_Status__c;    

    // Set the strStatus based on the current Task Status value
      If (record.Status == 'Not Started' ){
          StrStatus = 'Attempting to Contact';}
      If (record.Status == 'Left Message w/ Person' ){
          StrStatus = 'Successful Contact';}
      If (record.Status == 'Member Hung Up' ){
          StrStatus = 'Successful Contact';}
      If (record.Status == 'Phone # Invalid' ){
          StrStatus = 'Phone # Invalid';}
      If (record.Status == 'Reached Recording - No Msg Left' ){
          StrStatus = 'Attempting to Contact';}
      If (record.Status == 'Reached Target - Call Later' ){
          StrStatus = 'Successful Contact';} 
      If (record.Status == 'Reached Recording - No Msg Left' ){
          StrStatus = 'Attempting to Contact';}              
      If (record.Status == 'Reached Target - Call Later' ){
          StrStatus = 'Successful Contact';}
      If (record.Status == 'Reached Target - Declined - Copay' ){
          StrStatus = 'Declined - Copay';}
      If (record.Status == 'Reached Target - Declined - Other' ){
          StrStatus = 'Declined - Other';}
      If (record.Status == 'Reached Target - Declined - Transport' ){
          StrStatus = 'Declined - Transportation';}
      If (record.Status == 'Reached Target - Requested Info' ){
          StrStatus = 'Decision Pending';}
      If (record.Status == 'Reached Target - Sent to Care Coach' ){
          StrStatus = 'Referred to Care Coach';}
      If (record.Status == 'Reached Target - Thinking About It' ){
          StrStatus = 'Decision Pending';}
          
    SD_Member__C SD1 = new SD_Member__C(ID=record.WhatId,Enrollment_Progress_Status__c=strStatus);
    update SD1; 
    
    }  }

But it keeps ignoring the line 
if(Record.RecordType.DeveloperName =='Member Outreach' &&
Any ideas on how to reference the Recordtype in the trigger?  I only want this to fire off for one of the five Task recordtypes we have.  

Thanks, 

Todd B.  
 
Best Answer chosen by Todd B.
Himanshu ParasharHimanshu Parashar
Hi Todd,

We can't get the relationship value inside trigger so you need to get that value from the database prior to move into Trigger.new loop

Add Following line before line 17
 
Map<id,Task> TaskMap = new Map<id,Task>([select id,RecordType.DeveloperName from Task where id in : Trigger.new.keyset()]);



add after that your if condition will be
 
if(TaskMap.get(Record.id).RecordType.DeveloperName =='Member Outreach' &&
           record.isclosed == True)

Thanks,
Himanshu
Salesforce Certified Developer | Administrator | Service Cloud Consultant

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

All Answers

Himanshu ParasharHimanshu Parashar
Hi Todd,

We can't get the relationship value inside trigger so you need to get that value from the database prior to move into Trigger.new loop

Add Following line before line 17
 
Map<id,Task> TaskMap = new Map<id,Task>([select id,RecordType.DeveloperName from Task where id in : Trigger.new.keyset()]);



add after that your if condition will be
 
if(TaskMap.get(Record.id).RecordType.DeveloperName =='Member Outreach' &&
           record.isclosed == True)

Thanks,
Himanshu
Salesforce Certified Developer | Administrator | Service Cloud Consultant

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
This was selected as the best answer
Todd B.Todd B.
I update ny code as follows:
 
trigger Update_SD_Member_Enrollment_Progress_Status on Task (after update) {
/*
Automatically update the "Enrollment Progress Status" field, based upon the "Status" field 
of the last Member Task
*/

// Tasks that meet criteria, and new tasks to create
Task[] qualifiedTasks = new Task[0], newTasks = new Task[0];

Date dt=DateTime.now().addDays(1).date();
Date rt=DateTime.now().addDays(1).date();
    
// Map of SD Members
   Map<Id,SD_Member__c> members = new Map<Id,SD_Member__c>();

Map<id,Task> TaskMap = new Map<id,Task>([select id,RecordType.DeveloperName from Task 
                                         where id in : Trigger.new.keyset()]);

// Find qualifying tasks
for(Task record:Trigger.new)
	if(TaskMap.get(Record.id).RecordType.DeveloperName =='Member Outreach' &&
           record.isclosed == True)
           qualifiedtasks.add(record);
    
    // Obtain member ID values
    for(Task record:qualifiedtasks)
        members.put(record.whatid,null);
   
    // If there are any members to query, do so.
    if(!members.isempty())
        members.putall([select id, Enrollment_Progress_Status__c from SD_Member__c where id in :members.keyset()]);


 // For each qualifying task, check the current Enrollment_Progress_Status__c from SD_Member__c and assign
 // to the strStatus variable.  That way if the Task Status does not meet the critera below, the status 
 // will reamin it's current value 
for(Task record:qualifiedtasks) {
    String strStatus = members.get(record.whatid).Enrollment_Progress_Status__c;    

    // Set the strStatus based on the current Task Status value
      If (record.Status == 'Not Started' ){
          StrStatus = 'Attempting to Contact';}
      
      If (record.Status == 'Left Message w/ Person' ){
          StrStatus = 'Successful Contact';}
      
      If (record.Status == 'Reached Target - Member Hung Up' ){
          StrStatus = 'Successful Contact';}
      
      If (record.Status == 'Phone # Invalid' ){
          StrStatus = 'Phone # Invalid';}
      
      If (record.Status == 'Reached Recording - No Msg Left' ){
          StrStatus = 'Attempting to Contact';}
      
      If (record.Status == 'Reached Target - Call Later' ){
          StrStatus = 'Successful Contact';} 
      
      If (record.Status == 'Reached Recording - No Msg Left' ){
          StrStatus = 'Attempting to Contact';}              
      
      If (record.Status == 'Reached Target - Call Later' ){
          StrStatus = 'Successful Contact';}
      
      If (record.Status == 'Reached Target - Declined - Copay' ){
          StrStatus = 'Declined - Copay';}
      
      If (record.Status == 'Reached Target - Declined - Other' ){
          StrStatus = 'Declined - Other';}
      
      If (record.Status == 'Reached Target - Declined - Transport' ){
          StrStatus = 'Declined - Transportation';}
      
      If (record.Status == 'Reached Target - Requested Info' ){
          StrStatus = 'Decision Pending';}
      
      If (record.Status == 'Reached Target - Sent to Care Coach' ){
          StrStatus = 'Referred to Care Coach';}
      
      If (record.Status == 'Reached Target - Thinking About It' ){
          StrStatus = 'Decision Pending';}
          
    SD_Member__C SD1 = new SD_Member__C(ID=record.WhatId,Enrollment_Progress_Status__c=strStatus);
    update SD1; 
    
    }  }

On I am getting a error on line 17: Method does not exist or incorrect signature: [List<Task>].keyset()   
Himanshu ParasharHimanshu Parashar
Hi Todd,

I am sorry for the typo. it is 
 
Trigger.newmap.keyset();

Thanks,
Himanshu
Todd B.Todd B.
It worked, but I had to change one thing, 
Map<id,Task> TaskMap = new Map<id,Task>([select id,RecordType.DeveloperName from Task 
                                         where id in : Trigger.new.keyset()]);

// Find qualifying tasks
for(Task record:Trigger.new)
	if(TaskMap.get(Record.id).RecordType.DeveloperName =='Member Outreach' &&
           record.isclosed == True)

I have to do away with the .DeveloperName
 
Map<id,Task> TaskMap = new Map<id,Task>([select id,RecordType.Name from Task 
                                         where id in : Trigger.new.keyset()]);

// Find qualifying tasks
for(Task record:Trigger.new)
	if(TaskMap.get(Record.id).RecordType.Name =='Member Outreach' &&
           record.isclosed == True)

And just use RecordType.Name instead.  
Himanshu ParasharHimanshu Parashar
good job Todd! :)