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
ankitha varraankitha 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
@Karanraj@Karanraj
Ankita - You can use cross object formula field in the child object, which pulls value from the parent object field. Check this link to know, more about cross object formula field https://help.salesforce.com/HTViewHelpDoc?id=customize_cross_object.htm
sachin kadian 5sachin kadian 5
trigger roomno on Discharge__c (before insert,bfeore update) {
list<discharge__c> dc= new list<discharge__c>();
List<Id> maintainanceIds = new List<Id>();
for(discharge__c dc : trigger.new){
    maintainanceIds.add(dc.relationShipFieldName__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.relationShipName__c).roomnumber__c);
}
}

Try it like this.. 
ankitha varraankitha varra
the code showing errors
sachin kadian 5sachin kadian 5
Can you paste what is the error?
ankitha varraankitha varra
Error: Invalid Data. 
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
sachin kadian 5sachin kadian 5
dc.room_no__c = String.valueOf(msMap.get(dc.relationShipName__c).roomnumber__c);

what are you putting for "relationShipName__c" here? it is the lookup field to master object. so please write that field name here.
ankitha varraankitha varra
trigger roomno1 on Discharge__c (before insert,before update) {
//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
sachin kadian 5sachin kadian 5
What are you doing here?

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??
ankitha varraankitha varra
please  give me some example for relationship name
sachin kadian 5sachin kadian 5
I am talking about the api name of filed that you are creating on child object to reference the master object? is 
room_no__c is api name of that field??
ankitha varraankitha varra
Yes that is the api name