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
Amit Singh1989Amit Singh1989 

create/update/delete one objects record based on another objects record (DML operation)

Hi friends,
i have one requirement which is given below,

I have an object Object1 with two fields
1)Size (Number)
2)Description (Long Text)

another object is object2 which has similar fields like object1 i.e. Size,Description

user can not create/update object2's record manually it will be created or updated based on object1's record
means
there should be a trigger on object1 which will be fired when any record of object1 is created/updated/deleted,

How to do this?

 

Thanks

Navatar_DbSupNavatar_DbSup

 

Hi,

Use the below code snippet as reference:

 

trigger testinsertupdatedelete on ObjectA__c (after delete, after insert, after update)

{

public ObjectA__c obja;

public ObjectB__c objb;

public List<ObjectB__c> listobjb;

if (Trigger.isInsert)

{

     system.debug('______isInsert_______');

     listobjb=new List<ObjectB__c>();

     for (ObjectA__c a : Trigger.new)

     {   

         objb=new ObjectB__c();

         objb.Name=a.Name;

         objb.size__c=a.size__c;

         objb.Description__c=a.Description__c;

         listobjb.add(objb);

        

     }

     insert listobjb;

}

if (Trigger.isUpdate)

{

     system.debug('_____isUpdate________');

     listobjb=new List<ObjectB__c>();

     obja=new ObjectA__c();

     obja=Trigger.new[0];

     system.debug('____isUpdate__obja_'+obja);

     ObjectB__c  obj=[select id from ObjectB__c where name=:Trigger.old[0].Name and size__c=:Trigger.old[0].size__c Limit 1];

     system.debug('____isUpdate__obj_'+obj);

         objb=new ObjectB__c(id=obj.id);

         objb.Name=obja.Name;

         objb.size__c=obja.size__c;

         objb.Description__c=obja.Description__c;

         update objb;

        

}

 

if (Trigger.isDelete)

{

    system.debug('______isDelete_______');

   ObjectB__c  obj=[select id from ObjectB__c where name=:Trigger.old[0].Name and size__c=:Trigger.old[0].size__c Limit 1];

   system.debug('____isDelete__obj_'+obj);

   objb=new ObjectB__c(id=obj.id);

   delete objb;

   

}

 

 

}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.