You need to sign in to do that
Don't have an account?
ankitha varra
How to update child lookup field value with parent field values
I have two objects with lookup relationship those maintaince__c and discharge__c, maintaince object contains fields name,room__c wardno__c
i want to get the field value room__c from maintaince__c object to the lookup relationship field roomno__c in discharge object
where maintaince__c is the parent object and discharge__c is the child object,and name is the standard field,
for that i have written a trigger
trigger roomno on Discharge__c (after insert,after update) {
list<discharge__c> dc= new list<discharge__c>();
list<maintaince__c> ms=[select id,name,roomnumber__c from Maintaince__c];
list<discharge__c> ds=[select id ,name,room_no__c from discharge__c where id=:trigger.new[0].id ];
for(maintaince__c m: ms){
for(discharge__c d: ds){
if(d.id== m.id)
{
d.room_no__c=string.valueof(m.roomnumber__c);
dc.add(d);
}
}
}
update dc;
}
i want to update in the place of room_no__c of discharge__c with the value maintaince__c of roomnumber__c
i want to get the field value room__c from maintaince__c object to the lookup relationship field roomno__c in discharge object
where maintaince__c is the parent object and discharge__c is the child object,and name is the standard field,
for that i have written a trigger
trigger roomno on Discharge__c (after insert,after update) {
list<discharge__c> dc= new list<discharge__c>();
list<maintaince__c> ms=[select id,name,roomnumber__c from Maintaince__c];
list<discharge__c> ds=[select id ,name,room_no__c from discharge__c where id=:trigger.new[0].id ];
for(maintaince__c m: ms){
for(discharge__c d: ds){
if(d.id== m.id)
{
d.room_no__c=string.valueof(m.roomnumber__c);
dc.add(d);
}
}
}
update dc;
}
i want to update in the place of room_no__c of discharge__c with the value maintaince__c of roomnumber__c
Try it like this..
Review all error messages below to correct your data.
Apex trigger roomno1 caused an unexpected exception, contact your administrator: roomno1: execution of BeforeUpdate caused by: System.StringException: Invalid id: 101: Trigger.roomno1: line 11, column 1
what are you putting for "relationShipName__c" here? it is the lookup field to master object. so please write that field name here.
//list<discharge__c> dc= new list<discharge__c>();
List<Id> maintainanceIds = new List<Id>();
for(discharge__c dc : trigger.new){
maintainanceIds.add(dc.room_no__c);
}
Map<Id,maintaince__c> msMap=new Map<Id,maintaince__c>([select id,name,roomnumber__c from Maintaince__c where ID IN : maintainanceIds]);
for(discharge__c dc : trigger.new){
dc.room_no__c = String.valueOf(msMap.get(dc.room_no__c).roomnumber__c);
}
}
i wrote this
dc.room_no__c = String.valueOf(msMap.get(dc.room_no__c).roomnumber__c);
I am still confused whats your exact requirement? what is this field "room_no__c"? is it a text/number field or is it the lookup field to maintainance? Do you want to assign the maintainance to discharege when it is cretated/updated or do you want to any fields value from maintainance to dischare??
room_no__c is api name of that field??