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
dkndkn 

trigger to change record type ....

Hi

 

I have a contact record and a related list as application.There is an look up relationship between application and contact record. I have written  a trigger to change the record type of the application based on  a particular value entered on the contact record. This trigger works fine , but the issue i am facing is , what if I have mutliple application records . I want to make the record type change only the most recent application rather than all the application records....I would really appreciate any help on this.....thank you ....below is the main part of the code, working on...

 

trigger rcdtypechg on Contact (before update) {


set<Id> chngRcdtype=new set<Id>();
    
    for(Contact co : Trigger.New)
    {
        if(co.What_are_you_applying_to__c=='Interlochen Summer Arts Camp')
            chngRcdtype.add(co.Id);
            
    }
    
    List<Enrollment_Opportunity__c> app=[Select Id,RecordTypeId from Enrollment_Opportunity__c where Enrollment_Opportunity__c.Applicant__c IN:chngRcdtype];
    for(Enrollment_Opportunity__c ap:app)
    {
        ap.RecordTypeId='012T00000004cfd';
    }
    
    update app;
    
}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

This is a little tricky if your trigger is bulkified (processing multiple contact records).  As you are retrieving all of the applications for all contacts, there's no way that I know of to limit the query to only return one per id.

 

Off the top of my head, I think something like the following may work:

 

 

trigger rcdtypechg on Contact (before update) {


set<Id> chngRcdtype=new set<Id>();
    
    for(Contact co : Trigger.New)
    {
        if(co.What_are_you_applying_to__c=='Interlochen Summer Arts Camp')
            chngRcdtype.add(co.Id);
            
    }

    // create a map of application keyed by contact id. 
    // Then extract all applications, oldest first
    // Then iterate and put the app in the map based on the contact id
    // Each subsequent put will overwrite the previous one,
    // leaving a map containing the most recent app for each contact
    
    List<Enrollment_Opportunity__c> app=[Select Id,RecordTypeId, LastModifiedDate, Enrollment_Opportunity__c.Applicant__c from Enrollment_Opportunity__c where Enrollment_Opportunity__c.Applicant__c IN:chngRcdtype order by LastModifiedDate asc];
    Map<Id, Enrollment_Opportunity__c> appMap=new Map<Id, Enrollment_Opportunity__c>();

    for(Enrollment_Opportunity__c ap:app)
    {
        appMap.put(Enrollment_Opportunity__c.Applicant__c, app);
    }

    for(Enrollment_Opportunity__c ap:appMap.values())
    {
        ap.RecordTypeId='012T00000004cfd';
    }
    
    update appMap.values();
    
}

 

Caveat - I haven't compiled this, but hopefully you get the idea.  There's no doubt other ways to do this too.

 

 

 

All Answers

bob_buzzardbob_buzzard

This is a little tricky if your trigger is bulkified (processing multiple contact records).  As you are retrieving all of the applications for all contacts, there's no way that I know of to limit the query to only return one per id.

 

Off the top of my head, I think something like the following may work:

 

 

trigger rcdtypechg on Contact (before update) {


set<Id> chngRcdtype=new set<Id>();
    
    for(Contact co : Trigger.New)
    {
        if(co.What_are_you_applying_to__c=='Interlochen Summer Arts Camp')
            chngRcdtype.add(co.Id);
            
    }

    // create a map of application keyed by contact id. 
    // Then extract all applications, oldest first
    // Then iterate and put the app in the map based on the contact id
    // Each subsequent put will overwrite the previous one,
    // leaving a map containing the most recent app for each contact
    
    List<Enrollment_Opportunity__c> app=[Select Id,RecordTypeId, LastModifiedDate, Enrollment_Opportunity__c.Applicant__c from Enrollment_Opportunity__c where Enrollment_Opportunity__c.Applicant__c IN:chngRcdtype order by LastModifiedDate asc];
    Map<Id, Enrollment_Opportunity__c> appMap=new Map<Id, Enrollment_Opportunity__c>();

    for(Enrollment_Opportunity__c ap:app)
    {
        appMap.put(Enrollment_Opportunity__c.Applicant__c, app);
    }

    for(Enrollment_Opportunity__c ap:appMap.values())
    {
        ap.RecordTypeId='012T00000004cfd';
    }
    
    update appMap.values();
    
}

 

Caveat - I haven't compiled this, but hopefully you get the idea.  There's no doubt other ways to do this too.

 

 

 

This was selected as the best answer
BritishBoyinDCBritishBoyinDC

I'm wondering if you compile a set of unique contact ids in the trigger, then use an aggregration query to pull the application with the max(createddate)  for each Contact in the set - (you could use any date field)  -  then iiterate through the aggregration results,  add the applications to a list, and update it...?

Satya Ranjan MohantySatya Ranjan Mohanty
HI 
i need small help from u for writng a trigger

my requirment is i need to write a trigger  that changes the record type when Lead status changes,
can u help me out

Thanks in advance
satya