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
BrittanieBrittanie 

Trigger to autopop custom lookup on account from custom lookup on opportunity

I'm learning as I go with this but have successfully manipulated a good 5 triggers.  Can't seem to figure this one out and besides necessary it is also the solution to why a workflow that works fine in sadbox wont let me save without this field on the account filled in on my live sf.  I apologize after about 80 rounds and trying to manipulate off other posts the syntax my be destroyed!

 

Account

Contact_Declining__c

 

needs to be updated from the Primary_Contact__C on the Opportunity.

 

Trigger UpdateContactDeclining on Account(before insert, before update)
{

Set<id> OpportunityIds = new Set<id>();
for (Account a : Trigger.new)
{
OpportunityIds.add(a.Contact_Declining__c );
}
Map<Id, Opportunity> OpportunityContactMap = new Map<id, Opportunity>(
[SELECT Id,Primary_Contact__c FROM Opportunity WHERE Id IN :OpportunityIds]);

for (Account a: Trigger.new) 
{
if (a.Contact_Declining__c == null && OpportunityContactMap .containsKey(a.Opportunity )) 
{
a.Contact_Declining__c = OpportunityContactMap.get (Opportunity.Primary_Contact__c);
}
}
}

 

Best Answer chosen by Admin (Salesforce Developers) 
Avidev9Avidev9

You never made an update to Account that was queried

 

trigger UpdateAcctContactDeclining on Opportunity (before insert, before update)
{
   //map to hold the account id and the corresponding related opportunity
    Map<Id,Opportunity> mapAccIdToOpp=new Map<Id,Opportunity>();
    List<Id> AccountsToUpdate = new List<Id>{};

  // Find accounts to update
  for(Opportunity o: Trigger.new){
    if (o.AccountId != Null)  
    {
      AccountsToUpdate.add(o.AccountId);
    }

  // Update the accounts
  Account[] accts = [SELECT Id, Contact_Declining__c FROM Account WHERE Id IN :AccountsToUpdate];

  for(Account a: accts){
    a.Contact_Declining__c = o.Primary_Contact__c ;
  }
update accts; //do the update } }

All Answers

swatKatswatKat

Firstly an account can be the parent of many opportunities. How do u want to choose the one child opportunity which will be used to populate the custom look up on parent Account ?

BrittanieBrittanie

Our company only has one opportunity at a time for an account.  also the field updates allow you to pull from opp to act but not on lookups.

swatKatswatKat

 And both these lookups are to Contact so the following code should help u :

Trigger UpdateContactDeclining on Account(before insert,before update)
{
    //map to hold the account id and the corresponding related opportunity
    Map<Id,Opportunity> mapAccIdToOpp=new Map<Id,Opportunity>();
   
    //query all the opporunities with parent accounts matched in Trigger.New and add them to map
    for(Opportunity opp:[SELECT Id,Primary_Contact__c,AccountId FROM Opportunity WHERE AccountId in:Trigger.New]){
        mapAccIdToOpp.put(opp.AccountId,opp);
    }
    
    //loop through the Updated accounts in Trigger.New and find the matching opportunity from map
    for (Account a: Trigger.new) 
    {
        if (a.Contact_Declining__c == null && mapAccIdToOpp.containsKey(a.Id)){
            Opportunity relatedOpp=mapAccIdToOpp.get(a.Id);
            a.Contact_Declining__c = relatedOpp.Primary_Contact__c ;
        }
    }
}

 One more thing this functionality is not relevant to account insert , because you will not have any opportunities which are already related to the accounts to be inserted.

BrittanieBrittanie

hmmm. i looks good and no errors... however the end result is the field still not updating... I'm honestly at a loss as it looks to cover everything and the fields are correct.

swatKatswatKat

Try looking at the Debug logs. There might be some other process interfering.

Avidev9Avidev9
@Brittanie
As per My Understanding you want to bring "Primary_Contact__c" from Opp to Account "Contact_Declining__c"

how about moving the triggers to Opportunity ?
Whenever you will be doing an update the changes will be on Opportunity object and not in the Account.

So it makes sense to have a trigger on Opportunity rather than account.
BrittanieBrittanie

So I've put together a trigger for the opportunity that I thought was great and saved with no problem... but when I try and add a primary contact to the opportunity I get the following very scarry error and it won't let me save.

 

Since this seems like such a simple task this is becoming increasingly irratating that I can't seem to get it to work! (probably due to the fact I am very new at this.)

 

Error:Apex trigger UpdateAcctContactDeclining caused an unexpected exception, contact your administrator: UpdateAcctContactDeclining: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 001S000000dBpniIAC; first error: FIELD_FILTER_VALIDATION_EXCEPTION, Value does not exist or does not match filter criteria.: [Contact_Declining__c]: Trigger.UpdateAcctContactDeclining: line 18, column 1

 

 
trigger UpdateAcctContactDeclining on Opportunity (before insert, before update)
{
   
    List<Id> AccountsToUpdate = new List<Id>{};

  // Find accounts to update
  for(Opportunity o: Trigger.new){
    if (o.AccountId != Null)  
    {
      AccountsToUpdate.add(o.AccountId);
    }

  // Update the accounts
  Account[] accts = [SELECT Id, Contact_Declining__c FROM Account WHERE Id IN :AccountsToUpdate];

  for(Account a: accts){
    a.Contact_Declining__c = o.Primary_Contact__c ;
    update a;
  }
}
 }

 

BrittanieBrittanie

Fixed the code to be able to save the Primary_Contact_c in the opportunity but it still doesn't update the Contact_Declining__c  on the Account...

 

 

I should also add that some how on the account trigger under field dependencies it checks the right field under account to be updated but the object itself isn't checked to be updated... I know that it should be, I'm just not sure how to make that happen

 

Now I have codes both ways that say they are supposed to do what I want but without the final result enacted.

 

trigger UpdateAcctContactDeclining on Opportunity (before insert, before update)
{
   //map to hold the account id and the corresponding related opportunity
    Map<Id,Opportunity> mapAccIdToOpp=new Map<Id,Opportunity>();
    List<Id> AccountsToUpdate = new List<Id>{};

  // Find accounts to update
  for(Opportunity o: Trigger.new){
    if (o.AccountId != Null)  
    {
      AccountsToUpdate.add(o.AccountId);
    }

  // Update the accounts
  Account[] accts = [SELECT Id, Contact_Declining__c FROM Account WHERE Id IN :AccountsToUpdate];

  for(Account a: accts){
    a.Contact_Declining__c = o.Primary_Contact__c ;
  }
}
 }

 

Avidev9Avidev9

You never made an update to Account that was queried

 

trigger UpdateAcctContactDeclining on Opportunity (before insert, before update)
{
   //map to hold the account id and the corresponding related opportunity
    Map<Id,Opportunity> mapAccIdToOpp=new Map<Id,Opportunity>();
    List<Id> AccountsToUpdate = new List<Id>{};

  // Find accounts to update
  for(Opportunity o: Trigger.new){
    if (o.AccountId != Null)  
    {
      AccountsToUpdate.add(o.AccountId);
    }

  // Update the accounts
  Account[] accts = [SELECT Id, Contact_Declining__c FROM Account WHERE Id IN :AccountsToUpdate];

  for(Account a: accts){
    a.Contact_Declining__c = o.Primary_Contact__c ;
  }
update accts; //do the update } }
This was selected as the best answer
BrittanieBrittanie

back to getting the error

 

Error:Apex trigger UpdateAcctContactDeclining caused an unexpected exception, contact your administrator: UpdateAcctContactDeclining: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 001S000000dOjOCIA0; first error: FIELD_FILTER_VALIDATION_EXCEPTION, Value does not exist or does not match filter criteria.: [Contact_Declining__c]: Trigger.UpdateAcctContactDeclining: line 20, column 1

 

Avidev9Avidev9

This one you have to solve.

the "Contact_Declining__c" field has a lookup filter added and the record value you are setting is not satisfying the filter condition

BrittanieBrittanie

yup, I'm a big dork! works like a charm.

 

Thanks for the help!

BrittanieBrittanie

question on the test as it is a lookup... what would I put for Primary_Contact__c= (??)

Currently getting the following error

 

Error Message System.StringException: Invalid id: Contact Stack Trace

Class.UpdateAcctContactDecliningTestClass.myUnitTest: line 14, column 1

 

@isTest

private class UpdateAcctContactDecliningTestClass { 

   static testMethod void myUnitTest(){
    Account accts = new Account (Contact_Declining__c = Null);
      
          System.debug('Created Account with Id: ' + accts.id);

        
            // fill in all other required fields fields
   
   
    Opportunity o = new Opportunity(AccountId=accts.Id,Primary_Contact__c = 'Contact');
        
   
   o.Primary_Contact__c = 'Contact';
    
    Test.startTest();
    
        //verify if the trigger did the job  
        System.assertEquals(o.Primary_Contact__c,[SELECT Contact_Declining__c FROM Account WHERE
    Id=:o.Id].Contact_Declining__c ); 
       
             update accts;

       
   Test.stopTest();

    }
}