You need to sign in to do that
Don't have an account?
How to choose a field value in the master as per a criteria from the related list records
Dear All,
In my Quote object, i have a related list called " Named Driver" , as per below,
I want to create a field in Quote, which will show me the youngest driver i.e in this case 21. If less one say 18 added later, it will change to 18. Can you plz suggest how to create such field, It would be a look up or text ( if text should be a hyperlink) to go inside the record.
Or if apex trigger needed, any sample code writing suggestions.
Thnx

In my Quote object, i have a related list called " Named Driver" , as per below,
I want to create a field in Quote, which will show me the youngest driver i.e in this case 21. If less one say 18 added later, it will change to 18. Can you plz suggest how to create such field, It would be a look up or text ( if text should be a hyperlink) to go inside the record.
Or if apex trigger needed, any sample code writing suggestions.
Thnx
Please Make the Youngest Driver Name field of Rich Text Area.
And then copy the below code in Apex class:
public class OpportunityHandlers
{
list<Quote> oppList = new list<Quote>();
set<id> oppIDs = new set<id>();
Map<string, Integer> mapMinAge = new Map<string, Integer>();
public void oppInsert(List<Quotation_Driver__c> oppLists)
{
for(Quotation_Driver__c opp : oppLists)
{
if(opp.Quote__c !=null)
{
oppIDs.add(opp.Quote__c);
}
}
aggregateResults();
}
public void oppUpdate(List<Quotation_Driver__c> oppLists,Map<Id,Quotation_Driver__c> oppOldmap)
{
for(Quotation_Driver__c opp : oppLists)
{
if(opp.Quote__c!=null)
{
oppIDs.add(opp.Quote__c);
if(oppOldmap.get(opp.id).Quote__c!=opp.Quote__c)
{
oppIDs.add(oppOldmap.get(opp.id).Quote__c);
}
}
}
aggregateResults();
}
public void oppDelete(List<Quotation_Driver__c> oppLists)
{
for(Quotation_Driver__c opp : oppLists)
{
if(opp.Quote__c!=null)
{
oppIDs.add(opp.Quote__c);
}
}
aggregateResults();
}
public void oppundelete(List<Quotation_Driver__c> oppLists)
{
for(Quotation_Driver__c opp : oppLists)
{
if(opp.Quote__c!=null)
{
oppIDs.add(opp.Quote__c);
}
}
aggregateResults();
}
public void aggregateResults()
{
AggregateResult[] groupedResults = [SELECT MIN(AgeUpdated__c), Quote__c FROM Quotation_Driver__c where Quote__c IN :oppIDs GROUP BY Quote__c];
for(AggregateResult ar:groupedResults)
{
mapMinAge.put((String)ar.get('Quote__c'),(Integer)ar.get('expr0'));
/*Id oppId = (ID)ar.get('Quote__c');
Double min = (DOUBLE)ar.get('expr0');
Quote opp1 = new Quote(Id=oppId);
opp1.Youngest_DriverT__c = min;
oppList.add(opp1);*/
}
for (Quotation_Driver__c objNamedDriver : [SELECT Id,Name, AgeUpdated__c, Quote__c
FROM Quotation_Driver__c
WHERE Quote__c IN :oppIDs ]) {
Quote objQuote;
if(mapMinAge.containsKey(objNamedDriver.Quote__c)){
if (mapMinAge.get(objNamedDriver.Quote__c) == objNamedDriver.AgeUpdated__c){
oppList.add(new Quote(Id = objNamedDriver.Quote__c, Youngest_DriverT__c = '<a href=/' + objNamedDriver.Id+ '>'+objNamedDriver.Name+ '</a>'));
}
}
}
if(oppList.size()>0 && oppList!=null)
{
update oppList;
}
}
}
All Answers
Hi Sourav,
If there is master-detail relationship between quote and named driver then it can be done by making rollup summary field on Quote which will select the minimum value.
If relationship is lookup then we need to go for trigger.
Hi Sourav,
Please provide me the API name of Named Driver object and its fields.
Thanks
The API name of Named Driver is : Quotation_Driver__c
and the Age field on this object is : AgeUpdated__c
Thanks
In Quote the field where the youngest driver should reflect is : Youngest_DriverT__c ( its a text field),
In Named Driver, the Quote look up field is : Quote__c
In Quote, once it reflect the youngest driver name, it should be a hyperlink to go the Named driver record.
Thank you.
If i missed somthing, plz let me know
Please create a Integer field on Quote with API name "Min_of_Age_Updated__c".
And then try below trigger and class.
FYI- I have assumed API of lookup field as Quote__c.
******************************************************************Trigger*******************************************************
trigger DealTrigger on Deal__c (after delete,after update, after insert, after undelete)
{
if(trigger.isInsert)
{
OpportunityHandlers oppHandler =new OpportunityHandlers();
oppHandler.oppInsert(Trigger.New);
}
else if(trigger.isDelete)
{
OpportunityHandlers oppHandler =new OpportunityHandlers();
oppHandler.oppDelete(Trigger.Old);
}
else if(trigger.isUnDelete)
{
OpportunityHandlers oppHandler =new OpportunityHandlers();
oppHandler.oppundelete(Trigger.New);
}
else if(trigger.isUpdate)
{
OpportunityHandlers oppHandler =new OpportunityHandlers();
oppHandler.oppUpdate(Trigger.New,Trigger.Oldmap);
}
}
*******************************************************************Class***************************************************************************************************************************
public class OpportunityHandlers
{
list<Quote> oppList = new list<Quote>();
set<id> oppIDs = new set<id>();
public void oppInsert(List<Quotation_Driver__c> oppLists)
{
for(Quotation_Driver__c opp : oppLists)
{
if(opp.Quotation_Driver__c !=null)
{
oppIDs.add(opp.Quotation_Driver__c);
}
}
aggregateResults();
}
public void oppUpdate(List<Quotation_Driver__c> oppLists,Map<Id,Quotation_Driver__c> oppOldmap)
{
for(Quotation_Driver__c opp : oppLists)
{
if(opp.Quote__c!=null)
{
oppIDs.add(opp.Quote__c);
if(oppOldmap.get(opp.id).Quote__c!=opp.Quote__c)
{
oppIDs.add(oppOldmap.get(opp.id).Quote__c);
}
}
}
aggregateResults();
}
public void oppDelete(List<Quotation_Driver__c> oppLists)
{
for(Quotation_Driver__c opp : oppLists)
{
if(opp.Quote__c!=null)
{
oppIDs.add(opp.Quote__c);
}
}
aggregateResults();
}
public void oppundelete(List<Quotation_Driver__c> oppLists)
{
for(Quotation_Driver__c opp : oppLists)
{
if(opp.Quote__c!=null)
{
oppIDs.add(opp.Quote__c);
}
}
aggregateResults();
}
public void aggregateResults()
{
AggregateResult[] groupedResults = [SELECT MIN(AgeUpdated__c), Quote__c FROM Quotation_Driver__c where Quote__c IN :oppIDs GROUP BY Quote__c];
for(AggregateResult ar:groupedResults)
{
Id oppId = (ID)ar.get('Quote__c');
Integer min = (INTEGER)ar.get('expr0');
Quote opp1 = new Quote(Id=oppId);
opp1.Min_of_Age_Updated__c= min;
oppList.add(opp1);
}
if(oppList.size()>0 && oppList!=null)
{
update oppList;
}
}
}
This will update the text field with the ID of the youngest driver.
Let me know if this helps.
Error: Compile Error: Invalid type: Quote__c at line 16 column 53
That is, the Quote look up in teh Named Driver object( below one)
Hi Sourav,
As you are using standard Quote object so please use Quote in place of Quote__c
Yes. If it is standard quote object, please change the object reference from Quote__c to Quote.
A word of caution, please do not change the lookup relationship names in your Named Driver object.
and, you can remove line 28.
Hi Sourav,
Please let me know if issue is resolved and if not please let me know what error is coming
Thanks
Error: Compile Error: No such column 'Quote' on entity 'Quotation_Driver__c'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names. at line 20 column 35
{
if(trigger.isInsert)
{
OpportunityHandlers oppHandler =new OpportunityHandlers();
oppHandler.oppInsert(Trigger.New);
}
else if(trigger.isDelete)
{
OpportunityHandlers oppHandler =new OpportunityHandlers();
oppHandler.oppDelete(Trigger.Old);
}
else if(trigger.isUnDelete)
{
OpportunityHandlers oppHandler =new OpportunityHandlers();
oppHandler.oppundelete(Trigger.New);
}
else if(trigger.isUpdate)
{
OpportunityHandlers oppHandler =new OpportunityHandlers();
oppHandler.oppUpdate(Trigger.New,Trigger.Oldmap);
}
}
************************************************************************************Class**********************************************************************************************************
public class OpportunityHandlers
{
list<Quote> oppList = new list<Quote>();
set<id> oppIDs = new set<id>();
public void oppInsert(List<Quotation_Driver__c> oppLists)
{
for(Quotation_Driver__c opp : oppLists)
{
if(opp.Quote__c !=null)
{
oppIDs.add(opp.Quote__c);
}
}
aggregateResults();
}
public void oppUpdate(List<Quotation_Driver__c> oppLists,Map<Id,Quotation_Driver__c> oppOldmap)
{
for(Quotation_Driver__c opp : oppLists)
{
if(opp.Quote__c!=null)
{
oppIDs.add(opp.Quote__c);
if(oppOldmap.get(opp.id).Quote__c!=opp.Quote__c)
{
oppIDs.add(oppOldmap.get(opp.id).Quote__c);
}
}
}
aggregateResults();
}
public void oppDelete(List<Quotation_Driver__c> oppLists)
{
for(Quotation_Driver__c opp : oppLists)
{
if(opp.Quote__c!=null)
{
oppIDs.add(opp.Quote__c);
}
}
aggregateResults();
}
public void oppundelete(List<Quotation_Driver__c> oppLists)
{
for(Quotation_Driver__c opp : oppLists)
{
if(opp.Quote__c!=null)
{
oppIDs.add(opp.Quote__c);
}
}
aggregateResults();
}
public void aggregateResults()
{
AggregateResult[] groupedResults = [SELECT MIN(AgeUpdated__c), Quote__c FROM Quotation_Driver__c where Quote__c IN :oppIDs GROUP BY Quote__c];
for(AggregateResult ar:groupedResults)
{
Id oppId = (ID)ar.get('Quote__c');
Double min = (DOUBLE)ar.get('expr0');
Quote opp1 = new Quote(Id=oppId);
opp1.Youngest_DriverT__c = min;
oppList.add(opp1);
}
if(oppList.size()>0 && oppList!=null)
{
update oppList;
}
}
}
while trying to save the apex class, i am getting the below error, Do you know why?
same code I am using in my org it is working.
Can you please again copy and paste the code?
Compile Error: line 58:39 no viable alternative at character ' ' at line 58 column 39
This is again the same type of error which you received earlier.
It can be resolved again in same way.
Error: Compile Error: Illegal assignment from Double to String at line 62 column 1
Actually, you have taken Youngest driver as text field, please make it number type because age will be in number.
I got you point you want the name of Youngest Driver name in the text field.
No issue first you try this then I will add the logic for that in my class
ok let me add that functionality in code.
As soon as I will do will let you know
Please Make the Youngest Driver Name field of Rich Text Area.
And then copy the below code in Apex class:
public class OpportunityHandlers
{
list<Quote> oppList = new list<Quote>();
set<id> oppIDs = new set<id>();
Map<string, Integer> mapMinAge = new Map<string, Integer>();
public void oppInsert(List<Quotation_Driver__c> oppLists)
{
for(Quotation_Driver__c opp : oppLists)
{
if(opp.Quote__c !=null)
{
oppIDs.add(opp.Quote__c);
}
}
aggregateResults();
}
public void oppUpdate(List<Quotation_Driver__c> oppLists,Map<Id,Quotation_Driver__c> oppOldmap)
{
for(Quotation_Driver__c opp : oppLists)
{
if(opp.Quote__c!=null)
{
oppIDs.add(opp.Quote__c);
if(oppOldmap.get(opp.id).Quote__c!=opp.Quote__c)
{
oppIDs.add(oppOldmap.get(opp.id).Quote__c);
}
}
}
aggregateResults();
}
public void oppDelete(List<Quotation_Driver__c> oppLists)
{
for(Quotation_Driver__c opp : oppLists)
{
if(opp.Quote__c!=null)
{
oppIDs.add(opp.Quote__c);
}
}
aggregateResults();
}
public void oppundelete(List<Quotation_Driver__c> oppLists)
{
for(Quotation_Driver__c opp : oppLists)
{
if(opp.Quote__c!=null)
{
oppIDs.add(opp.Quote__c);
}
}
aggregateResults();
}
public void aggregateResults()
{
AggregateResult[] groupedResults = [SELECT MIN(AgeUpdated__c), Quote__c FROM Quotation_Driver__c where Quote__c IN :oppIDs GROUP BY Quote__c];
for(AggregateResult ar:groupedResults)
{
mapMinAge.put((String)ar.get('Quote__c'),(Integer)ar.get('expr0'));
/*Id oppId = (ID)ar.get('Quote__c');
Double min = (DOUBLE)ar.get('expr0');
Quote opp1 = new Quote(Id=oppId);
opp1.Youngest_DriverT__c = min;
oppList.add(opp1);*/
}
for (Quotation_Driver__c objNamedDriver : [SELECT Id,Name, AgeUpdated__c, Quote__c
FROM Quotation_Driver__c
WHERE Quote__c IN :oppIDs ]) {
Quote objQuote;
if(mapMinAge.containsKey(objNamedDriver.Quote__c)){
if (mapMinAge.get(objNamedDriver.Quote__c) == objNamedDriver.AgeUpdated__c){
oppList.add(new Quote(Id = objNamedDriver.Quote__c, Youngest_DriverT__c = '<a href=/' + objNamedDriver.Id+ '>'+objNamedDriver.Name+ '</a>'));
}
}
}
if(oppList.size()>0 && oppList!=null)
{
update oppList;
}
}
}
So you mean i make the "Youngest_DriverT__c" as Text Area (Rich) ? Is that not working if i make that a look up field kind ? as it should be clickable to go to that record.
Hi Sourav,
Yes you have to make "Youngest_DriverT__c" as Text Area (Rich). It will store record as a link. When you will click on that link then you can go to that record.
Hope this works for you!
If not then please let me know.
I was just trying to edit it. One error constantly showing, its like before , i am not able to chck how to resolve it, In Before code it went automatically
In line 57 why you have put semi colon ;.
Please again copy and paste the code which I have sent to you
Hi Sourav,
Please mark as a best answer if this helps you!
Hi Sourav,
Did my solution doesn't work for you?
Please let me know where did you stuck.
Thanks
Deepak
dpkm20@gmail.com
Also, i saved the field as text rich, but it will not proper, as on edit page its showing me a very big space in the Young driver field, Do making it as a text field work ? Thanks
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger DealTrigger caused an unexpected exception, contact your administrator: DealTrigger: execution of AfterInsert caused by: System.TypeException: Invalid conversion from runtime type Decimal to Integer: Class.OpportunityHandlers.aggregateResults: line 59, column 1
If you donot want rich text area type field then Young_DriverT__c as text field which will store id of record.
Then we will create a formula field to show as a link.
Please confirm for this then I need to some changes in the code.
Yes, Can we plz change it to text field. As making it text rich, is not showing proper in the edit page. Thanks
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger DealTrigger caused an unexpected exception, contact your administrator: DealTrigger: execution of AfterInsert caused by: System.DmlException: Update failed. First exception on row 0 with id 0Q0N00000007tcPKAQ; first error: STRING_TOO_LONG, Youngest DriverT: data value too large: <a href=/a0eN0000004fg9YIAQ>QD-02023</a> (max length=25): [Youngest_DriverT__c]: Class.OpportunityHandlers.aggregateResults: line 75, column 1
<a href=/a0eN0000004fg9nIAA>QD-02026</a>
oppList.add(new Quote(Id = objNamedDriver.Quote__c, Youngest_DriverT__c = objNamedDriver.Name));
Hi Sourav,
Please create 1 more text field: "Name of Driver" on Quote which will show name of field.
Please change below line of code :
oppList.add(new Quote(Id = objNamedDriver.Quote__c, Youngest_DriverT__c = '<a href=/' + objNamedDriver.Id+ '>'+objNamedDriver.Name+ '</a>'));
to
oppList.add(new Quote(Id = objNamedDriver.Quote__c, Youngest_DriverT__c = URL.getSalesforceBaseUrl().toExternalForm() + '/' + objNamedDriver.Id,Name_of_Driver__c=objNamedDriver.Name));
After that create a formula field "Young Driver" and put below in it:
HYPERLINK( Youngest_DriverT__c , Name_of_Driver__c ).
Also, increase the size of the text field. then remove both the text field from layout and only formula field will be visible on layout.
Hi Sourav,
Hope this works for you!
Thanks
Deepak
dpkm20@gmail.com
Finally it worked :).
Hi Sourav,
You can directly drop me a mail if you face any issue in future.
Thanks
Deepak
dpkm20@gmail.com
Regards,
Sourav