+ Start a Discussion
Phil WridePhil Wride 

trigger for copying custom field value to another custom field in a different object

Hi All,

I'm new to the Apex side of things and creating Triggers so hoping someone can help.. Here's the scenario; I'd like to automatically update a custom field on a Contact when a custom field relating to Task (ActivityHistory) has a value assigned (1-5). The custom field on the Task is populated based on the Subject content of the completed Task. For example, an email comes in with the Subject of "1 star", the custom field for the Task updates with the value 1, I'd like the custom field on the Contact to also update to read the value 1.

I saw on another post this code but I'm wondering how I need to manipulate it for the above.

Many thanks in advance.


trigger UpdateOpptyIndicator on Opportunity (after insert,after update) {
    System.Debug('ENTERED INTO TRIGGER');
   Contact con,oldCon;
    if (trigger.isInsert) {
    System.Debug('INSIDE ISINSERT');
        if(trigger.isUpdate)
        {
           
        }
        else
        {
            for(Opportunity opp : trigger.new) {
                con=[Select id,Opportunity_Indicator__c from Contact where id=:opp.Contact__c];
                if(con.Opportunity_Indicator__c != null)
                    con.Opportunity_Indicator__c = con.Opportunity_Indicator__c + 1;
                else
                    con.Opportunity_Indicator__c = 1;
                    System.Debug('First Place');
                    update con;
            }
        }
  }
  else {
      System.Debug('INSIDE ISUPDATE');
      if(trigger.isUpdate)
      {               
                for(Opportunity opp : trigger.new) {
                    Opportunity oldOpp = Trigger.oldMap.get(opp.id);
                    System.Debug('OLD OPPORTUNITY :' + oldOpp.Contact__c);
                    System.Debug('NEW OPPORTUNITY :' + opp.Contact__c);
                    if(opp.Contact__c != oldOpp.Contact__c) {
                        con=[Select id,Opportunity_Indicator__c from Contact where id=:opp.Contact__c];
                        System.Debug('PREVIOUS VALUE :'+ con.Opportunity_Indicator__c);
                        if(con.Opportunity_Indicator__c != null)
                            con.Opportunity_Indicator__c = con.Opportunity_Indicator__c + 1;
                        else
                            con.Opportunity_Indicator__c = 1;
                           
                        System.Debug('NEW VALUE :'+ con.Opportunity_Indicator__c);
                        update con;
                        Contact con1 = [Select id,Opportunity_Indicator__c from Contact where id=:opp.Contact__c ];
                        System.Debug('AFTER DATABASE UPDATE :'+ con1.Opportunity_Indicator__c);
                        break;

                    }
                   
                      //System.Debug('Second Place');
                 }
      }
  }
/* try{
        update con;
     }catch(Exception e){
        System.debug('Exception Ocurred'+e.getMessage());       
    }*/

}
RoyGiladRoyGilad
H
iI'm not sure what you're question is but a few pointers:
a) Change your trigger to work with lists:
you need the query OUTSIDE the loop to get the related Contacts and later updated them as a list.
B) never query in a loop...

Tell me if it helps,
Roy

Phil WridePhil Wride
Hi Roy,

Thanks for the response. My question is....how do I create a trigger that updates a custom field in one object based on a custom field in a different object.

Regards
suyog dongaonkarsuyog dongaonkar
You will need to create a trigger on task object that updates corresponding contact object records. You can utilize trigger.new context variable which provides a list of records in context.
I hope following algorithm might help:

for each task in trigger.new{
create a map of contact id to task (say it map1)
}

list<Contact> list1 = [get all contacts where id in map1.keyset]

for each contact c in list1{
t1= map1.get(c.id)
c.contact_field = t1.task_field;
}
update list1;