0

I am trying to populate a lookup field based off of a list of records in a different object. The problem is the 2 objects are not related but they have a field that is the same. Is it possible to do this? Below are the details.

Contact.Functional_Pipeline1__c matches Risk_Group_Lookup__c.Functional_Pipeline__c to filter down the records both are picklists

Contact.Job_Name__c is the field that matches Functional_Pipeline1__c.Job_List__c to find the exact record

Contact.Account is the one I need to be populated from Risk_Group_Lookup__c.Account__c both are Ids

Here is what I have tried so far with no luck. I am taking someone elses code and trying to use it for my purpose. I can't figure out the 2nd part for the update. Thanks. Trigger:

trigger LearnersTrigger on Contact (after insert, after update) {
    Set <String> salesJobTitles = new Set <String>();
    for(Contact jbList : trigger.New) {
        if(jbList.Functional_Pipeline1__c == 'SALES'){
            salesJobTitles.add(jbList.Job_Name__c); 
        }
    } 
    Map <String, Risk_Group_Lookup__c> matchingRGLMap = new Map <String, Risk_Group_Lookup__c> ();
    for (Risk_Group_Lookup__c rgLookup : [Select Id, Job_Title__c From Risk_Group_Lookup__c Where Job_Title__c IN :salesJobTitles]) {
        matchingRGLMap.put(rgLookup.Job_Title__c, rgLookup); 
    } 

    List <Contact> contactToUpdate = new List <Contact> (); 
    for(Contact jbList : trigger.New) {
      if (matchingRGLMap.get(jbList.Job_Name__c) != null) {
        matchingRGLMap.get(jbList.Job_Name__c).Account = matchingRGLMap.Account__c;
        contactToUpdate.add(matchingRGLMap.get(jbList.Job_Name__c));
        }  
    }  
    update contactToUpdate;  
}

2 Answers 2

0

If I'm understanding this correctly I think you're pretty close. However, when you're trying to populate your list contactToUpdate it seems like you're trying to update your Map, which contains Risk_Group_Lookup__c records rather than contacts. I've not tested this, or even checked to see if it compiles, but try this to see if it solves your issue.

    trigger LearnersTrigger on Contact (after insert, after update) {
Set <String> salesJobTitles = new Set <String>();
for(Contact jbList : trigger.New) {
    if(jbList.Functional_Pipeline1__c == 'SALES'){
        salesJobTitles.add(jbList.Job_Name__c); 
    }
} 
Map <String, Risk_Group_Lookup__c> matchingRGLMap = new Map <String, Risk_Group_Lookup__c> ();
for (Risk_Group_Lookup__c rgLookup : [Select Id, Job_Title__c From Risk_Group_Lookup__c Where Job_Title__c IN :salesJobTitles]) {
    matchingRGLMap.put(rgLookup.Job_Title__c, rgLookup); 
} 

List <Contact> contactToUpdate = new List <Contact> (); 
for(Contact jbList : trigger.New) {
  /*if (matchingRGLMap.get(jbList.Job_Name__c) != null) {
    *matchingRGLMap.get(jbList.Job_Name__c).Account = matchingRGLMap.Account__c; //you are setting values here of your Map instead of the contact
    *contactToUpdate.add(matchingRGLMap.get(jbList.Job_Name__c)); //you now try to add SObject Risk_Group_Lookup__c to a List<Contact>
    *}  
  */
    if(matchingRGLMap.containsKey(jbList.Job_Name__c){
        //since this is an after trigger, we cannot operate on trigger.new, so instead we create a "new" contact to update
        contactToUpdate.add(new Contact(
            Id = jbList.Id,
            Account = matchingRGLMap.get(jbList.Job_Name__c).Account__c
        ));
    }
}
update contactToUpdate;  
}

You might also want to try doing this as a before trigger so that you can operate directly on trigger.new, without having to do an additional DML statement.

And as always, it is best to utilize handler classes so that the trigger itself is logic-less and the class can handle the processing.

0

I think there are two things you need to do.

1) Include the account record as a part of the SOQL query. Something like:

for (Risk_Group_Lookup__c rgLookup : [Select Id, Job_Title__c, Account__c From Risk_Group_Lookup__c Where Job_Title__c IN :salesJobTitles]) {
        matchingRGLMap.put(rgLookup.Job_Title__c, rgLookup); 
    } 

2) Edit the code in the last for loop

for(Contact jbList : trigger.New) {
      if (matchingRGLMap.get(jbList.Job_Name__c) != null) {
        jbList.AccountId = matchingRGLMap.get(jbList.Job_Name__c).Account__c;
        contactToUpdate.add(jbList);
        }  
    } 

I'm not sure my second bit of code is syntactically correct but give it a try and let me know how it works.

4
  • I think this is what I needed but I am running into an issue. Getting an error "Illegal assignment from Id to Account" for this line of code ` jbList.Account = matchingRGLMap.get(jbList.Job_Name__c).Account__c;` They are both lookups to the Account Object so I am not sure why this is happening. Any ideas?
    – Nathan
    Commented Apr 1, 2016 at 19:58
  • Try the edited code I put above. I changed jbList.Account to jblist.AccountId.
    – dannymorty
    Commented Apr 1, 2016 at 20:32
  • It saved with no errors so I tried running it and got the below error: Error:Apex trigger LearnersTrigger caused an unexpected exception, contact your administrator: LearnersTrigger: execution of AfterUpdate caused by: System.FinalException: Record is read-only: Trigger.LearnersTrigger: line 19, column 1. The code in question is jbList.AccountId = matchingRGLMap.get(jbList.Job_Name__c).Account__c; Not sure why it is saying it is read only. Any ideas?
    – Nathan
    Commented Apr 1, 2016 at 21:51
  • Ah yes, the issue is that you can't perform operations directly on records as a part of trigger.new context variables. Here is some more information on that. @ToddSprinkel has code that should solve this. Essentially, he instantiates new contacts and sets their ids to the value of the contacts that he wants to updates, updates the account value and then performs the update operation. Boom!
    – dannymorty
    Commented Apr 2, 2016 at 8:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .