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
sharath Tsharath T 

how to copy object A records to object B based on a checkbox in object A?

Best Answer chosen by sharath T
suresh sanneboina 4suresh sanneboina 4
Try This
 
Trigger UpsertCustomObject on customobj1(after insert,after update,after Delete){
    
    List<customobj2> lstobj2=new List<customobj2>();
	Set<Id> lstSetId =new Set<Id>();
	if(Trigger.isinsert || Trigger.isUpdate){
		for(customobj1 obj1:Trigger.new)
		{
			if(obj1.checkboxField)
			{
				customobj2 obj2=new customobj2();
				obj2.Name=obj1.Name;
				obj2.externalField=obj1.Id;
				lstobj2.add(obj2);
			} else if(Trigger.isUpdate){
				lstSetId.add(obj1.Id);
			}
		}
		if(!lstSetId.isEmpty()){
			List<customobj2> lstcusobj=[Select Id,Name from customobj2 Where externalField IN : lstSetId];
			if(!lstcusobj.isEmpty()){
				for(customobj2 obj:lstcusobj){
					customobj2 obj2=new customobj2(Id=obj);
					obj2.Name=obj1.Name;
					obj2.externalField=obj1.Id;
					lstobj2.add(obj2);
				}
			}
		}
		if(!lstobj2.isEmpty())
		{
			upsert lstobj2 externalField;
		}
	} else if(Trigger.isDelete){
		List<customobj2> lstdelobj2=[Select Id from customobj2 Where externalField IN : Trigger.oldMap.keySet()];
		if(!lstdelobj2.isEmpty())
		{
			delete lstdelobj2;
		}
		
	}
}

 

All Answers

suresh sanneboina 4suresh sanneboina 4
Can you elaborate  little bit more on this.
suresh sanneboina 4suresh sanneboina 4
Trigger UpsertCustomObject on customobj1(after insert,after update){
    
    List<customobj2> lstobj2=new List<customobj2>();
    for(customobj1 obj1:Trigger.new)
    {
        if(obj1.checkboxField)
        {
            customobj2 obj2=new customobj2();
            obj2.Name=obj1.Name;
            obj2.externalField=obj1.Id;
            lstobj2.add(obj2);
        }
    }
    if(!lstobj2.isEmpty())
    {
        upsert lstobj2 externalField;
    }
}
sharath Tsharath T
Thanks suresh sanneboina 4 this is what i was looking for!! Another silly doubt in this, inorder to assign those fields from object A to object B, i need to have those fields in Object B(similar to object A) Am i right or wrong? 

Thanks 
Sharath T
suresh sanneboina 4suresh sanneboina 4
What are all the fields you need to assign from object a to object b you can assign thos fields. Its not manadatory you should have the same names.
sharath Tsharath T
 Thank you!!! In your code upsert lstobj2 externalField, what is the use of externalField there, can't we write upsert lstobj2??
suresh sanneboina 4suresh sanneboina 4
if you use upsert lstobj2 then it will check for the id as external field and updates based on the id. if you use 
upsert lstobj2 externalField then upserts based on the externalField field.
 
sharath Tsharath T
I have a dount in this Suresh. First I save a record in object A without checking the checkbox, that record will not be reflected in Object B. Secondly, I update the same record in object A with checkbox "Checked" at that time record will be displayed in object B(ie update operation) .thirdly if i try to update the same record by unchecking the checkbox in object A, the record which is already being copied to Object B must get deleted? how can i achieve the third condition? and finally when i delete a record in object A, those records in object B must also get deleted?

how to achieve these last two conditions? First two conditions are working perfectly fine as per your code.

Thanks,
Sharath T
suresh sanneboina 4suresh sanneboina 4
Try This
 
Trigger UpsertCustomObject on customobj1(after insert,after update,after Delete){
    
    List<customobj2> lstobj2=new List<customobj2>();
	Set<Id> lstSetId =new Set<Id>();
	if(Trigger.isinsert || Trigger.isUpdate){
		for(customobj1 obj1:Trigger.new)
		{
			if(obj1.checkboxField)
			{
				customobj2 obj2=new customobj2();
				obj2.Name=obj1.Name;
				obj2.externalField=obj1.Id;
				lstobj2.add(obj2);
			} else if(Trigger.isUpdate){
				lstSetId.add(obj1.Id);
			}
		}
		if(!lstSetId.isEmpty()){
			List<customobj2> lstcusobj=[Select Id,Name from customobj2 Where externalField IN : lstSetId];
			if(!lstcusobj.isEmpty()){
				for(customobj2 obj:lstcusobj){
					customobj2 obj2=new customobj2(Id=obj);
					obj2.Name=obj1.Name;
					obj2.externalField=obj1.Id;
					lstobj2.add(obj2);
				}
			}
		}
		if(!lstobj2.isEmpty())
		{
			upsert lstobj2 externalField;
		}
	} else if(Trigger.isDelete){
		List<customobj2> lstdelobj2=[Select Id from customobj2 Where externalField IN : Trigger.oldMap.keySet()];
		if(!lstdelobj2.isEmpty())
		{
			delete lstdelobj2;
		}
		
	}
}

 
This was selected as the best answer
sharath Tsharath T
Thank you so much Suresh!!! .It worked!!!
suresh sanneboina 4suresh sanneboina 4
If that solves your issue. Can you make this as best solution.