You need to sign in to do that
Don't have an account?
Scott2014
Trigger to Update Contact Record Owner with Account Record Owner
Can someone help me with a trigger that would update the contact record owner with account record owner automatically? I am not too familiar with triggers, hence why I am asking for help, and desperately want to stay away from ongoing data loads. Thank you in advance for anyone that can assist!
See the links below which have some sample code,
http://salesforce.stackexchange.com/questions/30912/salesforce-apex-update-object-and-all-child-object-records-associated-to-parent
https://developer.salesforce.com/forums/ForumsMain?id=906F000000092LaIAI
https://success.salesforce.com/answers?id=90630000000gkOnAAI
Regards,
Ashish
Error: Compile Error: unexpected token: 'trigger' at line 1 column 0
trigger reassignContactOwnerToAccountOwner on Contact ( before insert, before update ) {
List<Id> accountIds = new List<Id>();
Map<Id, Id> accountOwnerIdMap = new Map<Id, Id>();
// all the accounts whose owner ids to look up
for ( Contact c : Trigger.new ) {
accountIds.add( c.accountId );
}
// look up each account owner id
for ( Account acct : [ SELECT id, ownerId FROM account WHERE id IN :accountIds ] ) {
accountOwnerIdMap.put( acct.id, acct.ownerId );
}
// change contact owner to its account owner
for ( Contact c : Trigger.new ) {
c.ownerId = accountOwnerIdMap.get( c.accountId );
}
}
trigger reassignContact on Contact (before insert, before update) {
try {
Set<Id> accountIds = new Set<Id>();
Map<Id, Id> accountOwnerIdMap = new Map<Id, Id>();
// all the accounts whose owner ids to look up
for ( Contact c : Trigger.new ) {
if(c.accountId <> null){
accountIds.add( c.accountId );
}
}
// look up each account owner id
for ( Account acct : [ SELECT id, ownerId FROM account WHERE id IN :accountIds ] ) {
accountOwnerIdMap.put( acct.id, acct.ownerId );
}
// change contact owner to its account owner
for ( Contact c : Trigger.new ) {
if(c.AccountId <> null){
c.ownerId = accountOwnerIdMap.get( c.accountId );
}
}
} catch(Exception e) { //catch errors
System.Debug('reassignContacts failure: '+e.getMessage()); //write error to the debug log
}
}
[App Setup]--[Customize]--[Contacts]--[Triggers]--New
I don't want it to "trigger" or update contact owners in the event that the account record type is a "Channel Partner Account".
$RecordType.Name="Channel Partner Account"
Thank you!
You can use a for loop with the condition
If (recordtypeid !=particularID){
//Process
}
See some sample example in the link below,
You can either use : RecordTypeId!='01220000000MEEW',
Or
Id rtId = [select Id,name from RecordType where name='Label of the record type'
https://developer.salesforce.com/forums?id=906F00000008zXfIAI
Regards,
Ashish
1)
trigger reassignContact on Contact (before insert, before update) {
try {
If (recordtypeid !=012G00000014AxX){
//Process
}
Set<Id> accountIds = new Set<Id>();
Map<Id, Id> accountOwnerIdMap = new Map<Id, Id>();
// all the accounts whose owner ids to look up
for ( Contact c : Trigger.new ) {
if(c.accountId <> null){
accountIds.add( c.accountId );
}
}
// look up each account owner id
for ( Account acct : [ SELECT id, ownerId FROM account WHERE id IN :accountIds ] ) {
accountOwnerIdMap.put( acct.id, acct.ownerId );
}
// change contact owner to its account owner
for ( Contact c : Trigger.new ) {
if(c.AccountId <> null){
c.ownerId = accountOwnerIdMap.get( c.accountId );
}
}
} catch(Exception e) { //catch errors
System.Debug('reassignContacts failure: '+e.getMessage()); //write error to the debug log
}
}
Error: Compile Error: expecting a right parentheses, found 'G00000014AxX' at line 4 column 30
2)
trigger reassignContact on Contact (before insert, before update) {
try {
Set<Id> accountIds = new Set<Id>();
Map<Id, Id> accountOwnerIdMap = new Map<Id, Id>();
Id rtId = [select Id,name from RecordType where name='Business Account');
// all the accounts whose owner ids to look up
for ( Contact c : Trigger.new ) {
if(c.accountId <> null){
accountIds.add( c.accountId );
}
}
// look up each account owner id
for ( Account acct : [ SELECT id, ownerId FROM account WHERE id IN :accountIds ] ) {
accountOwnerIdMap.put( acct.id, acct.ownerId );
}
// change contact owner to its account owner
for ( Contact c : Trigger.new ) {
if(c.AccountId <> null){
c.ownerId = accountOwnerIdMap.get( c.accountId );
}
}
} catch(Exception e) { //catch errors
System.Debug('reassignContacts failure: '+e.getMessage()); //write error to the debug log
}
}
Error: Compile Error: expecting right square bracket, found ')' at line 6 column 79
Try using as a string
If (recordtypeid !="012G00000014AxX") {
//code;
}
Could not identify which is line 6 , can you copy paste the code on line 6 for easy access.
Regards,
Ashish
Line 6: Id rtId = [select Id,name from RecordType where name='Business Account');
There is a syntax error the SOQL starts with a [ square bracket and ends with a ) after business account, update and same and see if that solves the issue.
Id rtId = [select Id,name from RecordType where name='Business Account'];
Regards,
Ashish
public static void insertAccRecordTpes(List<Account> listAccount){
//Update record type
Map<Id, RecordTypeInfo> recordTypes = Account.SObjectType.getDescribe().getRecordTypeInfosById();
//System.debug('recordTypes'+recordTypes);
List<Account> accObj = new List<Account>();
for(Account record: listAccount) {
if(record.RecordTypeId != null) {
string recordTypeName = Schema.SObjectType.Account.getRecordTypeInfosById().get(record.recordtypeid).getname();
//System.debug('recordTypeName'+recordTypeName);
if(recordTypeName == 'Brand') {
accObj.add(record);
} else if(recordTypeName == 'Merchant Owner') {
accObj.add(record);
}}}
Set<Id> UpdatedAccountOppIds = new Set<Id>();
set<id> ownerId = new Set<Id>();
for(Account acc:accObj){
if(acc.ParentId!=null){
UpdatedAccountOppIds.add(acc.ParentId);
ownerId.add(acc.OwnerId);
System.debug('Id>>>>>>>'+acc.Id);
System.debug('ParentId>>>>>>>'+acc.ParentId);
System.debug('Chid Account Name>>>>>>>'+acc.Name);
}
}
}
public static void parentAccwithChildAccUpdated(List<Account> listAccount2){
Map < Id, Account > mapAccount = new Map < Id, Account >();
List<Account> listAccount = new List<Account>();
for(Account acct :listAccount2)
mapAccount.put(acct.Id, acct);
System.debug('>>>>mapAccount>>>'+mapAccount);
listAccount = [ SELECT ID,ParentId,Name,Status__c,AccountManager__c
FROM Account WHERE ParentId IN : mapAccount.keySet() ];
for ( Account opp : listAccount ) {
//opp.Status__c = mapAccount.get(opp.AccountId).Status__c;
opp.AccountManager__c = mapAccount.get(opp.ParentId).AccountManager__c;
opp.ownerId = mapAccount.get( opp.ParentId ).ownerId;
}
update listAccount;
System.debug('>>>parentAccwithChildAccUpdated>>>'+listAccount);
}
public static void parentAccWithChildOpportunityUpdated(List<Account> listAccount2){
Map < Id, Account > mapAccount = new Map < Id, Account >();
List<Opportunity> listOpportunity = new List<Opportunity>();
for(Account acct :listAccount2)
mapAccount.put(acct.Id, acct);
System.debug('>>>>mapAccount>>>'+mapAccount);
listOpportunity = [ SELECT ID,AccountId,ownerId,Name,Status__c,AccountManager__c
FROM Opportunity WHERE AccountId IN : mapAccount.keySet() ];
for ( Opportunity opp : listOpportunity ) {
if (opp.AccountId !=null){
//opp.AccountManager__c = mapAccount.get(opp.AccountId).AccountManager__c;
opp.ownerId = mapAccount.get( opp.AccountId ).ownerId;
}}
update listOpportunity;
System.debug('>>>parentAccWithChildOpportunityUpdated>>>'+listOpportunity);
}
public static void parentAccwithChildOppStatusUpdated(List<Account> listAccount3){
Set<Id> accountIds = new Set<Id>();
for(Account ac : listAccount3) {
if(ac.Status__c=='Do Not Change')
accountIds.add(ac.Id);
System.debug('accountIds'+accountIds);
}
List<Opportunity> oppsToUpdate = new List<Opportunity>();
for(Opportunity opp : [select id, StageName from Opportunity where AccountId in: accountIds]){
opp.StageName='In Pilot';
oppsToUpdate.add(opp);
}
update oppsToUpdate;
System.debug('>>>>parentAccwithChildOppStatusUpdated>>>>>>'+oppsToUpdate);
}
}