function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
pooja biswaspooja biswas 

custom objects with No relationship

Hi friends
I have 2 custom objects, book1 and book2 with no relationship between them.
when I update a record in book1, the same record should be updated in book2.

Is this scenario possible if yes pls provide some inputs.

Thanks
pooja biswas
Veenesh VikramVeenesh Vikram
Hi Pooja,

Yes the scenario can be achieved using a trigger.
However there must be some identifier field that will halp identify the corressponding field. Like Name (which must be same for both the records).

Let me know if you need anything else.
Kindly mark this as answer if it helped you.

Veenesh
Ishwar ShindeIshwar Shinde
Hi Pooja,

It is possible using trigger but depends how you identify the simillar record in book 2.

Assuming, you have unique field, name or any identifier which inficates both are simillar. Then, on after update event of book1, you need to fetch book2 matching record using soql and update them accordingly.

Hope this will help.
pooja biswaspooja biswas
Hi
I have written a code but I require some help. pls let me know
 
public class Book1Scenarios
{
  
  Book2__c book2=new Book2__c();      
//insert code is working
  public void Create(List<Book1__c> book1)
  {
    for(Book1__c b1: book1)
    {
      book2.Name=b1.Name;
      book2.price__c=b1.price__c;
      
    }
    
    insert book2;
   } 
   

//when I update a record in book1, the same record should be updated in Book2 object
   public void UpdateRecord(List<Book1__c> oldbook1)
   {
     Map<ID,Book1__c> map1=new Map<ID,Book1__c>(oldbook1);
     
     Map<ID,Book2__c> map2=new Map<ID,Book2__c>([select Name,price__c from Book2__c]);
     
     List<Book2__c> list2=map2.values();
     
     for(Book1__c b1:oldbook1)
     {
       if (map2.containskey(b1.name))
       {
         Book2__c b2=new Book2__c();
         b2.Name=b1.Name;
         b2.price__c=b1.price__c;
         
         list2.add(b2);
       }
     }
     
     database.update(list2);
   }
}


trigger trg_Book1Scenarios on Book1__c (after Insert)
{
   Book1Scenarios b1=new Book1Scenarios();
   
   if (trigger.isInsert)
   {
    b1.create(Trigger.New);
   }
   
  if (trigger.isUpdate)
  {
    b1.UpdateRecord(Trigger.Old);
  }

   
}

thanks
pooja

 
Veenesh VikramVeenesh Vikram
Hi Pooja,

Update the method like below:
public void UpdateRecord(List<Book1__c> oldbook1)
   {
     //Map<ID,Book1__c> map1=new Map<ID,Book1__c>(oldbook1);
     
     Map<ID,Book2__c> map2=new Map<ID,Book2__c>();
	 
	 for(Book2__c bk2 : [select Name,price__c from Book2__c]){
		map2.put(bk2.name,bk2);
	 }
     
     //List<Book2__c> list2=map2.values();
     
     for(Book1__c b1:oldbook1)
     {
       if (map2.containskey(b1.name))
       {
         Book2__c b2=map2.get(b1.name);
         b2.price__c=b1.price__c;
         
         //list2.add(b2);
       }
     }
     
     database.update(map2.values());
   }

This will work i guess (There might be some syntax errors, but the logic is fine).Try this and let me know if it helps.

Veenesh
pooja biswaspooja biswas
Hi
I tried the code but its not working.

pooja
Veenesh VikramVeenesh Vikram
Is there any erro you are getting while executing the code?
pooja biswaspooja biswas
Hi
No errors I am getting.

pooja biswas
Veenesh VikramVeenesh Vikram
Try this one, i did a silly mistake i guess
public void UpdateRecord(List<Book1__c> oldbook1)
   {
     //Map<ID,Book1__c> map1=new Map<ID,Book1__c>(oldbook1);
     
     Map<ID,Book2__c> map2=new Map<ID,Book2__c>();
	 
	 for(Book2__c bk2 : [select Name,price__c from Book2__c]){
		map2.put(bk2.name,bk2);
	 }
     
     //List<Book2__c> list2=map2.values();
     
     for(Book1__c b1:oldbook1)
     {
       if (map2.containskey(b1.name))
       {
         map2.get(b1.name).price__c=b1.price__c;;
          //list2.add(b2);
       }
     }
     
     database.update(map2.values());
   }

This will do i guess :)

Veenesh
pooja biswaspooja biswas
Hi
the code is not updating, no errors

pooja
Veenesh VikramVeenesh Vikram
Im afraid, the code looks perfect to me. Cant say why its not working. Perhaps you need to check the data you are testing with Or from where the method is being called.