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
WannabeDeveloperWannabeDeveloper 

How to map multi-select picklist from Account to Contract?

I desperately need to map some custom fields from the Account to a Contract.  How would I go about writing this code?  I can't find anyone who is doing this, but I'm sure it can be done, if someone can help me with the code, or and idea outside of the box!  Please help!!!!!

frederic baudaxfrederic baudax

Hi,

 

Here is some similar trigger except it send data from account to opportunity

 

 

trigger updateopptycurdata on Account (after insert, after update) {

Map<Id,Account> Accounts = new Map<Id,Account>{};
List<Opportunity> OppssToUpdate = new List<Opportunity>{};
Set<Id> AccountIds = new Set<Id>{};

for(Account a :Trigger.new) 
{
 if (a.Opportunity_on_account__c > 0){ 
if (a.Current_Contract_End_Date__c != NULL || a.Current_Operator__c != NULL )  {
    if (trigger.Isupdate && (trigger.new[0].Current_Contract_End_Date__c != trigger.old[0].Current_Contract_End_Date__c || trigger.new[0].Current_Operator__c != trigger.old[0].Current_Operator__c ))  {

      Accounts.put(a.Id, a);
      AccountIds.add(a.Id);
    }
   }
  }
 }  
  If (Accounts.size() > 0){

for(Opportunity o : [SELECT  Id,Isclosed,AccountId from Opportunity WHERE AccountId IN :AccountIds AND IsClosed = FALSE]){

o.Current_contract_end_date__c = Accounts.get(o.AccountId).Current_Contract_End_Date__c; 
o.Current_operator__c = Accounts.get(o.AccountId).Current_Operator__c;

OppssToUpdate.add(o);
  }
 }
If (OppssToUpdate.size() > 0){
try {
 Update OppssToUpdate;} 
 
 catch (System.DmlException e) {
    for (Integer i = 0; i < e.getNumDml(); i++) {
        // Process exception here  
    
        trigger.new[0].addError(e.getDmlMessage(i)); 
    }
  }
 }


}

 Hope this helps you get started.

Kr,

Fred

 

Pradeep_NavatarPradeep_Navatar

Please find below the sample trigger :

 

//if you are updating Account object

trigger AftrUpdate on Account (after update)

{

      if(Trigger.isAfter)

      {

            for(Account objAcc:Trigger.new)

            {

                   for(Contract objCntrct:[select Id,Type__c,AccountId from Contract where AccountId= :objAcc.Id])

                   {

                        objCntrct.Type __c=objAcc. Type __c;

                        update objCntrct;

                   }

            }

      }

}

//if you are in Contract object

trigger Test_Contract on Contract (before insert)

{

      if(Trigger.isBefore)

      {

            for(Contract objCntrct:Trigger.new)

            {

                  String strHobbies = [select Type__c from Account where id= :objCntrct.AccountId].Hobbies__c;

                  objCntrct. Type __c=strHobbies;

            }

      }

}

 

Hope this helps.