• Phil Wride
  • NEWBIE
  • 0 Points
  • Member since 2013

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
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());       
    }*/

}
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());       
    }*/

}