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
SKollcakuSKollcaku 

A trigger to copy the value of a custom picklist from Account to another custom picklist in Contact?

Hi,

I need to have a trigger to copy the value of a picklist from Account to its related Contacts (another custom picklist).
The requirement is only to be copied if in the Contact poage the picklist is empty.

I tried this trigger:
 
trigger UpdateContactRegione on Contact(before Insert, before Update) {

    for(Contact Cont: Trigger.new){

     Account acct = [select id,Regione__c from Account where Id=:Cont.AccountId];

       if(Cont.Regione__c == null)

                     
          Cont.Regione__c= acct.Regione__c;

     
       }

   }

But it worksonly if I change the picklist in Contact and not viceversa.

Any suggestions?

Thanks,
Skender
Best Answer chosen by SKollcaku
Srinivas SSrinivas S
Hi,

You should use a batch class for these kind of scenarios, please find the following solution -

Batch Class:
global class UpdateEmptyConRegionFields implements Database.Batchable<sObject>{
	global Database.QueryLocator start(Database.BatchableContext BC){
		//Query all the contacts which are having region as blank
		return Database.getQueryLocator('select Region__c, accountId from Contact where Region__c = \'\'');
	}

	global void execute(Database.BatchableContext BC, List<sObject> sobjLst){
		Set<Id> accSet = new Set<Id>();
		for(Contact con : (List<Contact>)sobjLst){
			//Collect all the related accounts from which we need to fetch the region value.
			accSet.add(con.accountId); 
		}
		//Query all the related accounts in order to fetch the region and keep it in a map to use it in next steps
		Map<Id,Account> accMap = new Map<Id,Account>([select Region__c from Account where id in: accSet]);
		List<Contact> conLst4Upd = new List<Contact>();
		for(Contact con : (List<Contact>)sobjLst){
			//keep all the modified contacts in a list
			con.Region__c = accMap.get(accountId).Region__c;
			conLst4Upd.add(con);
		}
		//Update the contacts.
		if(conLst4Upd.size() > 0)
			update conLst4Upd;
	}

	global void finish(Database.BatchableContext BC){
		//Do some post commit logic
	}
}

Schedulable Class:
global class scheduledUpdateEmptyConRegionFields implements Schedulable {
	global void execute(SchedulableContext sc) {
		UpdateEmptyConRegionFields b = new UpdateEmptyConRegionFields(); 
		database.executebatch(b);
	}
}

Schedule the above class by navigating to Setup > Develop > Apex Classes > Schedule Apex 

(or)

Developer Console > anonymous block > past the following logic -
UpdateEmptyConRegionFields b = new UpdateEmptyConRegionFields(); 
		database.executebatch(b);

------------
Thanks,
Srinivas
- Please mark as solution if your problem is resolved.

All Answers

Dinesh Suggala 8Dinesh Suggala 8
Skender,

Have one more field Regione_Checkbox__c(Default the value to True).It will work n the Contact poage the picklist is empty. 

trigger UpdateContactRegione on Contact (before insert,before Update) {

    for(Contact Cont: Trigger.new){

     Account acct = [select id,Regione__c from Account where Id=:Cont.AccountId];

        if(Cont.Regione__c == null && Cont.Regione_Checkbox__c==True)
        {
          Cont.Regione__c= acct.Regione__c;
        }
       }  

}

Please Let me know if it is helpful or not.

Thanks& Regards
Dinesh
Srinivas SSrinivas S
Hi,

You should use a batch class for these kind of scenarios, please find the following solution -

Batch Class:
global class UpdateEmptyConRegionFields implements Database.Batchable<sObject>{
	global Database.QueryLocator start(Database.BatchableContext BC){
		//Query all the contacts which are having region as blank
		return Database.getQueryLocator('select Region__c, accountId from Contact where Region__c = \'\'');
	}

	global void execute(Database.BatchableContext BC, List<sObject> sobjLst){
		Set<Id> accSet = new Set<Id>();
		for(Contact con : (List<Contact>)sobjLst){
			//Collect all the related accounts from which we need to fetch the region value.
			accSet.add(con.accountId); 
		}
		//Query all the related accounts in order to fetch the region and keep it in a map to use it in next steps
		Map<Id,Account> accMap = new Map<Id,Account>([select Region__c from Account where id in: accSet]);
		List<Contact> conLst4Upd = new List<Contact>();
		for(Contact con : (List<Contact>)sobjLst){
			//keep all the modified contacts in a list
			con.Region__c = accMap.get(accountId).Region__c;
			conLst4Upd.add(con);
		}
		//Update the contacts.
		if(conLst4Upd.size() > 0)
			update conLst4Upd;
	}

	global void finish(Database.BatchableContext BC){
		//Do some post commit logic
	}
}

Schedulable Class:
global class scheduledUpdateEmptyConRegionFields implements Schedulable {
	global void execute(SchedulableContext sc) {
		UpdateEmptyConRegionFields b = new UpdateEmptyConRegionFields(); 
		database.executebatch(b);
	}
}

Schedule the above class by navigating to Setup > Develop > Apex Classes > Schedule Apex 

(or)

Developer Console > anonymous block > past the following logic -
UpdateEmptyConRegionFields b = new UpdateEmptyConRegionFields(); 
		database.executebatch(b);

------------
Thanks,
Srinivas
- Please mark as solution if your problem is resolved.
This was selected as the best answer
Deepak Pandey 13Deepak Pandey 13
trigger UpdateContactRegione on Contact(before insert ,After Insert) {
set<id> setid =new set<id>();
    for(Contact Cont: Trigger.new){
        if(cont.accoutid != null)
        {
        setid.add(cont.accoutid);
        }
        for(contact con : trigger.new)
     Account acct = [select id,Regione__c from Account where Id in: setid];
     
       if(con.Regione__c == null)

            {         
          Con.Regione__c= acct.Regione__c;
          con.accountid=acct.id;
             }

   }
Adrianne Martinson 5Adrianne Martinson 5
This is test

User-added image

 
def do_stuff_with_number(n):
    print n

the_list = (1, 2, 3, 4, 5)

for i in range(20):
    try:
        do_stuff_with_number(the_list[i])
    except IndexError: # Raised when accessing a non-existing index of a list
        do_stuff_with_number(0)