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
sflearningsflearning 

Trigger to update encrypted fields from Account to Opportunity

Hi...
I want to copy the encryted fields from Account to Opportunity...whenever new opportunity is created and Account  encrypted fields are not null..
Account fields should b copied to opportunity..

Thanks in advance...its urgent..
plz help me with sample code...

 
sflearningsflearning
Here is my code...and its not working....
trigger Update_cc on Opportunity (before insert, before update) {
    
    set<ID> cObjectID = new set<ID>();    
    
    for(Opportunity opp : Trigger.new){
        if(opp.Account != null){
       cObjectID.add(opp.Accountid);
      

        }
    }
    
    if(!cObjectID.isEmpty()){
        
        Map<ID,Account> cObjectMap = new Map<ID,Account>([Select Id, Credit_Card_Number__c,Routing_Number__c,Account_Number__c From Account where Id IN: cObjectID]);
        
        for(Opportunity opp : trigger.new){            
            if( opp.CC__c == null && cObjectMap.get(opp.Accountid) != Null){
                // fill the cc name on Opportunity with cc Name on Account
                opp.CC__c = cObjectMap.get(opp.Accountid).Credit_Card_Number__c;
                opp.Routing_Number__c = cObjectMap.get(opp.Accountid).Routing_Number__c;
                opp.Accounting_Number__c = cObjectMap.get(opp.Accountid).Account_Number__c;
            }
        }
    }
}
AvaneeshAvaneesh
Hi sflearning,

Writing a trigger for you i was create one field in Account Encripted_Text__c and Another field in opportunity named Encripted_Text_Account__c 


 
trigger EncriptedTextToOppourtunity on Opportunity (after insert) {    
    set<Id> cObjectID = new set<Id>();    
    set<Id> cObjectAccount = new Set<Id>();
    for(Opportunity opp : Trigger.new){
        if(opp.Id !=NULL)
        {
            cObjectID.add(opp.Id);
            cObjectAccount.add(opp.AccountId);
        }
    }
       Map<Id,Account> allAccountInfo = new Map<Id,Account>([select id, name,Encripted_Text__c from Account where id IN : cObjectAccount]);
          System.debug('@@@@@@'+allAccountInfo);
          List<Opportunity> newBulkOpp = new List<Opportunity>();
         for(Opportunity opp:[select id,name ,Encripted_Text_Account__c,Accountid from Opportunity where id IN : cObjectID])
         {
           Opportunity O = new Opportunity();
             O.Id=opp.Id;
             O.Encripted_Text_Account__c=allAccountInfo.get(opp.AccountId).Encripted_Text__c;
             newBulkOpp.add(O);
             System.debug('!!!!!!!!!!!!!!!!!!!!!'+allAccountInfo.get(opp.AccountId).Encripted_Text__c);
         }
    	if(newBulkOpp.size()>0)
        {
            update newBulkOpp;
        }
}

Don't forget to marked best answer if this was helpful else let me know 

Thank you
Avaneesh Singh