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
Jagadeesh AdaveniJagadeesh Adaveni 

whenever i update record in Obj__A same record should update in objB__C

Hi All,

I am having 2 objects called ObjA__c and ObjB__c and fileds are same in both objects viz. name phone__c,email__c. So my requirement is whenever i inserted records on ObjA__C automatically same record should insert into ObjB__C for that code is

trigger AutoUpdate on MainDemo__c (Before insert, Before Update) {

    List<Demo__C> dl = new List<Demo__C>();
    List<Demo__C> dlu1 = new List<Demo__C>();
    if(Trigger.isInsert){
    for(MainDemo__C md:Trigger.new){
    
        Demo__c d = new Demo__c();
        d.name=md.name; 
        d.email__c=md.email__c;
        d.phone__c=md.phone__c;
        dl.add(d);
    
    }
    insert dl;
    } 

Now whenever i update record in Obj__A same record should update in objB__C but problem here is updated record is reinserting in objB__C how can i achieve this and also same for "Delete"

Thanks,
A.Jagadeesh
James LoghryJames Loghry
Not sure why you're duplicating records like this, but putting that aside, you'll need to establish some sort of relationship between MainDemo and Demo.  That relationship could be either a Master Detail, Lookup relationship, or it could be something simple like an external id field.  Basically you need some way of saying "This Main Demo is related to this demo record".

From there you could update or delete the demo record when the main demo record is updated or deleted.

If you go the external id route, then you could use an upsert instead of an insert to handle both inserts and updates in one swoop.  

If you end up tying the demo record to the maindemo__c record using a master detail relationship, you would need to handle inserts and updates separately, but you shouldn't have to worry about deletes, because the child would be cascade deleted.

If you end up tying the demo record to the maindemo__c record with a lookup relationship, then you'll need to handle inserts, updates, and deletes separately.
kanchi chandra mohankanchi chandra mohan
simple u can write the if(trigger.isinsert&&trigger.isupdate){}