You need to sign in to do that
Don't have an account?
Nidhi 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?
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:--
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
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;
}
}
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.
Using this code only last record of Response__c object will be updated.
Please let me know if hepled you.
Thanks
* 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:
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
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:--
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
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.
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
Thanks a lot,
Deepali