You need to sign in to do that
Don't have an account?
Jane Nicolas
Trigger to update a field with values of other field in other object
Hi
This is Jane here I had a scenario where Feild 1 of A object needs to be updated with same value of the Field 2 in Object B , whenever an update or insert of Field 2 occurs. I tried following Trigger But it doesn't seem to help. Can anyone suggest ? Thanks.
trigger OpportunityTrigger on Opportunity (after insert, after update)
{
Set<Id> accountIds = new Set<Id>();
for(Opportunity opp : Trigger.new)
{
accountIds.add(opp.AccountId);
}
Map<ID, Account> mapAccounts = new Map<ID, Account>([SELECT Id, Plan_Number__c FROM Account where Id IN :accountIds]);
List<Account> lstAccToUpdate = new List<Account>();
For(Opportunity opp : Trigger.new)
{
Account acc = mapAccounts.get(opp.AccountId);
acc.Plan_Number__c = opp.Plan_Number__c;
lstAccToUpdate.add(acc);
}
update lstAccToUpdate;
Map<Id, Account> mapUpdatedAccounts = new Map<Id,Account>();
For(Account acc : lstAccToUpdate)
{
mapUpdatedAccounts.put(acc.Id,acc);
}
}
This is Jane here I had a scenario where Feild 1 of A object needs to be updated with same value of the Field 2 in Object B , whenever an update or insert of Field 2 occurs. I tried following Trigger But it doesn't seem to help. Can anyone suggest ? Thanks.
trigger OpportunityTrigger on Opportunity (after insert, after update)
{
Set<Id> accountIds = new Set<Id>();
for(Opportunity opp : Trigger.new)
{
accountIds.add(opp.AccountId);
}
Map<ID, Account> mapAccounts = new Map<ID, Account>([SELECT Id, Plan_Number__c FROM Account where Id IN :accountIds]);
List<Account> lstAccToUpdate = new List<Account>();
For(Opportunity opp : Trigger.new)
{
Account acc = mapAccounts.get(opp.AccountId);
acc.Plan_Number__c = opp.Plan_Number__c;
lstAccToUpdate.add(acc);
}
update lstAccToUpdate;
Map<Id, Account> mapUpdatedAccounts = new Map<Id,Account>();
For(Account acc : lstAccToUpdate)
{
mapUpdatedAccounts.put(acc.Id,acc);
}
}
So when we update or insert opportunty then we should find the related accound and then we should update the accountf feidl with opportunity foeld correct ? please confirm If I am correct..
Thanks
Sandeep
Account acc = mapAccounts.get(opp.AccountId);
acc.Plan_Number__c = opp.Plan_Number__c;
lstAccToUpdate.add(acc);
replace with below lines
Please check and let me know if it works...
Thanks