+ Start a Discussion
sessusessu 

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;

}

}

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

There is no reason that this trigger will not work are you sure you only have one record for Member2__c 

 

try

this

 

trigger updt on Member__c (after update) 
{
  Member2__c mem = [Select id from Member2__c Limit 1];

List<Member2__c> memList = [Select id from Member2__c];
if(memList.size() >  1)
{
Trigger.new.get(0).addError('Size is greater than one : ' + memList.size());

}
for (Member__c a1 : Trigger.new) 
  {  
    
      mem.FirstName__c=a1.FirstName__c;  
   
  } 
   update mem;
}

 this will Show you size as well, let me know with results.

All Answers

Shashikant SharmaShashikant Sharma

Your code is in right direction but not complete

 

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();
        // You need to copy all the field
        mem2.Field1_APIName = a.Field1_APIName;
        mem2.Field2_APIName = a.Field2_APIName;
        mem.add(mem2);
   }
   upsert mem;
}

 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.

sessusessu

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

Shashikant SharmaShashikant Sharma

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.

sessusessu

Hi

 

I have not created any master detail or look up fields in both the objects

Shashikant SharmaShashikant Sharma

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.

sessusessu

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.

Shashikant SharmaShashikant Sharma

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.

sessusessu

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; 

Shashikant SharmaShashikant Sharma

Ok then try this it will do your work

 

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;
}

 let me know with your results

 

sessusessu

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

Shashikant SharmaShashikant Sharma

Try this 

 

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;
} 

 

let me know if nay issues in it.

sessusessu

 

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;     

Shashikant SharmaShashikant Sharma

Ohh How can we miss it, we are adding duplicate item in list , we don't need all that this is wahat required.

 

trigger updt on Member__c (after update) 
{
  Member2__c mem = [Select id from Member2__c Limit 1];

for (Member__c a1 : Trigger.new) 
  {  
    
      mem.FirstName__c=a1.FirstName__c;  
   
  } 
   update mem;
}

 

sessusessu

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

 

Shashikant SharmaShashikant Sharma

There is no reason that this trigger will not work are you sure you only have one record for Member2__c 

 

try

this

 

trigger updt on Member__c (after update) 
{
  Member2__c mem = [Select id from Member2__c Limit 1];

List<Member2__c> memList = [Select id from Member2__c];
if(memList.size() >  1)
{
Trigger.new.get(0).addError('Size is greater than one : ' + memList.size());

}
for (Member__c a1 : Trigger.new) 
  {  
    
      mem.FirstName__c=a1.FirstName__c;  
   
  } 
   update mem;
}

 this will Show you size as well, let me know with results.

This was selected as the best answer
sessusessu

Thanks Its working

sessusessu
Hi Please help me, if I want to update multiple records from one object to another process to write the code