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
vamsi garapativamsi garapati 

I have two objects say Bottle1 and Bottle2, if a record is inserted in bottle1 a record will be inserted in bottle2. If a record in bottle1 is updated the reocrd in bottle2 with same data as of bottle1 needs to be updated as well.Is that possible?

Raza0195Raza0195
There is any relationbetween thrm ?
Andrew GAndrew G
Short answer is yes.

The longer answer is how to achieve it.

If there is a relationship (lookup field), and if the fields on bottle 2 are not to be updated by humans or code, then you could do formula fields (e.g. bottle1__r.fieldname).
If there is a relationship(lookup field), and the fields in bottle 2 can be updated by other things and we are overwriting, then process builder would be the easiest non code option. You could also use Flow or Apex.
If there is no relationship, and all we have is some name reference to the record in a field that is not a lookup, then we are looking at flows or apex triggers.  You would need to find the bottle2 record by uses of findrecord in flow or a SOQL in apex. 

There are some other things to be considered such as whether there is a parent-child between bottle 1 and bottle 2

That's the basic high level response.

Regards
Andrew
 
vamsi garapativamsi garapati
Thank you for your response. help me on the code to update bottle2 when bottle1 is updated.
sai007sai007
you can Implement using trigger if you have relationship

//Trigger
trigger Bottle1trigg on Bottle2__c (before update) {
    if(trigger.IsUpdate){
        Updateclass.updatefields(trigger.newMap);
    }
}

//class
public class Updateclass {
    public static void updatefields(Map<Id,Bottle2__c> newMap){
        List<Bottle1__c> Bottle1List = [select id,name from Bottle1__c where Bottle2__c =: newMap.values().Id];
        for(Bottle1__c b1 : Bottle1List){
            for(Bottle2__c b2 :newMap.values()){
                b1.Name = b2.Name;
            }
        }
        update Bottle1List;
    }
}
vamsi garapativamsi garapati
Thank you for your response. But two sobjects didn't have any relation between them.
Andrew GAndrew G
if there is no relationship, how does bottle 1 know which bottle 2 to update?

And the other question would be, why do you need to create a new bottle2 record just because bottle1 is inserted if there is no relationship?

To achieve what you are chasing you will need to establish some sort of relationship or you need to create a field to use as a key between the two records.

for a solution where you don't want to create a lookup relationship, the following would be the high level solution.

Since we create bottle2 on insert of bottle1, create a text field on bottle2 called "bottle1id" and when creating populate that field with the ID.  Then on update you would do a SELECT where that field contains the ID on the bottle1 record being updated.


Regards
Andrew
vamsi garapativamsi garapati
Thank you for your reply..