If i have two fields say user1 and user2(user look up fields )in account and their email fields in contract,now can anyone give me a basic sample of trigger to update the emails in contract,whenever those users in account are updated?
If you are talking about when the User records on your account are updated (ie you change Account.User1__c from "Mary" to "Jane") then update the contract, then you'll want to do an after update trigger on the Account object and then update the related contracts. I would recommend starting with the Apex Triggers trailhead [1]. And if you have any specific issues with your trigger, please include the code you have written here and the issue you are having and we'll be happy to assit you.
NOTE: When including code please use the "Add a code sample" button (icon <>) to increase readability and make referencing code easier.
I have tried this,but getting error could you please help me out??
//trigger to update the contract emails whenever the users in the correspoding account related to the contract change// [Error: Compile Error: Invalid field Account_executive_email__c for SObject Account at line 13 column 54]
<Add a code sample>
trigger updatecontractemails on Account (after insert,after update) {
List<contract> contIds = New List<contract>();
if(Trigger.isInsert && Trigger.isAfter){
List<contract> ct = new List <contract>();
for(Account acc : trigger.new){
contract c = new contract(Name = acc.name +'contract', AccountId=acc.id, Account_executive_email__c = acc.Account_executive_email__c, Account_manager_email__c = acc.Account_manager_email__c );
ct.add(c);
} if(!ct.isEmpty()) insert ct;
}
if(Trigger.isUpdate && Trigger.isAfter) { for(contract con:[select id,Account_executive_email__c,Account_manager_email__c,AccountId from contract where Accountid IN :Trigger.NewMap.Keyset()]) {
So the problem you are seeing there is that the field you have in your trigger is not on your account object. It's likely that you're just using the wrong field name. If you look at the account fields you should see what the name is. Additionally, you are doing this a little bit wrong. I've taken the liberty to update your code below.
trigger updateContractEmails on Account (after update) {
Set<Id> accountIds = new Set<Id>();
for (Account acct : Trigger.new) {
Account oldAccount = Trigger.oldMap.get(acct.Id);
if (
oldAccount.Executive_email__c != acct.Executive_email__c ||
oldAccount.Manager_email__c != acct.Manager_email__c
) {
accountIds.add(acc.Id);
}
}
if (!accountIds.isEmpty()) {
List<Contract> contractsToUpdate = [
select AccountId,
Account_executive_email__c,
Account_manager_email__c
from Contact
where AccountId in :accountIds
];
for (Contact cont : contractsToUpdate) {
Account acct = Trigger.newMap.get(cont.AccountId);
cont.Account_executive_email__c = acct.Executive_email__c;
cont.Account_manager_email__c = acct.Manager_email__c;
}
if (!contractsToUpdate.isEmpty()) {
update contractsToUpdate;
}
}
}
NOTE: This code has not been tested and may contain typographical or logical errors
You will need to update the fields Executive_email__c and Manager_email__c to be the correct field name that is on your account object.
NOTE: When including code please use the "Add a code sample" button (icon <>) to increase readability and make referencing code easier.
[1] https://developer.salesforce.com/trailhead/en/module/apex_triggers
//trigger to update the contract emails whenever the users in the correspoding account related to the contract change//
[Error: Compile Error: Invalid field Account_executive_email__c for SObject Account at line 13 column 54]
<Add a code sample>
trigger updatecontractemails on Account (after insert,after update) {
List<contract> contIds = New List<contract>();
if(Trigger.isInsert && Trigger.isAfter){
List<contract> ct = new List <contract>();
for(Account acc : trigger.new){
contract c = new contract(Name = acc.name +'contract',
AccountId=acc.id,
Account_executive_email__c = acc.Account_executive_email__c,
Account_manager_email__c = acc.Account_manager_email__c
);
ct.add(c);
}
if(!ct.isEmpty())
insert ct;
}
if(Trigger.isUpdate && Trigger.isAfter)
{
for(contract con:[select id,Account_executive_email__c,Account_manager_email__c,AccountId from contract where Accountid IN :Trigger.NewMap.Keyset()])
{
con.Account_executive_email__c = Trigger.NewMap.get(con.AccountId).Account_executive_email__c;
con.Account_manager_email__c = Trigger.NewMap.get(con.AccountId).Account_manager_email__c;
contIds.add(con);
}
update contIds;
}
}
NOTE: This code has not been tested and may contain typographical or logical errors
You will need to update the fields Executive_email__c and Manager_email__c to be the correct field name that is on your account object.