You need to sign in to do that
Don't have an account?

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;
}
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:
Caveat - I haven't compiled this, but hopefully you get the idea. There's no doubt other ways to do this too.
All Answers
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:
Caveat - I haven't compiled this, but hopefully you get the idea. There's no doubt other ways to do this too.
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...?
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