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
KeerthigeeKeerthigee 

Issue related to Account and Contact

Hi All,

Here contact is having custom field primary of type checkbox
● Each account may have multiple contacts but only one marked “Primary”
● Each account must have 1 primary contact.
● The account should display the name of its primary contact on the account detail screen (i.e. in a field).

I wrote code as 

trigger primarycontact on Contact (before insert,before update) {
   
    set<id> accid= new set<id>();
    set<id> conid=new set<id>();
    list<contact> con=new list<contact>();
    for(contact c:trigger.new)
    {
       if(trigger.isInsert)
       {
          if(c.Primary__c == true)
          {
             AccID.add(c.Accountid);
             conId.add(c.id);
           }
        }
        else if(trigger.isUpdate)
        {
           if(trigger.oldMap.get(c.id).Primary__c != c.Primary__c && c.Primary__c == true)
           {
              AccID.add(c.Accountid);
              conId.add(c.id);
            }
        }
      
       }
       for(contact cc:[select id, AccountID,Primary__c from Contact where AccountId IN : AccID AND Primary__c=true])
       {
          if(cc != null && !(ConId.contains(cc.Id)))
          {
                 contact co = new contact(id=cc.Id);
                 co.Primary__c = false;
                 con.add(co);
          }
       }
        if(!Con.isEmpty())
        {
        update con;
      }
}

But ,I didn't get any update in field. Can anyone help me?

Kindly support and suggest.

Thanks.
Hargobind_SinghHargobind_Singh
Hi Gita, 

Assuming that you have a lookup field on Account for Primary Contact (primary_Contact__c), I've added code to your trigger to update Account as well. Your current trigger only handles primary checkbox for Contact. 
trigger UpdateAccountPrimary on Contact (after insert, after update) {
	set<id> accid= new set<id>();
	set<id> conid=new set<id>();
	list<contact> con=new list<contact>();
	list<Account> accList = new List<Account>(); 
	for(contact c:trigger.new)
	{
		if(trigger.isInsert)
		{
			if(c.Primary__c == true)
			{
				AccID.add(c.Accountid);
				conId.add(c.id);
				Account a1 = new Account(id = c.AccountID); 
				a1.Primary_Contact__c = c.id; 
				accList.add(a1); 
			}
		}
		else if(trigger.isUpdate)
		{
			if(trigger.oldMap.get(c.id).Primary__c != c.Primary__c && c.Primary__c == true)
			{
				AccID.add(c.Accountid);
				conId.add(c.id);
				Account a1 = new Account(id = c.AccountID); 
				a1.Primary_Contact__c = c.id; 
				accList.add(a1); 
			}
		}

	}
	for(contact cc:[select id, AccountID,Primary__c from Contact where AccountId IN : AccID AND Primary__c=true])
	{
		if(cc != null && !(ConId.contains(cc.Id)))
		{
			contact co = new contact(id=cc.Id);
			co.Primary__c = false;
			con.add(co);
		}
	}
	if(!Con.isEmpty())
	{
		update con;
	}
	if(!accList.isEmpty())
	{
		update accList;
	}
}


KeerthigeeKeerthigee
Hi,

Thank you for immediate reponse.t

I didn't get any difference with creation of field i.e Primary__c  in Contact object and make it as primary.

Eventhough I didn't marked as primary , that time also updated as primary contact.

Awaiting for your response.


Hargobind_SinghHargobind_Singh
Hi Geetha, Your code was already working for primary checkbox. You need to add a field on account object to shoe primary contact.
KeerthigeeKeerthigee
Hi,

Thank you for immediate response.

I created a  lookup field  i.e PrimaryContact __c  in account object.
Whenever  I had entered one record in contact object ,then also i didnt  get any update automatically to PrimaryContact field in account object.

Awaiting for your response

Hargobind_SinghHargobind_Singh
Here is the modified code: 

trigger UpdateAccountPrimary on Contact (after insert, after update) {
	set<id> accid= new set<id>();
	set<id> conid=new set<id>();
	list<contact> con=new list<contact>();
	list<Account> accList = new List<Account>(); 
	List<Contact> conList1 = [select id, Primary__c, AccountId from contact where id in :trigger.new]; 
	for(contact c:conList1)
	{
		if(trigger.isInsert)
		{
			if(c.Primary__c == true)
			{
				AccID.add(c.Accountid);
				conId.add(c.id);
				Account a1 = new Account(id = c.AccountID); 
				a1.Primary_Contact__c = c.id; 
				accList.add(a1); 
			}
		}
		else if(trigger.isUpdate)
		{
			if(trigger.oldMap.get(c.id).Primary__c != c.Primary__c && c.Primary__c == true)
			{
				AccID.add(c.Accountid);
				conId.add(c.id);
				Account a1 = new Account(id = c.AccountID); 
				a1.Primary_Contact__c = c.id; 
				accList.add(a1); 
			}
		}

	}
	
	for(contact cc:[select id, AccountID,Primary__c from Contact where AccountId IN : AccID AND Primary__c=true])
	{
		if(cc != null && !(ConId.contains(cc.Id)))
		{
			contact co = new contact(id=cc.Id);
			co.Primary__c = false;
			con.add(co);
		}
	}
	if(!Con.isEmpty())
	{
		update con;
	}
	if(!accList.isEmpty())
	{
		update accList;
	}
}


KeerthigeeKeerthigee
Hi,

Thank you for response.

I got an error(i.e Invalid Constructor syntax,name=value pairs can only be used for Sobjects) on this line.

 Account a1=new account(id=c.accountid);

I can rewrite this line as given below..

Account  a1=new Account();
a1.id=c.accountid;

Now,also I am getting an error as"Variable doesnot exist:id".

Kindly support and suggest.