You need to sign in to do that
Don't have an account?
lp oklll
Populating Contact on case which is related to account.
Hi,
I have 3 account types A B C
When on case account A is chosen B and C populate automatically. The trigger seems to work for that.
The problem is arising when I am trying to populate a contact related to account A
I simply do not know how to traverse the relationship.
This is my code
Trigger PopulateResellerCase on Case (before insert,before update) { for (Case NewCase : Trigger.new) If (NewCase.Accountid != null) { Account AccountReseller = [SELECT Id,Reseller__c,APA__c,(SELECT id,Contact.FirstName FROM Account.Contacts)FROM Account WHERE Id = :NewCase.Accountid ]; NewCase.APA__c = AccountReseller.APA__c;
NewCase.Reseller__c = AccountReseller.Reseller__c;
} }
I am administrator on the path of learning code. I have tried the following options below but always get an error.
NewCase.contact = AccountReseller.contact.id;
NewCase.contact = AccountReseller.account.contactid;
But nothing seems to work.
I have 3 account types A B C
When on case account A is chosen B and C populate automatically. The trigger seems to work for that.
The problem is arising when I am trying to populate a contact related to account A
I simply do not know how to traverse the relationship.
This is my code
Trigger PopulateResellerCase on Case (before insert,before update) { for (Case NewCase : Trigger.new) If (NewCase.Accountid != null) { Account AccountReseller = [SELECT Id,Reseller__c,APA__c,(SELECT id,Contact.FirstName FROM Account.Contacts)FROM Account WHERE Id = :NewCase.Accountid ]; NewCase.APA__c = AccountReseller.APA__c;
NewCase.Reseller__c = AccountReseller.Reseller__c;
} }
I am administrator on the path of learning code. I have tried the following options below but always get an error.
NewCase.contact = AccountReseller.contact.id;
NewCase.contact = AccountReseller.account.contactid;
But nothing seems to work.
Regards.
Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
All Answers
Keep in mind that Account/Contact is an one to many relationship which means that you when you query (SELECT id,Contact.FirstName FROM Account.Contacts) it will return a list with zero, one or more contacts depending of how many are associated with that Account.
First of all, you have to understand the scenario and decides which Contact are going to associate with the Case? Can be anyone or you want to associate the first Contact created or do you want to get a Contact based if some criteria?
Regards.
Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
I will always have only one contact.
regards
lawrence
Regards.
Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
Hello,
In addition, have a look on this article: https://developer.salesforce.com/page/Apex_Code_Best_Practices (https://developer.salesforce.com/page/Apex_Code_Best_Practices" target="_blank)
Practice the use of Map, List and Sets and it will be easier to know how to resolve and manage relationships.
Regards.
Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
Its perfect.
Please add this condition to prevent from getting an error when the Account has no Contacts: