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
Kamatchi Devi RKamatchi Devi R 

Trigger to update the owner and Lookup fields that refer salesforce user

Hi,

 

I have a trigger that auto populate the account owner and a lookup field that refers the user of salesforce account.

Now its working while the user id is matched by comparision in trigger while creating the account.

 

But, I need to auto populate the owner by giving the username directly through data loader. Its showing an error like too many sql Queries: 20001. And i need the way to update the owner and lokup field while giving the name directly.

  

Following is the Trigger for Auto Populating the Account Object fields including  the Owner and Lookup,

 

trigger demo on Account (before insert)
{
List<Account> accountsWithContacts;
Map<Id, String> ownerMap = new Map<Id, String>();
ID mangerid; ID vestid; ID accowner;

 

accountsWithContacts=Trigger.new;
List<Account_Trigger__c> l=[select Id, ACCOUNT_MANAGER__c,AccountOwner__c,Agency_Category__c,Region__c,RegionalRep__c,State__c,SubRegion__c,VEST__c from Account_Trigger__c];
List<AccountTriggerforabove100__c> lt=[select Id, Account_Manager__c,Account_Owner__c,Agency_Category__c,Region__c,Regional_Rep__c,State__c,SubRegion__c,VEST__c from AccountTriggerforabove100__c];
List<User> u=[select Id,Name from User ];

for(Account a:accountsWithContacts)
{
for(Account_Trigger__c at:l)
{
for(User us:u)
{
if(us.Name==a.Account_Manager1__c)
mangerid=(ID)us.Id;
if(us.Name==a.VEST__c)
vestid=(ID)us.Id;
}

if(a.BillingState==at.State__c )
{
if(a.RecordTypeId =='01290000000SUc5' || a.RecordTypeId =='01290000000SUcA' || a.RecordTypeId =='01290000000SUcK' || a.RecordTypeId =='01290000000SUcF' )
{
if(a.Number_of_Officers__c < 100 && a.Number_of_Officers__c > 0)
{
a.Sub_Region__c=at.SubRegion__c;
a.Regional_Rep__c=at.RegionalRep__c;
a.Region__c=at.Region__c;
a.Agency_Category__c=at.Agency_Category__c;
a.OwnerId=accowner;
a.Account_Manager1__c=mangerid;
a.VEST__c=vestid;
}

}
}

}

for(AccountTriggerforabove100__c aa :lt)
{

if(a.BillingState==aa.State__c )
{
if(a.RecordTypeId =='01290000000SUc5' || a.RecordTypeId =='01290000000SUcA' || a.RecordTypeId =='01290000000SUcK' || a.RecordTypeId =='01290000000SUcF' )
{
if(a.Number_of_Officers__c >= 100 )
{
/* User u=[select Id,Name from User where Name=:aa.Account_Owner__c limit 1];
User u1=[select Id,Name from User where Name=:aa.Account_Manager__c limit 1];
User u2=[select Id,Name from User where Name=:aa.VEST__c limit 1];*/
a.Sub_Region__c=aa.SubRegion__c;
a.Regional_Rep__c=aa.Regional_Rep__c;
a.Region__c=aa.Region__c;
a.Agency_Category__c=aa.Agency_Category__c;
a.OwnerId=accowner;
a.Account_Manager1__c=mangerid;
a.VEST__c=vestid;
}

}
}
}
}
}

Here i refer a custom setting object to store the details of account back end.

And OwnerID, Account_Manager1__c and VEST__c are the fields that refers Salesforce User name.

So, please guide me how to update the user name in owner or lookup fields.

 

Thanks in Advance,

Kamatchi Devi R