You need to sign in to do that
Don't have an account?
sonali verma
No relationship
Hi
I have 2 custom objects say Book1 and Book2 and I insert exactly same records in both the objects.
There is no relaitonship between Book1 and Book2.
Both objects have same fields Name and price respectively.
My requirement is when I update a record in Book1, then it should update the matching record in Book2.
I am stuck at the update level, pls help me out
here is the code
sonali verma
divya
I have 2 custom objects say Book1 and Book2 and I insert exactly same records in both the objects.
There is no relaitonship between Book1 and Book2.
Both objects have same fields Name and price respectively.
My requirement is when I update a record in Book1, then it should update the matching record in Book2.
I am stuck at the update level, pls help me out
here is the code
public class Book1Scenarios { Book2__c book2=new Book2__c(); //Insert 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; } // update operation. public void UpdateRecord(List<Book1__c> oldbook1) { List<Book2__c> oldbook2=[select Name,price__c from Book2__c]; for(Book1__c b1:oldbook1) { for(Book2__c b2:oldbook2) { if((b1.Name == b2.Name) && (b1.price__c == b2.price__c)) { b2.Name=b1.Name; b2.price__c=b1.price__c; } } } Database.update(oldbook2); } } 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); } }
sonali verma
divya
You are not updating with new values.
from trigger pass trigger.oldMap and trigger.new like
b1.UpdateRecord(Trigger.OldMap,trigger.new);
Change code inside class as follows:
public void UpdateRecord(Map<Id,Book1__c> oldmap,List<Book1__c> newbooklist1)
{
List<Book2__c> book2list=[select Name,price__c from Book2__c];
for(Book1__c b1:newbooklist1)
{
for(Book2__c b2:book2list)
{
if((oldmap.get(b1.Id).Name == b2.Name) && (oldmap.get(b1.Id).price__c == b2.price__c))
{
b2.Name=b1.Name;
b2.price__c=b1.price__c;
}
}
}
Database.update(book2list);
}
Please check the below code, hope this will work.
Helper class:
============
public class BookHelper {
public Static map<String,Book2__c> bookMap = new Map<String,Book2__c>();
public static void CreateBooks(List<Book1__c> books){
List<Book2__c> books2 = new List<Book2__c>();
for(Book1__c bk1 : books){
Book2__c b2 = new Book2__c();
b2.Name = bk1.name;
b2.Price__c = bk1.Price__c;
bookMap.put(bk1.Name,b2);
books2.add(b2);
}
insert books2;
}
public static void UpdateBooks2(List<Book1__c> bks1){
for(Book1__c b1 : bks1){
if(bookMap.get(b1.name).name==b1.name){
bookMap.get(b1.Name).Price__c=b1.Price__c;
}
}
update bookMap.values();
}
}
=======
Trigger:
=======
trigger booksTrigger on Book1__c (after insert, after update) {
if(Trigger.isAfter){
if(Trigger.isInsert)
BookHelper.CreateBooks(Trigger.new);
if(Trigger.isUpdate)
BooKHelper.UpdateBooks2(Trigger.old);
}
}
=======
Test class
=======
@isTest
public class TestBooksTrigger {
@isTest static void createbooks1(){
List<Book1__c> newBooks = new List<Book1__C>();
for(Integer i=0; i<200; i++){
Book1__c b1 = new Book1__c(Name = 'TestBook1_' +i, Price__c = i);
newBooks.add(b1);
}
insert newBooks;
List<Book1__C> updateBoks = new List<Book1__C>();
List<Book1__c> newBooksList = [Select name FROM Book1__C WHERE ID IN:newBooks];
for(Book1__c b1 : newBooksList){
b1.Price__c = 120;
updateBoks.add(b1);
}
update updateBoks;
}
}
The problem I am facing is after we edit record in book1, bookMap will not contain any values.
so the condition " if(bookMap.get(b1.name).name==b1.name) " fails, hence it doesn't update.
thanks
sonali
Assuming that there is unique name, by using apex we can like this as shown below:
Trigger:
-------------
trigger booksTrigger on Book1__c (after insert, after update) {
BookHelper bhelp = new BookHelper();
public List<Book2__C> books2 = new List<Book2__C>();
public Map<String,Book2__C> newbook2Map = new Map<String,Book2__C>();
if(Trigger.isAfter){
if(Trigger.isInsert)
bhelp.CreateBooks(Trigger.new);
if(Trigger.isUpdate)
bhelp.UpdateBooks2(Trigger.new);
}
}
Helper class
----------------
public class BookHelper {
public map<String,Book2__c> bookMap;
public map<String,Book2__c> newbookMap;
public List<Book2__c> book2List;
public BookHelper(){
bookMap = new Map<String,Book2__c>();
}
public void CreateBooks(List<Book1__c> books){
List<Book2__c> books2 = new List<Book2__c>();
for(Book1__c bk1 : books){
Book2__c b2 = new Book2__c();
b2.Name = bk1.name;
b2.Price__c = bk1.Price__c;
books2.add(b2);
}
insert books2;
}
// update operation.
public void UpdateBooks2(List<Book1__c> oldbook1)
{
newbookMap = new map<String,Book2__c>();
book2List = [Select id,name FROM Book2__c];
for(Book2__c b2 : book2List){
newbookMap.put(b2.name,b2);
}
for(Book1__c b1:oldbook1)
{
if(newbookMap.get(b1.name).name.equals(b1.name)){
newbookMap.get(b1.name).Price__c = b1.Price__c;
}
}
Database.update(newbookMap.values());
}
}
However we can do this by using process builder.