+ Start a Discussion
Prashant GulvePrashant Gulve 

On creation of Contact New Record website field should get auto populate, but it's not reflecting for account parent website.

Hi Team Need your Help on this trigger detail info provide please help..

On this trigger while creatig new contact record when i select Child Sham record on 'Account Name' field then Parent website getting auto populate on contact website but when i Select Parent Account Ram in contact 'Account Name' filed then it's showing Contact Website field blank. so it should reflect parent website in both scenario. Below is details
[when ever new contact record will created parent account website filed should get update on contact. even if child selected]

Created a trigger on Contact object (before update) to auto populate  Website__c field on creation of New Contact record. on update of Account Parent -Account-Website field and Child Account Website field.
1- I have created Website Field in both account and conatct object data type URL
2- Account Parent Record created. e.g- Ram and website filed updated www.Ram@pareant.com
3- Account Child record created. e.g- Sham and Parent account name is Ram selected website updated www.sham@child.com
4- On New Contact creation always Pareant Account Website should get display on contact object. e.g- www.Ram@pareant.com  even if we select Sham on contact object Account name field still www.Ram@pareant.com should get display on Contact object website filed.
Trigger:-
trigger WebsiteAutopopuoonContactTrigger on Contact (before insert) {
    WebsiteAutopopuoonContact.createWebsite(trigger.new);
  }
Handler:-
public class WebsiteAutopopuoonContact {
    public static void createWebsite(List<Contact> contList) {
        
       Set<Id> accSet = new Set<Id>();
        
        for(Contact Con : contList) {
            accSet.add(Con.AccountId);
            System.debug('accSet===='+accSet);
        }
        List<Account> accList = [Select Id
                                       , Name
                                        , Website
                                         , Account.Parent.Website
                                 from Account where Id IN:accSet];
        
        map<Id, String> idvswebsite = new map<Id, String>();
        for(Account acc : accList){
            idvswebsite.put(acc.id, acc.Parent.Website);
        }
        System.debug('accList==='+accList);
        
        Map<Id,Contact> conNameKeys = new Map<Id,Contact>();
        for(Contact Con : contList) {
            conNameKeys.put(Con.AccountId, Con);
            Con.Website__c = idvswebsite.get(Con.AccountId);
            System.debug('conNameKeys==='+conNameKeys);
        }
    }
}
 
Best Answer chosen by Prashant Gulve
AnkaiahAnkaiah (Salesforce Developers) 
Hi Prasanth,

try with below code.
trigger WebsiteUpdate on Contact (before insert,before update) {
    
    set<id> accids = new set<id>();
    if(trigger.isbefore && (trigger.isinsert || trigger.isupdate)){
        
    for(contact con:trigger.new){
        
        if(con.accountid != NULL){
            accids.add(con.accountid);
        }
     }
   }    
    map<id,account> accmap = new map<id,account>([SELECT id,parentid,website__c,parent.Website__c from account where ParentId!=Null AND id=:accids]);
    
    map<id, string> parentaccmap = new map<id,string>();
    for(account acc: accmap.values()){
        
       parentaccmap.put(acc.ParentId,acc.parent.Website__c);
        
    }
    
    for(contact con:trigger.new){
        for(Account acc :accmap.values()){
            
            if(con.accountid==acc.id && parentaccmap.containsKey(acc.ParentId) ){
                con.Website__c = parentaccmap.get(acc.ParentId);
            }
        }
    }
    
    

}

If this helps, Please mark it as best answer.

Thanks!!​​​​​​​

All Answers

Maharajan CMaharajan C
Your requirement is not clear Prashant...Please explain clearly...

Thanks,
Maharajan.C
AnkaiahAnkaiah (Salesforce Developers) 
Hi Prasanth,

try with below code.
trigger WebsiteUpdate on Contact (before insert,before update) {
    
    set<id> accids = new set<id>();
    if(trigger.isbefore && (trigger.isinsert || trigger.isupdate)){
        
    for(contact con:trigger.new){
        
        if(con.accountid != NULL){
            accids.add(con.accountid);
        }
     }
   }    
    map<id,account> accmap = new map<id,account>([SELECT id,parentid,website__c,parent.Website__c from account where ParentId!=Null AND id=:accids]);
    
    map<id, string> parentaccmap = new map<id,string>();
    for(account acc: accmap.values()){
        
       parentaccmap.put(acc.ParentId,acc.parent.Website__c);
        
    }
    
    for(contact con:trigger.new){
        for(Account acc :accmap.values()){
            
            if(con.accountid==acc.id && parentaccmap.containsKey(acc.ParentId) ){
                con.Website__c = parentaccmap.get(acc.ParentId);
            }
        }
    }
    
    

}

If this helps, Please mark it as best answer.

Thanks!!​​​​​​​
This was selected as the best answer
Prashant GulvePrashant Gulve
Hi Ankaiah,
Thank you for help, but code is not working for parent account Website, it's working for child only. so mine code is also working for child account record only. the our purpose is to Account Parent website should get auto populate on Contact website field on creation of new contact record and selecting "Account Name" is Pareant e.g Ram so it should display www.ram@parent.com only in any scnario. using this code if i select parent Account Name Ram then Contact website showing blank, it should reflect  www.ram@pareant.com.
AnkaiahAnkaiah (Salesforce Developers) 
Hi Prasanth,

I have tested from my end, It working as per your requirement.

1.I have created parent account with website = www.ram@parent.com
2.I have created child account with webisite =www.sham@child.com and linked this account to parent account.
3. I have created/updated a contact for child account and the website name autopopulated from parent account and value is www.ram@parent.com.

Please follow these steps, it should work. 

Thanks!!

 
Prashant GulvePrashant Gulve
Yes, Thats correct but while creating new Contact Record when you select "Account Name" is parent Ram then it's showing website field blank it suppsed to display www.ram@parent.com. 
so the code i have created it's working for Child Account name but not for Parent so for that i am stuck can you please help me out of that.
AnkaiahAnkaiah (Salesforce Developers) 
Hi Prashanth,

Try with below code.
trigger WebsiteUpdate on Contact (before insert,before update) {
    
    set<id> accids = new set<id>();
    if(trigger.isbefore && (trigger.isinsert || trigger.isupdate)){
        
    for(contact con:trigger.new){
        
        if(con.accountid != NULL){
            accids.add(con.accountid);
        }
     }
   }    
    map<id,account> accmap = new map<id,account>([SELECT id,parentid,website__c,parent.Website__c from account where id=:accids]);
    
    
    system.debug('accmap=='+accmap.values());
    
    map<id, string> parentaccmap = new map<id,string>();
    for(account acc: accmap.values()){
        if(acc.parentid != NULL){
            parentaccmap.put(acc.ParentId,acc.parent.Website__c);       
        }
        
    }
    
    system.debug('parentaccmap=='+parentaccmap);
    
    for(contact con:trigger.new){
        for(Account acc :accmap.values()){
            
            if(con.accountid==acc.id && parentaccmap.containsKey(acc.ParentId) ){
                con.Website__c = parentaccmap.get(acc.ParentId);
            }else if(con.accountid==acc.id){
                con.Website__c = acc.Website__c;
            }
        }
    }  

}

If this helps, Please mark it  as best answer.

Thanks!!
Prashant GulvePrashant Gulve
Hi Ankaiah,
Thank you so much for your Help Code is working for Both Parent and Child record.
Thanks once and marked as Best Answer..