0

can anyone suggest plz. I have a custom object " Quotation" where there is a Driver details section with below fields,

enter image description here

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

enter image description here

Each has an Age maintained in the 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. Can you plz suggest how to achieve this, i was trying formula field and process builder but couldn't able to make it work. when we keep on adding new records, it should recalculate and choose the latest min one. Thanx

I tried to write the trigger, but got stuck, anyone plz help me on rectifying the below trigger,

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;
}
2
  • 1
    What part of this doesn't work? There's no point saying "I got stuck". You need to help people so they can help you.
    – Nick Cook
    Oct 18, 2016 at 6:47
  • Sorry Nick, But I got stuck means, I really don't know which part got wrong here, else I would have specified clearly the part. Every part seems to be fine for me but its not working, so I clearly don't have idea. but thnks for the suggestion.
    – sourav
    Oct 18, 2016 at 8:48

1 Answer 1

0

If I understand correctly, you want to store the youngest driver's age in every driver's record and mark a driver as young driver if he/she is the youngest.

If that is the case - first of all, you shouldn't store the youngest driver's age in a custom field. Instead, you should be using a custom setting. Think about it: now you only have several driver's record and it is fine. But if you have one million driver's record in the future, every time you need to update 1 million records if a younger driver is coming. That would be insane.

I wrote a sample code for you for insert and update scenario. I haven't tested and it definitely need some polish to avoid infinite loop. But it would be able to give you an idea:

trigger trgYoungDriver on Quotation__c (before insert, before update) {
    Youngest_age__c ya = Youngest_age__c.getOrgDefaults();
    Decimal youngest_age = Decimal.valueOf(ya.age__c);

    Boolean younger_found = false;
    Quotation__c youngest = new Quotation__c();
    List<Quotation__c> quotation_list = [Select Id, Name, Contact_Number__r.Age__c, Young_Driver__c From Quotation__c Where Id in :Trigger.New];
    for(Quotation__c q: Trigger.New) {
        if(q.Contact_Number__r.Age__c < youngest_age) {
            younger_found = true;
            youngest = q;
        }
    }

    if(younger_found) {
        ya.age__c = '' + youngest.Contact_Number__r.Age__c;
        update ya;

        youngest.Young_Driver__c = true;

        Quotation__c previous_youngest = [Select Id, Young_Driver__c From Quotation__c Order by Contact_Number__r.Age__c Limit 1];
        previous_youngest.Young_Driver__c = false;

        update previous_youngest;
    }
}

You should be able to polish it and write your delete trigger by yourself. If you still can't, you really should learn more about coding.

3
  • Hi Lance. thanks for the input. I am new to SF coding and certainly I am keep on learning each day.
    – sourav
    Oct 18, 2016 at 8:40
  • Actually, the need is, The young driver is going to be choosen only fro the few records which are there in the quotation object record , and each quotation object record generally have maximum 4 to 5 driver records, from those 5 only it need to choose the youngest one and not from the entire driver records maintained in the system.
    – sourav
    Oct 18, 2016 at 8:42
  • It would be complicated in this case. And in that case, the code should be on driver trigger instead of quotation. You need to utilise map and find out the youngest value in the current quotation object.
    – Lance Shi
    Oct 18, 2016 at 11:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.