please bare with me am new to triggers. lead and account are the two objects. by using before insert i have to update the fields like lead object: name,password into account : name,password. my code is updating the password in account and it is encrypting **** only after the update is done. i want to do the whole thing using before insert. please can u guys help me .

trigger passwordleadtocontact on Lead (before update) {
    set<string> sfidset= new set <string>();
        map<string,string> sfoldnew =new map<string,string>();
        for(lead ld : Trigger.new){
             if(ld.password__C !=trigger.oldmap.get(ld.id).password__C){
                sfidset.add(trigger.oldmap.get(ld.id).password__c );
                    ld.password__c='*******';   }
      list<account> aclist=[select password__c from account where password__c IN:sfidset];
        for(account ac :aclist){
        update aclist;

updated code :

trigger Password_update on Lead (before insert,before update) {
    set<string> sfidset = new set<string>();
    map<string,string> sfoldnew = new map<string,string>();
    map<string, Account> userToCustomSetting = new map<string, Account>();
    list<account> newsettings = new list<account> ();
    list<Account> settings =  [SELECT  Id,Name,Password__c FROM   Account  ];
    list<Account> validApplicationSettings = new list<Account>();            

    if (!settings.isEmpty()){
        for (Account s : settings){
    for (lead stg : trigger.new){
        if ((stg.Password__c != null && stg.Password__c  != '' )){

            if (trigger.isInsert){


            else if (trigger.isUpdate){
                if (stg.Password__c != '************' && stg.Password__c != trigger.oldMap.get(stg.id).Password__c)
    if (!validApplicationSettings.isEmpty()){
            for (lead stg : validApplicationSettings){
             if( stg.password__c != null  ){
                    newSettings.add(new account(Name = string.valueof(stg.id), Password__c = stg.Password__c));
                    stg.Password__c = '************';

            if (!newSettings.isEmpty() && validApplicationSettings[0].id != null){
                insert newSettings;

    for(Lead ld : Trigger.new){
        if(ld.password__c != Trigger.oldmap.get(ld.id).password__c){


    if(!newsettings.isempty())  {
        for(Lead ld : newsettings ){
            if(ld.password__c != null ){
                settings.add(new account(password__C =ld.password__C ,id=ld.id , name=ld.company));


    list<Account> acclist =[select password__c from Account where password__C IN:sfidset];

    for(Account  ac : acclist){
        if(sfoldnew.containskey (ac.password__C))
            ac.password__C = sfoldnew.get (ac.password__C);
    update acclist;  

closed as unclear what you're asking by Himanshu, codeyinthecloud, Derek F, glls, battery.cord Jan 7 at 15:10

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.


This application design does not make sense and has some serious security problems. I suspect (and hope) that these issues stem from a miscommunication of the requirements to you, and that clarifying the design requirements will also deal with the issues. I'm not going to correct the code but just draw out several key issues.

Encryption, Masking, and Deleting Are Not The Same Thing

Overwriting a field with asterisks ('******') is not a form of encryption. It is also not masking, where some or all of the real data is stored but is obscured, as it may be in some Salesforce Classic Encrypted Fields. You are simply deleting the data from your Lead.

Storing Passwords in Plain Text is Bad

Enough said; it's a security nightmare.

Using Passwords as a Unique Identifier is Very Bad

Your original version of your trigger, and the comments, indicate that the only link between a Lead and an Account is sharing the contents of the Password__c field. This is a terrible idea, because two entities may choose the same password, and you're creating potential password-disclosure attacks against your application.

  list<account> aclist=[select password__c from account where password__c IN:sfidset];

This query will return more than one record if two Accounts have the same password. What do you do then? You risk setting Account A's password and Account B's password to the same value if Account A changes his password.

If you detect collisions and throw an error, you're telling Account A that somebody else has the same password as him, or that he's just chosen a password that somebody else is using. That's a disaster.

Code Issues

Your second version will not compile for a number of reasons, including type mismatches between Account and Lead.

Storing the Lead Id as the Account Name strikes me as very unlikely to be correct behavior.

You are running queries without filters, which risk performance issues and non-selectivity problems in a larger Salesforce org.

I really suggest you go back to the drawing board and get clarity on how this thing is supposed to work.

Not the answer you're looking for? Browse other questions tagged or ask your own question.