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
Sourav PSourav P 

How to autopopulate the field name from a set of records depends upon the minimum age fields maintained in another object

Dear All,, can anyone ssuggest plz.
I have a custom object " Quotation" where there is a Driver details section with below fields,
User-added image
Now, I have a related object on Quotation object, which is a look up to the " Personal account " field. Below are the various test names of the drivers

User-added image
Each has an Age maintained in teh respective records. Now i need to select the minimum age from all these records and whoever has the min age, that name will reflect in the " Young driver" field with all other detailed fields.
when we keep on adding new records, it should recalculate and choose the latest min one. I tried to write the trigger, but got stuck, can anybody plz try to rectify my trigger, thnx
 
trigger trgYoungDriver on Quotation__c (after insert, after update, after delete) {
Set<Id> orgIds = new Set<Id>();
List< Account> updates = new List<Account>();
Quotation__c[] qot = null;

 if(Trigger.isInsert||Trigger.isUpdate)
    {
        qot = Trigger.new;
    }
    else if(Trigger.isDelete)
    {
        qot = Trigger.old;
    }
    
for(Quotation__c  q : [select id,
        min(Contact_Number__r.Age__c),Young_Driver__c,AgeYoungD__c from Quotation__c 
        where id IN :Trigger.new])
for(Quotation__c  p : [select id,
        min(Contact_Number__r.Age__c),Young_Driver__c,AgeYoungD__c from Quotation__c 
        where id IN :Trigger.old])
{
  
        updates.add(q.AgeYoungD__c)=q.Contact_Number__r.Age__c;
            
    }

update updates;


}


 
 
EldonEldon
Hi Sourav,

Refer the below trigger for  your req. Change the contact object to your custom object quotation__c and instead of age__c i have used testnumber__c and in the account object i have used one field called sumoftotalnumber__c instead of the field u want to update in account object.
 
trigger devcomcont1 on Contact (after insert,after update) {
    contact MinCOnt = new contact();
    list<id> AccIds = new list<id>();
    map<id,list<contact>> accWithCont = new map<id,list<contact>>();
    decimal min;
    if(Trigger.isInsert||Trigger.isUpdate)
        
    {
        
        for(contact c : trigger.new){
            min=c.testNumber__c;
            accids.add(c.accountid);
            if(accWithCont.containsKey(c.accountid)){
                accWithCont.get(c.accountid).add(c);
            }
            else{
                accWithCont.put(c.accountid,new list<contact>{c});
            }
        }
        list<account> ToUpdateAccount = new list<account>();
        
        for(id acc : accwithcont.keySet() ){
            
            contact mincontact = new contact();
            for(contact c : accwithcont.get(acc)){
                
                if(c.testNumber__c<=min){
                    mincontact = c;
                    min=c.testNumber__c;
                }
            }
            if(min>0){
                account a = [select id,name,SumofTotalNumber__c from account where id = : acc];
                a.SumofTotalNumber__c=mincontact.testNumber__c;
                ToUpdateAccount.add(a);
            }
        }
        update toupdateaccount;
    }
    
}

Regards
Sourav PSourav P
Thanks Eldon, Let me try this
EldonEldon
Hi,

Please close the thread if it solved your problem

Thanks
Sourav PSourav P
Hi Eldon, Thanks . For this issue, i tried to put the values as you suggested but  i think i have jumbled everything. plz suggest on teh below,
Actually the need is,

I want to update the below fields, on the " Quotation__c" object, ( so this is the main field where the min age updation should come, but can we make all details to be updated, like e.g Young drive is a name string data type, gender, MS are picklists, so depends upon min age records, all fields here should get updated.)
User-added image

The objects where age records are getting stored is the " Driver__c" object, these are the driver names with all details and various ages and other values.
User-added image
I have written the above code as per your suggestions, but seems to me it should be opposite , the code is showing me various errors. Thanks
 
trigger devcomcont1 on Quotation__c (after insert,after update) {
    Quotation__c MinCOnt = new Quotation__c();
    list<id> DriverIds = new list<id>();
    map<id,list<Quotation__c>> DrivWithQuot = new map<id,list<Quotation__c>>();
    decimal min;
    if(Trigger.isInsert||Trigger.isUpdate)
        
    {
        
        for(Quotation__c c : trigger.new){
            min=c.AgeYoungD__c;
            Driverids.add(c.Driverid);
            if(DrivWithQuot.containsKey(c.Driverid)){
               DrivWithQuot.get(c.Driverid).add(c);
            }
            else{
                DrivWithQuot.put(c.Driverid,new list<Quotation__c>{c});
            }
        }
        list<Driver__c> ToUpdateQuotation = new list<Driver__c>();
        
        for(id dri :DrivWithQuot.keySet() ){
            
         Quotation__c mindriver = new Quotation__c();
            for(Quotation__c c :DrivWithQuot.get(dri)){
                
                if(c.Driver_Age__c<=min){
                   mindriver = c;
                    min=c.Driver_Age__c;
                }
            }
            if(min>0){
               Quotation__c a = [select id,name,AgeYoungD__c from Quotation__c where id = : dri];
                a.AgeYoungD__c=mindriver.Age__c;
                ToUpdateQuotation.add(a);
            }
        }
        update ToUpdateQuotation;
    }
    
}




 
EldonEldon
Which object is parent and which object is child ?
Also name the fields in each objects and from which field you want to take values and to where you want to store them?
Sourav PSourav P
Hi Eldon,
Parent object : Quotation__c
Child object : Driver__c

From the parent object Quotation__c, the below fields should get filled up ( depends upon the min age from the driver records attached to the Quotation object, say if 4 driver records attached, then min age from this 4 drivers should reflect on the below fields on Quotation__c),

1.Young Driver         (Young_Driver__c)
2. AgeAge  (YoungD__c)
3. Gender (GenderYoungD__c)
4. Marital Status (Marital_Status__c)
5. Driving Experience (Driving_ExperienceYoungD__c)
6. Accidents in the last 12 Months  (Accidents_in_the_last_12_MonthsYoungD__c)

In the child object, Driver__c, the same above fields do exists as per below,

1. Contact Number (Contact_Number__c) [ move to Young driver name field ]
2. Driver Age  (Driver_Age__c)
3. Driver Gender (Driver_Gender__c)
4.Driver Marital StatusDriver_Marital_Status__c
5. Accidents in the last 12 MonthsAccidents_in_the_last_12_Months__c

Thanks
 
EldonEldon
Replace contact with Driver__c and account with Quotation__c and testnumber__c with Driver_Age__c (child object) and sumoftotalnumber__c with youngD__c (parents) and add other fields too
 
trigger devcomcont1 on Contact (after insert,after update) {
    contact MinCOnt = new contact();
 contact mincontact = new contact();
    list<id> AccIds = new list<id>();
    map<id,list<contact>> accWithCont = new map<id,list<contact>>();
    decimal min;
    if(Trigger.isInsert||Trigger.isUpdate)
        
    {
        
        for(contact c : trigger.new){
            min=c.testNumber__c;
mincontact =c;
            accids.add(c.accountid);
            if(accWithCont.containsKey(c.accountid)){
                accWithCont.get(c.accountid).add(c);
            }
            else{
                accWithCont.put(c.accountid,new list<contact>{c});
            }
        }
        list<account> ToUpdateAccount = new list<account>();
        
        for(id acc : accwithcont.keySet() ){
            
           
            for(contact c : accwithcont.get(acc)){
                
                if(c.testNumber__c<=min){
                    mincontact = c;
                    min=c.testNumber__c;
                }
            }
            if(min>0){
                account a = [select id,name,SumofTotalNumber__c from account where id = : acc];
                a.SumofTotalNumber__c=mincontact.testNumber__c;

//a.GenderYoungD__c= mincontact.Driver_Gender__c;
//UPDATE ALL THE FIELDS OF PARENT LIKE ABOVE

                ToUpdateAccount.add(a);
            }
        }
        update toupdateaccount;
    }
    
}

Let me know if there is any issues
pavan501pavan501
Hi,

The following code works only if one young driver exists.
 
trigger SetYoungDriver on Driver__c (after insert,after Update) {
		
    Driver__c driver = new Driver__c();
    Quotation__c q = new Quotation__c();
    
    for(Driver__c d:Trigger.new){
        q.Id = d.Quotation__c;
    }
    system.debug('Quotation ID'+q.Id);
    driver = [select id,name,age__c from Driver__c where quotation__r.id =: q.id order by age__c Limit 1];
    
    q.Age__c = driver.Age__c;
    q.Young_Driver__c = driver.Name;
    
    update q;
    
}

Regards,
Pavan.