You need to sign in to do that
Don't have an account?
jgirard1.3931352457096028E12
Trigger not firing before insert
Hey everyone, I'm new to the forum and to APEX in general, but was hoping someone could help me with the problem I'm having.
I have an object "Job" that has a lookup to another custom field "Well". The Well object has a lookup back to Account standard object and the Job has a lookup to the Account object. Job__r.Account__c is a required field. Basically, I want our users to do the following: Click New to create a new Job record, choose the Well lookup, and the trigger needs to fire as soon as the Job__r.Well__c field is populated to populate the Job__r.Account__c field with that Well's related Account.id. Hopefully that all makes sense! Here is what I have so far, and the trigger is working, but I have to select an Account and save the record for the trigger to fire even when the trigger is a before insert. If I choose an account that is not linked to the correct Well that I chose, it updates it perfectly, but I need it to automatically populate the Account field as soon as the Well is chosen.
trigger UpdateJobAccount on Job__c (before insert){
if (trigger.isBefore &&(trigger.isInsert||trigger.isUpdate))
{
set<Id> WellIds = new set<ID>();
for(Job__c job: trigger.new)
{
WellIds.add(job.Well__c);
}
Map<Id, Id> WellToAccountMap = new Map<Id, Id>();
for(Well__c well :[select Id, Account__c from Well__c where id in: WellIds])
{
WellToAccountMap.put(well.Id,well.Account__c);
}
for(Job__c job: trigger.new)
{
job.Account__c = WellToAccountMap.get(job.Well__c);
}
}
}
I have an object "Job" that has a lookup to another custom field "Well". The Well object has a lookup back to Account standard object and the Job has a lookup to the Account object. Job__r.Account__c is a required field. Basically, I want our users to do the following: Click New to create a new Job record, choose the Well lookup, and the trigger needs to fire as soon as the Job__r.Well__c field is populated to populate the Job__r.Account__c field with that Well's related Account.id. Hopefully that all makes sense! Here is what I have so far, and the trigger is working, but I have to select an Account and save the record for the trigger to fire even when the trigger is a before insert. If I choose an account that is not linked to the correct Well that I chose, it updates it perfectly, but I need it to automatically populate the Account field as soon as the Well is chosen.
trigger UpdateJobAccount on Job__c (before insert){
if (trigger.isBefore &&(trigger.isInsert||trigger.isUpdate))
{
set<Id> WellIds = new set<ID>();
for(Job__c job: trigger.new)
{
WellIds.add(job.Well__c);
}
Map<Id, Id> WellToAccountMap = new Map<Id, Id>();
for(Well__c well :[select Id, Account__c from Well__c where id in: WellIds])
{
WellToAccountMap.put(well.Id,well.Account__c);
}
for(Job__c job: trigger.new)
{
job.Account__c = WellToAccountMap.get(job.Well__c);
}
}
}
Please try this............
trigger UpdateJobAccount on Job__c (before insert){
if (trigger.isBefore &&(trigger.isInsert||trigger.isUpdate))
{
set<Id> WellIds = new set<ID>();
for(Job__c job: trigger.new)
{
WellIds.add(job.Well__c);
}
Map<Id, Id> WellToAccountMap = new Map<Id, Id>();
for(Well__c well :[select Id, Account__c from Well__c where id in: WellIds])
{
WellToAccountMap.put(well.Id,well.Account__c);
}
for(Id wam: WellToAccountMap.keyset())
{
Job__c job = new Job__c();
job.Account__c = WellToAccountMap.get(wam).AccountId;
}
}
}
Regards,
Kirru