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
RamkumarVTRRamkumarVTR 

country pick list to default based on market custom picklist of user- Uing Trigger Upate

I need to write trigger to update the custom picklist name" Country__c" of Custom object call Medical__c from the "User' Object's custom field "market _c" Value. The Cusom object is having the custom field picklist "State_c " which dependent of " Country__c".
justin_sfdcjustin_sfdc
Hi RamKumar,

You can do this trigger. Do you want this change to happen when the value on Market__c of User object is changed?

trigger test on User__c(after update) {
list<id> userId= new list<id>();
 for(User u: trigger.new()) {
    if(u.Market__c!= Trigger.oldMap.get(u.market__c)) {
          userId.add(u);
     }
  }
 List<Medical__c> md= new List<Medical__c>();
 for (Medical__c m: [SELECT id, country__c, state__c, user__c from Medical__c WHERE user__c IN: userId]) {
    m.country__c='whatever value you want';
    m.state__c='whatever value you want';
    md.add(m);
 }
 if (md.size()>0) {
    update md;
 }
}

Hope that helped!
Thanks,
justin~sfdc
RamkumarVTRRamkumarVTR
Hi Justin,

Thanks for your reply. Actualy we need to update the country of medical object, when they creat a new record of medical object. The country value is should be the same as the current user who is creating the new medical record. Please advise the same.
justin_sfdcjustin_sfdc
Hi Ramkumar,
why don't you make your country field to be a formula field to the country field in User object. In that way, that record will always be updated to the country of that user.
Or is your requirement more like you have to make the country defaulted to the user's country field at the time of insert and then again you can change it to anything else?