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
sudha76sudha76 

Autopopulate the Lookup field on Accounts with a Trigger.

On Accounts, there is a related list called - Account Technical Profile . One Account can only have one ATP record. It will never have more than one ATP record.

There is a master details relationship between Account and ATP custom object.

 

I have another lookup field on Account - Copy_TechnicalProfile. I would like this field to be populated when the Account has an ATP record created.  Here is my trigger.

 

trigger Capture_AccountTechnicalProfile on Account (before update) {

Set<Id> accIds = new Set<Id>();
Map(Id, Account> accmap;

for(Account acct: trigger.new)
{
    if(acct.Account_Technical_Profile__Id != null)
    {
        accIds.add(acct.Account_Technical_Profile__Id);
    }

accmap = [select Id, Copy_Account_Technical_Profile__c FROM Account where Account_Technical_Profile__Id IN : accIds];

    for(Account acct : trigger.new)
    {
        if(acct.AccountId !=null && accmap.containsKey(acct.Account_Technical_Profile__Id)
        {
            
            Copy_Account_Technical_Profile__c = accMap.get(acct.Account_Technical_Profile__Id).Copy_Account_Technical_Profile__c;
        }
}

 

 

I am getting this error message:-

 

Error

Error: Compile Error: unexpected token: 'Map' at line 5 column 0

 

 

 

 

 

sudha76sudha76

i made more changes, :-

 

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

 

 

trigger Capture_AccountTechnicalProfile on Account (before update) {

Set<Id> accIds = new Set<Id>();
Map<Id, Account> accmap = new Map <Id, Account>();

for(Account acct: trigger.new)
{
    if(acct.Account_Technical_Profile__Id != null)
    {
        accIds.add(acct.Account_Technical_Profile__Id);
    }

accmap = [select Id, Copy_Account_Technical_Profile__c FROM Account where Account_Technical_Profile__Id IN : accIds];

    for(Account accttechy : trigger.new)
    {
        if(accttechy.AccountId !=null && accmap.containsKey(accttechy.Account_Technical_Profile__Id)
        {
           
            Copy_Account_Technical_Profile__c = accMap.get(accttechy.Account_Technical_Profile__Id).Copy_Account_Technical_Profile__c;
        }
        }
}

 

////////////////////////////////////////////////////////////////////////

 

 

 

 

BALA_RAMBALA_RAM

TRY this code defenitly u get the solution for Autopopulation just you have to make Looup relation with User

 

trigger populateContactfromUser on Contact (before insert , before update){
    
    Set<ID> setConIds = new Set<ID>();
    for(Contact obj : trigger.new){
        if(obj.User_Name__c!= null)
        setConIds.add(obj.User_Name__c);
    }
    
     MAP<ID , User> mapCon = new MAP<ID , User>([Select Id,Email,Phone from User where id in: setConIds]);
     for(Contact obj : trigger.new)
       {
        if(obj.User_Name__c != null)
          {
            User c = mapCon.get(obj.User_Name__c);
            obj.Email= c.Email;
            //Similarly you can assign Address fields as well, just add those field in Contact SOQL as well
          }
       
       }
}