You need to sign in to do that
Don't have an account?
uHaveOptions
Primary Contact ID to populate in Account Object Primary_Contact__c lookup field Apex Trigger
Using this Apex Trigger, how can I add or show the primary contact in the Account object. I know it can be shown using the contact related list but I would like it to show on the detail page instead as a lookup so users can just click on the contact link instead of looking for the primary contact in the related list.
Here's the code
Here's the code
trigger PrimaryContact on Contact (before insert, before update) { set<id> getid = new set<id>(); string contactId; List<Contact> conList = new List<Contact>(); // Trigger Functionality if(Trigger.isInsert || Trigger.isUpdate) { for(Contact cont: Trigger.New) { if(cont.Primary_Contact__c == true) { getid.add(cont.AccountId); contactId = cont.id; } } } // Fetching the other Contact which has primary contact checked List<contact> cList = [select id, Primary_Contact__c from contact where accountid IN: getid AND Primary_Contact__c = true]; // Unchecking the already checked primary contact if(cList.size() > 0) { for(Contact newClst: cList) { if(newClst.id != contactId) { newClst.Primary_Contact__c = false; conList .add(newClst); } } } update conList; }Thanks in advance
Try below code: This should work!
All Answers
Trigger Code:
Here how it looks,
Now, if i try to Select primary conatct checkbox for Josh Davis then it would throw an error mesage. Here we go,
Hope it helps! If it solves the query then kindly mark it Best Answer!
Thanks Govind!
I tried adding email and phone. How can I achieve that?
I added
But it's giving an error of invalid data
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger PrimaryContact caused an unexpected exception, contact your administrator: PrimaryContact: execution of BeforeUpdate caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Contact.Phone: ()
Can't it just be a lookup value? So I can build and add what ever field I need?
I'm going to try to build the test class as well.
Thanks again.
Try this:
Hope this helps!
I updated your code with the addition of the...
I got an error...Error: Compile Error:
Primary_Contact_First_and_Last_Name__c, act.Primary_Contact_Email__c, act.Primary_Contact_Phone__c
^
ERROR at Row:1:Column:75
Didn't understand relationship 'act' in field path. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names. at line 58 column 23
So I updated and removed "act." on act.Primary_Contact_Email__c, act.Primary_Contact_Phone__c,
That passed but updating a record gives me an error.
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger PrimaryContact caused an unexpected exception, contact your administrator: PrimaryContact: execution of BeforeUpdate caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Contact.Email: ()
Try below code: This should work!