• jgirard1.3931352457096028E12
  • NEWBIE
  • 0 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
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);
  }
 
}
}
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);
  }
 
}
}