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
Nidhi MeshramNidhi Meshram 

How to write update trigger for the existing records?

I have 2 custom objects:- They have no relations in between Request and Response..Both objects are having common fields Name,Add, Phone no :- When I update Address field in Request object it should update in response object as well?
Best Answer chosen by Nidhi Meshram
Deepali KulshresthaDeepali Kulshrestha
Hi Nidhi,

I have gone through your question. To update the address field of one object to another object if both the object has no relation between them.

There no relation between request__c and response__c object. So there no way to query the response object without relation.
In my code given below, I have made the query of response considering that the same Name of response object should be updated.

Example:--
trigger updateAddress on Request__c (after update){
 List<String>  reqNmameList = new List<String>();
 if(trigger.isAfter && trigger.isUpdate){
    for(Request__c re : trigger.new){
        reqNmameList.add(re.Name);
    }
    List<Response__c>  respList = [Select id,Name,Address__c from Response__c where name in : reqNmameList];
    if(respList != null){
    List<Response__c> updtResList = new List<Response__c>();
    for(Request__c req : trigger.new){
        for(Response__c res : respList){
            if(req.Name == res.Name){
                res.Address__c = req.Address__c;
                updtResList.add(res);
            }
        }
    }
    update updtResList;
    }
 } 
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com

All Answers

Nidhi MeshramNidhi Meshram
Hi Foram, Actually it is not working I am sharing my code as i converted it into handler:-
Apex Trigger:-
trigger Copyingrecords on Request__c  (before insert,after update)
{
    if(Trigger.isInsert) 
  {
        CopyingData.insertdata(Trigger.new);
        
        
  }
    if(Trigger.isUpdate)
    {
         CopyingData.updatedata(Trigger.new, Trigger.old);
    }
    
}

Apex Class:-

public class CopyingData 
{
    public static void insertdata(List<Request__c> triggernew)
    {
    Response__c res = new Response__c();
    for(Request__c re : Triggernew)
    {
        res.Name=re.Name;
        res.Address__c=re.Address__c;
        res.Comments__c= re.Comments__c;
        res.Date__c= re.Date__c;
    }
    insert res;
    }
    
   public static void updatedata(List<Request__c> triggernew, List<Request__c> triggerold)
    {
       List<Response__c> lstResponse = [Select Id,Address__c from Response__c];
       List<Response__c> updateResList = new List<Response__c>();
        
        for (Request__c req : triggernew) {
        Request__c oldreq = (Request__c)Trigger.oldMap.get(req.Id);
        if (req.Address__c != oldreq.Address__c)
        {
            System.debug('@@@ IN If');
            for(Response__c res : lstResponse) {
                res.Address__c = req.Address__c ; // set request object address here
                updateResList.add(res);
            }
        }
    }
    update updateResList;
    }

}
Deepak GerianiDeepak Geriani

Hello 
Nidhi Meshram
As you said that object 1 and object 2 has no relation between them then all the records will be updated as solution provide by Foram Rana R
but if you want to update the spefice record you have set a realtion between object 1 and object 2 so that only spefic record is updated.

This code which you have written.

Response__c res = new Response__c();
    for(Request__c re : Triggernew)
    {
        res.Name=re.Name;
        res.Address__c=re.Address__c;
        res.Comments__c= re.Comments__c;
        res.Date__c= re.Date__c;
    }
    insert res;
    }

Using this code only last record of Response__c object will be updated.

Please let me know if hepled you.

Thanks

Ajay K DubediAjay K Dubedi
Hi Nidhi,

* First you need to create a lookup field(Request__c) on Custom Object(Response__c) to Custom Object(Request__c)

* After that try below code it works fine:
 
TRIGGER----->

trigger Copyingrecords on Request__c (After insert,after update) 
{
    if(Trigger.isAfter && Trigger.isInsert) 
    {
        CopyingData.insertdata(Trigger.new);
    }
    if(Trigger.isAfter && Trigger.isUpdate)
    {
        CopyingData.updatedata(Trigger.new);
    }
}

CLASS----->

public class CopyingData 
{
    public static void insertdata(List<Request__c> Request_List)
    {
        Response__c res = new Response__c();
        for(Request__c re : Request_List)
        {
            res.Name = re.Name;
            res.Address__c=re.Address__c;
            res.Comments__c= re.Comments__c;
            res.Request__c = re.id;
            res.Date__c= re.Date__c;
        }
        insert res;
    }
    
    public static void updatedata(List<Request__c> Request_List)
    {
        List<Response__c> lstResponse = [Select Id,Address__c,Comments__c,Date__c,Request__c from Response__c where Request__c In:Request_List];
        List<Response__c> updateResList = new List<Response__c>();
        for (Request__c req : Request_List)
        {
            for(Response__c re : lstResponse)
            {
                if(req.id==re.Request__c)
                {
                    re.Name = req.Name;
                    re.Address__c=req.Address__c;
                    re.Comments__c= req.Comments__c;
                    re.Date__c= req.Date__c;
                    updateResList.add(re);
                }
            }
        }
        update updateResList;
    }
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
Deepali KulshresthaDeepali Kulshrestha
Hi Nidhi,

I have gone through your question. To update the address field of one object to another object if both the object has no relation between them.

There no relation between request__c and response__c object. So there no way to query the response object without relation.
In my code given below, I have made the query of response considering that the same Name of response object should be updated.

Example:--
trigger updateAddress on Request__c (after update){
 List<String>  reqNmameList = new List<String>();
 if(trigger.isAfter && trigger.isUpdate){
    for(Request__c re : trigger.new){
        reqNmameList.add(re.Name);
    }
    List<Response__c>  respList = [Select id,Name,Address__c from Response__c where name in : reqNmameList];
    if(respList != null){
    List<Response__c> updtResList = new List<Response__c>();
    for(Request__c req : trigger.new){
        for(Response__c res : respList){
            if(req.Name == res.Name){
                res.Address__c = req.Address__c;
                updtResList.add(res);
            }
        }
    }
    update updtResList;
    }
 } 
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
This was selected as the best answer
Nidhi MeshramNidhi Meshram
Hi Deepali,
Thanks for the response.

Actually Scenario is this:- There are 2 objects which consist of  same fields. After inserting the record in request__c object it should automatic create the response record which the same field. So I written the above code which is for insert and it is working fine. I need to update same record which is address__field. (single record Updation not mass). Because they have no relationship in between them. By the use of name field is difficult to update. NAme can be duplicates and address__c can be duplications.ed:- 1. ABC, Pune is one record   2. ABC, Pune is another record So for updation is difficult to get which record to be updated. 
Deepali KulshresthaDeepali Kulshrestha
Hi Nidhi,

I have gone through your reply. Yes, it is difficult to update the record if they have the same name. But in request__c and response__c either there should some relation between them or there should be any field that does not have duplicates value in the object. Without these two things, we cannot update or query the response__c record.

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
 
Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com 
Nidhi MeshramNidhi Meshram
Yes, Sot I created one more firld which is phone number which will never be duplicates, It will compare from phone number. 

Thanks a lot,
Deepali