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
beginner apexbeginner apex 

its never shows in description - error

trigger tiggercon on Account (before insert , before update)
 {
   for(Account a : trigger.new)
   {
    List<Contact> contact = [SELECT   id , FirstName , LastName , Email  
    FROM Contact WHERE  accountId = : a.Id];
     for(Contact c : contact)
     {
         System.debug ('contact id [' + c.id + '] , FirstName[' + c.FirstName + '] , LastName[' + c.LastName + ']');
         c.Description = c.FirstName + ' ' + c.LastName ;
          update c;
      }
    }
}

 

i worte the code in account object , den created account and contact ,

 

once i saved contact field but it never shows the description box in the firstname and lastname

 

can u help me what problem ll happen

 

 

Regards,

Ganez

bob_buzzardbob_buzzard

If you created the account and then the contact, the trigger won't fire when you create the contact, as its on an account.  You'd need to update the account to get it to process the new contact.

Marko LamotMarko Lamot

trigger tiggercon on Account (before insert , before update)
 {

   for(Account a : trigger.new)
   {
     List<Contact> contact = [SELECT   Description, id , FirstName , LastName , Email  FROM Contact WHERE  accountId = : a.Id];
     for(Contact c : contact)
     {
         System.debug ('contact id [' + c.id + '] , FirstName[' + c.FirstName + '] , LastName[' + c.LastName + ']');
         c.Description = c.FirstName + ' ' + c.LastName ;
      }
      update c;

    }
}

Marko LamotMarko Lamot

and yes, as bob_buzzard says the trigger must be on contact

 

 

trigger tiggercon on Contact (before insert , before update)
 {

   for(Contact c : trigger.new)
   {
         c.Description = c.FirstName + ' ' + c.LastName ;
    }

}

Yoganand GadekarYoganand Gadekar

Hi,

your trigger will invoke on account insert or update.

 

You have written query within for loop, this voilates basic rules in apex. you should avoid query within for loop under all circumstances.