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 select the particular field value from lookup box

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 (before insert,before update) {
list<discharge__c> dc= new list<discharge__c>();

list<maintaince__c> ms=[select id,name,roomnumber__c,(select id ,name ,room_no__c from discharges__r where id=:trigger.new[0].id) 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: m.discharges__r){
d.room_no__c= m.id;
dc.add(d);
}
}
update dc;
}

 but it does n't works, pls help me with some sample code
 
Best Answer chosen by ankitha varra
pconpcon
Well there's no way to customize what the Name is (you should have set the room number to be the "Name" of the Maintaince__c object) but we can get it to display the room number on the Discharge__c object.  I would first start by renaming or relabeling the room_no__c feild on your Discharge__c object.  I would name this Maintaince__c.  Then you will create a new formula field that is called Room_No__c and has the following for it's formula
 
Maintaince__r.Roomnumber__c

Then add the Room_No__c field to your layout (you will need to have the Maintaince__c field on your layout as well).

All Answers

pconpcon
This will not work because this is in a before trigger.  Before trigger happen prior to the object having an id assigned.  I would first try to do it on after insert/update.  Additionally, you would also want to think about reworking the logic in your trigger since this will not work if you insert / update two or more Discharge objects.
ankitha varraankitha varra
I have edited my code in the place of room_no__c og dicharge object am getting name value only
and the code is

trigger roomno on Discharge__c (before insert,before 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;
}
pconpcon
I've taken the liberty of re-writing your trigger to clear up some of your logic and to make it support bulk operations.  You will need to change lines 5 and 21 if the field that points from Discharge__c -> Maintaince__c is not called Maintaince__c.
 
trigger roomno on Discharge__c (before insert, befor update) {
    Set<Id> maintIds = new Set<Id>();

    for (Discharge__c discharge : Trigger.new) {
        maintIds.add(discharge.Maintaince__c); 
    }
    
    if (!maintIds.isEmpty()) {
        Map<Id, Maintaince__c> maintMap = new Map<Id, Maintaince__c>([
            select Name,
                Roomnumber__c
            from Maintaince__c
            where Id in :Trigger.newMap.keySet()
        ]);

        for (Discharge__c discharge : Trigger.new) {
            if (discharge.Maintaince__c == null) {
                continue;
            }

            discharge.Room_No__c = mainMap.get(discharge.Maintaince__c).Roomnumber__c;
        }
    }
}

NOTE: This code has not been tested and may contain typographical or logical errors
ankitha varraankitha varra
i have changed your code like this it is showing this error
trigger roomno on Discharge__c (before insert, before update) {
    Set<Id> maintIds = new Set<Id>();

    for (Discharge__c discharge : Trigger.new) {
        maintIds.add(discharge.room_no__c); 
    }
    
    if (!maintIds.isEmpty()) {
        Map<Id, Maintaince__c> mainMap = new Map<Id, Maintaince__c>([
            select Name,
                Roomnumber__c
            from Maintaince__c
            where Id in :Trigger.newMap.keySet()
        ]);

        for (Discharge__c discharge : Trigger.new) {
            if (discharge.room_no__c== null) {
                continue;
            }

            discharge.Room_No__c = mainMap.get(discharge.room_no__c).Roomnumber__c;
        }
    }
}
 Error:Illegal assignment from Decimal to Id at line 21 column 13
if i do typecasting exception occurs and the exception is

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

 
ankitha varraankitha varra
where 101 is the value of that field

 
pconpcon
Can you please include a screenshot of the field definitions of both your Discharge__c and your Maintaince__c objects so I can see the types of the fields we are working with and get a better understanding of the relationship.

NOTE: When adding code please use the "Add a code sample" button (icon <>) to increase readability and make it easier to reference.
ankitha varraankitha varra
User-added imageUser-added image

the lines hilighted are api names of that field 
 
pconpcon
You cannot do what you are trying to do because the Room_No__c field on your discharge object is a lookup to a Maintaince object.  And the Roomnumber__c field on your Maitaince object is a Number.  These fields are incompatable.

Can we please take a step back and try to explain what you are doing and then maybe I can help you actually accomplish your goal.  If you could take a screenshot of a test record of each type with the starting data and then show me a screenshot of your Discharge object with the data at the end, that would help.  To get the data at the end just change the data manually and tell me the manual steps you did to make the change.
ankitha varraankitha varra
User-added imageUser-added image

when we select the value of the look up dialog box  Maintaince name will come,when we have relationship with the maintaince__c ,
but i want the roomnumber__c  in discharge__c lookup field room_no__c,



 
pconpcon
Well there's no way to customize what the Name is (you should have set the room number to be the "Name" of the Maintaince__c object) but we can get it to display the room number on the Discharge__c object.  I would first start by renaming or relabeling the room_no__c feild on your Discharge__c object.  I would name this Maintaince__c.  Then you will create a new formula field that is called Room_No__c and has the following for it's formula
 
Maintaince__r.Roomnumber__c

Then add the Room_No__c field to your layout (you will need to have the Maintaince__c field on your layout as well).
This was selected as the best answer
ankitha varraankitha varra
i want to update that value with the trigger,how can we update with the trigger