You need to sign in to do that
Don't have an account?
Keerthigee
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.
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.
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.
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.
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
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.