0

I am trying to call this invocable method via PB when the Demand_Gen_Owner OR the AccountOwner is changed. It doesn't fire and I cannot determine why.

global class UpdateContactsFromAccount {

@InvocableMethod(label='save info' description='Update Contact Owners')
global static void saveInformation (List<Id> accountIds) {
    List<Account> listAccount = [Select Id, Demand_Gen_Owner__c, OwnerId FROM Account WHERE Id In :accountIds];
    List<Contact> listContact = [Select Id, AccountId, OwnerId FROM Contact WHERE AccountId In :accountIds];

    Map<Id, Account> mapIdToAccount = new Map<Id, Account>();
    mapIdToAccount.putAll(listAccount);
    System.debug('Accounts for which Contacts to be updated:: mapIdToAccount : ' + mapIdToAccount);
    for(Contact con: listContact){
        if(con.AccountId != null){
            Account acc = mapIdToAccount.get(con.AccountId);
            if(acc.Demand_Gen_Owner__c != 'Unassigned' || acc.Type != 'Current Customer'){
                con.OwnerId = acc.Demand_Gen_Owner__c;
            }
            else {
                con.OwnerId = acc.OwnerId;
            }
        }
    }
    update listContact;
}

} call apex action in PB

  • 2
    can you add label for innvocableMethod annotation like @InvocableMethod(label='save info' description=description') try – sdandamud1 Jul 2 '19 at 17:18
  • 2
    When you say "it doesn't fire", do you mean that you've looked at debug logs and the method is not actually being called? Or just that the expected field update isn't happening? This line looks odd to me: if(acc.Demand_Gen_Owner__c != 'Unassigned' || acc.Demand_Gen_Owner__c != null || acc.Type != 'Current Customer') If Demand_Gen_Owner__c is a lookup to user, it will only ever have a UserId or be null, so 1st check will always be TRUE, and since the 3 checks are OR'ed together, you'll ALWAYS be assigning acc.Demand_Gen_Owner__c to con.OwnerId. – Thomas Taylor Jul 2 '19 at 17:34
  • When I say "it doesn't fire", I mean that the expected update is not happening. The Log in dev console is blank. I understand what you mean about the line being odd. I have modified and edited it in my post. I kept 'Unassigned' in there because if the Demand Gen Owner UserId is the 'Unassigned' user, the contacts need to update to the account owner, if it's a different UserId, the contacts need to be assigned to the Demand Gen UserId. – Laura Babb Jul 2 '19 at 18:05
  • 1
    You don't need test coverage until you want to deploy to Prod. Have you tried adding another action, like a field update or chatter post, on the same logic branch as the InvocableAction in your Process? To confirm that the Process logic is getting to the right spot. – Thomas Taylor Jul 2 '19 at 20:24
  • 1
    (1) what is the target object of the PB; (2) are you actually passing an accountId and not a UserId in the PB reference to the invocable Apex argument? Screen shot shows truncation – cropredy Jul 2 '19 at 23:53
0

You've got an issue wherein you are assigning a field that has a string value

if(acc.Demand_Gen_Owner__c != 'Unassigned' || acc.Type != 'Current Customer')     {
            con.OwnerId = acc.Demand_Gen_Owner__c;  // won't work
        }

to an ID variable (con.OwnerId). Are you trying to reference acc.Demand_Gen_Owner__r.Name ? Object model is not clear.

In addition, this method could be coded more concisely

global class UpdateContactsFromAccount {

  @InvocableMethod(label='save info' description='Update Contact Owners')
  global static void saveInformation (List<Id> accountIds) {

  List<Contact> contacts = [SELECT Id, Account.Demand_Gen_Owner__c, Account.Type, 
                                   Account.OwnerId // use relationships to get Account fields
      FROM Contact 
      WHERE AccountId In :accountIds AND accountId != null];

  for(Contact con: contacts){
        if(con.Account.Demand_Gen_Owner__c != 'Unassigned' || // fix this !! 
           con.Account.Type != 'Current Customer'){
           con.OwnerId = con.Account.Demand_Gen_Owner__c;  // can this be null?
        }
        else {
            con.OwnerId = con.Account.OwnerId;
        }
  }
  update contacts;
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.