You need to sign in to do that
Don't have an account?
ankitha 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
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
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
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;
}
NOTE: This code has not been tested and may contain typographical or logical errors
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
NOTE: When adding code please use the "Add a code sample" button (icon <>) to increase readability and make it easier to reference.
the lines hilighted are api names of that field
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.
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,
Then add the Room_No__c field to your layout (you will need to have the Maintaince__c field on your layout as well).