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
lovetolearnlovetolearn 

Trigger code error. Please help!

Hello,

 

I am trying to write a trigger that updates the value of a field on an object when records are created on another object that references that particular record on the first object. For instance, I have a field on the ACCOUNT object called "Available number of contacts," I would like the value in this field to reduce by 1 whenever a record in the CONTACT object is created that references the particular Account. Here is the code, but I keep getting the same error when I hit save: "Variable Available_Number_of_Contacts does nto exist. Please review my code and let me know where I went wrong. Thank you.

 

 

trigger decreaseAvailableContactsOnAccount on Contact (after insert) {
  Map<Id,Integer> accountTotal = new Map<Id,Integer>();
  for(Contact c:Trigger.new) {
    if(c.accountid==null)
      continue;
    if(accountTotal.get(c.accountid)==null)
      accountTotal.put(c.accountid,1);
    else
      accountTotal.put(c.accountid,accountTotal.get(c.accountId)+1);
  }
  List<Account> accounts = new List<Account>();
  for(Account a:[SELECT id, Number_of_Available_Contacts__c 
                 FROM Account
                 WHERE id in :accountTotal.keyset()]) 
  {
    a.Number_of_Available_Contacts__c -= accountTotal.get(a.id);
    accounts.add(a);
  }
  update accounts;
}

 

sfdc.dev.ax856sfdc.dev.ax856

Can you check api name of the field.It looks like your are using wrong api name of the field else trigger code is correct.

lovetolearnlovetolearn

I copy and past of the exact API name. 

sfdc.dev.ax856sfdc.dev.ax856

But the error message u have posted Available_Number_of_Contacts does n't exist differrent from field( Number_of_Available_Contacts__c) u have used in code


lovetolearnlovetolearn

Yeah i changed the name around a couple of times to see if that was the problem. the error message doesnt state that the field doesn't exist. it states that the variable does not exist:
"Error: Compile Error: Variable does not exist: Number_of_available_contacts__c at line 16 column 5"

 

raseshtcsraseshtcs

Could you try the following code

trigger decreaseAvailableContactsOnAccount on Contact (after insert) {
  Map<Id,Integer> accountTotal = new Map<Id,Integer>();
  for(Contact c:Trigger.new) {
    if(c.accountid==null)
      continue;
    if(accountTotal.get(c.accountid)==null)
      accountTotal.put(c.accountid,1);
    else
      accountTotal.put(c.accountid,accountTotal.get(c.accountId)+1);
  }
  List<Account> accounts = new List<Account>([SELECT id,Number_of_Available_Contacts__c FROM Account WHERE id in :accountTotal.keyset()]);
  
  for(Account a:accounts) 
  {
    a.Number_of_Available_Contacts__c -= accountTotal.get(a.id);
  }
  update accounts;
}

 

lovetolearnlovetolearn

I get this error when I tried to run it: Invalid initial value type LIST<Account> for LIST<Account> at line 11 column 28