You need to sign in to do that
Don't have an account?
Update Triggers
Hi!
I have created 2 custom objects .with the help of community , i have written a trigger to insert
values from 1 custom object to another now i have to do update method for the same
(I have created 2 custom obj both have same api names and same fields), I have to update records in Member Object and Member 2 record will be automatically updated through trigger
please help me to write a code for update.
below Code I have written is not working
trigger updt on Member__c (after update)
{list<Member2__c> mem = new List<Member2__c>();
for (Member__c a1:Trigger.new)
{
Update mem;
}
}
There is no reason that this trigger will not work are you sure you only have one record for Member2__c
try
this
this will Show you size as well, let me know with results.
All Answers
Your code is in right direction but not complete
You must be having a reference field please let me know the field api name also for that , we need to update this code accordingly then.
Hi
Thanks for the reply,
but this code is still not working
As i am not able to update fields of another object automatically
trigger updt on Member__c (after update)
{
list<Member2__c> mem = new List<Member2__c>();
for (Member__c a1 : Trigger.new) {
Member2__c mem2 = new Member2__c();
mem2.FirstName__c=a1.FirstName__c;
mem.add(mem2); }
upsert mem;
}
this code is not displaying any errror but not updating fields of Member2 as i updated in Member, please help me to get the appropriate code
Are you having any lookup or master detail relation ship between these two objects?
if so please let me know the API Name of the field and some iformation that which object has this field.
Hi
I have not created any master detail or look up fields in both the objects
Then how can we decide which record of Member2 needs to be updated when any records of Member is updated. Means whenever we update a Member Record we have to pick the corresponding Member2 record so that we can copy information into it. So I would suggest you to create a lookup relationship field in Member2.
Let me know if any issues in it.
Hi
Thanks for the earlier reply
as both objects have same datatype and fieldnames, such FirstName, Lastname if i will update or edit Firstname of Member object than Firstname of Member2 should also be updated.
Do you have only single record in both object, or records can be more than one?
Like see ane example Req is When ever a Contact Name is updated It's Account Name should also be updated , here when we check that Name is Updated on Contact we will check which account is accoiciated with it and then update that account. Now to find that assciation we require any reference field. Similar in your case if records are more than one then we need to find which is the associated Member2 record.
Hi
thanks for your earlier reply
ERROR (I created new record in member object, and was automatically inserted in Member2 object, now wen i tried to edit my same record in member object an error below is displaying)
Apex trigger updt caused an unexpected exception, contact your administrator: updt: execution of AfterUpdate caused by: System.ListException: Duplicate id in list: a0F90000002NHPVEA4: Trigger.updt: line 10, column 4
Error: Invalid Data.
Review all error messages below to correct your data.
}
trigger updt on Member__c (after update)
{
list<Member2__c> mem = [Select id from Member2__c Limit 1];
for (Member__c a1 : Trigger.new)
{
Member2__c mem2 = mem.get(0);
mem2.FirstName__c=a1.FirstName__c;
mem.add(mem2);
}
update mem;
Ok then try this it will do your work
let me know with your results
Hi
Apex trigger updt caused an unexpected exception, contact your administrator: updt: execution of AfterUpdate caused by: System.ListException: Before Insert or Upsert list must not have two identically equal elements: Trigger.updt: line 9, column 1
Review all error messages below to correct your data.
above error is displaying, while trying to update firstname in member object
Try this
let me know if nay issues in it.
Hi
thanks for ur help but its also dispalying an error
Apex trigger updt caused an unexpected exception, contact your administrator: updt: execution of AfterUpdate caused by: System.ListException: Duplicate id in list: a0F90000002NHPVEA4: Trigger.updt: line 13, column 8
Error: Invalid Data.
Review all error messages below to correct your data.
trigger updt on Member__c (after update)
{
list<Member2__c> mem = [Select id from Member2__c Limit 1];
Set<ID> setMemID = new Set<ID>();
for (Member__c a1 : Trigger.new)
{
Member2__c mem2 = mem.get(0);
mem2.FirstName__c=a1.FirstName__c;
if(setMemID.add(mem2.id))
{
mem.add(mem2);
}
}
update mem;
Ohh How can we miss it, we are adding duplicate item in list , we don't need all that this is wahat required.
Hi
Thanks for your reply, I am not getting any error but record is not updating in member2 object as i am updating fistname in member object
There is no reason that this trigger will not work are you sure you only have one record for Member2__c
try
this
this will Show you size as well, let me know with results.
Thanks Its working