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
Vishal (Sops)Vishal (Sops) 

Help with trigger on task related to custom object

Guys - Can you help me out. I have a custom object CC Card. I need to update a picklist value field on this object depending on the task logged under this CC Card object. My requirement are:-

 

Trigger should fire only when task gets closed.

If task subject contains "Kick-Off" and is being closed, it should update the picklist field, say stage as Kick-Off .

 

I am new to triggers, see if you experts can help me out. Here is my try at it.

 

trigger TestVishal on Task (before update) {

//Preparting CCCards to Update
public list<CC_Card__c> CardsToUpdate = new List<CC_Card__c>();
public set<Id> CCCardIDs = new Set<Id>();

 if(Trigger.isUpdate){

 for(Task t : Trigger.new)
 {
      if(t.WhoId<>null)
      {
      
               if(string.valueOf(t.WhoId).startsWith('a0P') && t.IsClosed == True)
               CCCardIDs.add(t.whoId);
               
       }
               
 }
 }
 if(CCCardIDs.size()<>0){
  for(CC_Card__c c : [Select c.Id, c.stage__c,(Select Id, Ownerid From Tasks where IsClosed = True AND subject like '%Kick-Off%')From CC_Card__c c where Id in :CCCardIDs])
             
             CardsToUpdate.add(new CC_Card__c(Id=c.Id, stage__c = 'Kick-Off'));                
                                       
        }     
        
        update CardsToUpdate;                     
     
  }

 

It's not giving me any error message when I save it but it's not working either :(

Best Answer chosen by Admin (Salesforce Developers) 
Shailesh PatilShailesh Patil

Can you please try this? Change it appropritely.

 

//Preparting CCCards to Update
public list<CC_Card__c> CardsToUpdate = new List<CC_Card__c>();
public set<Id> CCCardIDs = new Set<Id>();

 if(Trigger.isUpdate){

 for(Task t : Trigger.new)
 {
      if(t.WhoId<>null)
      {
       if(t.IsClosed = True && t.subject.contains('Kick-Off'))
		  {
			CCCardIDs.add(Add related CCCrad Id here, it may be t.CCCradId or something like this);	
				
		  }
               
      }
               
 }
 }
 if(CCCardIDs.size()<>0)
	 {
		  for(CC_Card__c c : [Select c.Id, c.stage__c From CC_Card__c c where Id in :CCCardIDs])
			 {
					 c.stage__c = 'Kick-Off';
					 CardsToUpdate.add(c);                
			 }                                
     }     
        
        update CardsToUpdate;                     
     
  }

 

All Answers

Shailesh PatilShailesh Patil

Can you please specify the object relationship between cccards and task?

 

 

Thanks!

Vishal (Sops)Vishal (Sops)

Parent-Child relationship..

PrabhaPrabha

if tht is parent child...

 

please follow this instead of trigger

http://developer.force.com/cookbook/recipe/updating-a-field-on-a-parent-record

Shailesh PatilShailesh Patil

Can you please try this? Change it appropritely.

 

//Preparting CCCards to Update
public list<CC_Card__c> CardsToUpdate = new List<CC_Card__c>();
public set<Id> CCCardIDs = new Set<Id>();

 if(Trigger.isUpdate){

 for(Task t : Trigger.new)
 {
      if(t.WhoId<>null)
      {
       if(t.IsClosed = True && t.subject.contains('Kick-Off'))
		  {
			CCCardIDs.add(Add related CCCrad Id here, it may be t.CCCradId or something like this);	
				
		  }
               
      }
               
 }
 }
 if(CCCardIDs.size()<>0)
	 {
		  for(CC_Card__c c : [Select c.Id, c.stage__c From CC_Card__c c where Id in :CCCardIDs])
			 {
					 c.stage__c = 'Kick-Off';
					 CardsToUpdate.add(c);                
			 }                                
     }     
        
        update CardsToUpdate;                     
     
  }

 

This was selected as the best answer
Vishal (Sops)Vishal (Sops)

Thanks Shailesh for your reply but this still is not working, also what did you mean by related CCCardID, is there any issue with using CCCardIDs.add(t.WhoId).

Shailesh PatilShailesh Patil

Yes, it does make the difference. WhoID is the Contact ID. You need to specify the related CCCards Id .... Unless you do that, it wont work.

 

You you see in the object defination or in eclipse Schema, you will get the field name for CCCrads on Task object.

Vishal (Sops)Vishal (Sops)

Ok, I used t.whatId and it seems it's working. I will test it out further and will let you know about issues :), thanks a lot for your help. I will mark this as a solution once I am done with it