1

I have written a before update trigger which will basically update contact object Type__c field with the value present in Account object Type.The Type__c field of contact object is also a picklist.

public static void UpdateType(List<Contact> conList)
{
  Set<ID> setaccountid=new Set<ID>();
  List<Contact> finalconList=new List<Contact>();

  for(Contact c:conList)
  {
     if (c.AccountID != NULL)
        setaccountid.add(c.AccountID);
  }

  if (setaccountid != NULL)
  {
   Map<ID,Account> conMap=new Map<ID,Account>([select ID,Type from Account 
                                              where ID IN :setaccountid]);

    for(Contact con:conList)
    {
      if (conMap.containsKey(con.AccountID))

        con.Type__c=conMap.get(con.AccountId).Type;
    }
  }
 }

     trigger trg_conType on Contact (before Insert,before Update)
     {
          if (trigger.isUpdate)
          UpdatecontactType.UpdateType(Trigger.New);
     }

I havent been able to get the code working. Please let me know if I am missing something.

1
  • Can you go into more detail about what you mean by "my code isn't working"? Your code looks fine and correct to me (bulkified, logic outside of trigger, using the correct trigger event, operating on trigger.new). Are you getting an error somewhere (either when trying to save your code, or when you try to update a Contact)? If so, including the text of the error message verbatim would be helpful.
    – Derek F
    Commented May 8, 2017 at 13:30

2 Answers 2

1

You need to run your trigger on before action (like before insert and before update), below are the code snippet :

Apex trigger :

trigger trg_conType on Contact (before Insert,before Update)
{
    if(Trigger.isbefore && (Trigger.isInsert || trigger.isUpdate)){     
        UpdatecontactType.UpdateType(Trigger.New);
    }   
}

Apex class : -

public static void UpdateType(List<Contact> conList)
{
  Map<Id,String> map_account_type = new Map<Id,String>();
  Set<ID> setaccountid=new Set<ID>();
  List<Contact> finalconList=new List<Contact>();

    if(conList.size()>0){
        for(Contact c:conList)
        {
            map_account_type.put(c.AccountID,Type);
            setaccountid.add(c.AccountID);

        }
    }

    if(setaccountid.size()>0){
        for(Contact con : [Select Id,Type__c,AccountId from contact where AccountId IN : setaccountid]){
            con.Type__c = map_account_type.get(con.AccountID);


        }       
    }


 }

i think it will solve your purpose.

2
  • I don't see how using an after trigger event is required here, and would argue that before update is the correct one to use here (the trigger is on Contact, and the field being updated is on Contact). Can you go into more detail about how you reached your conclusion?
    – Derek F
    Commented May 8, 2017 at 13:22
  • @ Derek F, you are rt , have updated the code. It was my mistake i thought the trigger was written on Account.
    – Avijit Das
    Commented May 8, 2017 at 13:34
0


Your codes seems to be fine but what you are missing is to update the Contact record. In your code you are declaring a list of Contact but you haven't used it.
Try the code below:

public static void UpdateType(List<Contact> conList){
 Set<ID> setaccountid= new Set<ID>();
 List<Contact> finalconList= new List<Contact>();
for(Contact c: conList){
 if(c.AccountID != NULL)
    setaccountid.add(c.AccountID);
  }
 if(setaccountid != NULL){
Map<ID,Account> conMap= new Map<ID,Account>([select ID,Type from Account where ID IN :setaccountid]);
for(Contact con:conList){
 if(conMap.containsKey(con.AccountID)){
      con.Type__c=conMap.get(con.AccountId).Type;
      finalconList.add(con);
    }       
 }
 if(finalconList.size()> 0){
     Update finalconList;
    }
  }
}

Regards,
Akshay
Please mark my answer as a solution if it is helpful.

4
  • I made the changes , trigger is before update. It doesnt work Commented May 8, 2017 at 9:06
  • @Avijit das, since we are performing update on same object, should''t it be an before update trigger, also changes suggested by u are not working. Commented May 8, 2017 at 9:13
  • My code is working. actually I did not add picklist values to contact object Type__c field. so it was give error as " Bad value for restricted picklist field". Commented May 8, 2017 at 11:52
  • @PoojaBiswas, My apologies I didn’t tried it first and it’s not giving the right answer . Well what I will suggest is that you should try it by Process Builder or if you want to do this by trigger then the trigger should be on Account not on Contact because if you are writing trigger on Contact(On Update) and at last you will update the Contact then the trigger will again fried and it would go in recursive circle. Process Builder works fine I have tested it and it’s filling the Contact Type__c field from its parent Account. Commented May 8, 2017 at 12:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .