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
ShayKhanShayKhan 

Hello All, New to the Salesforce development community and have a requirement that i need some help with!

Im fairly new to Apex, so this question may seem mediocre but i've really just hit a wall at this point.
Below is the requirement:
Insert 2 accounts with type = 'Prospect'
Insert 2 contacts  - 1 contact Lead source = 'Phone Enquiry' - Relate to 1 account
                            - 2 Contact Lead Sorce ='Partner Referal' - Relate to 2 account 
 CHeck if(Contact Lead source = Phone Enquiry)
 Related Account type = "Installation Partner";

Below is the what i have so far:
public class AccountnContactClass{

//Create 2 account and 2 Contacts using List and Limiting the Insert DML statements
         
    
    public void AccountContactCreation(){
    List<Account> AccList = new list <Account>();
    Account ac8 = New Account();
    ac8.Name = 'Apples';
    ac8.type = 'prospect';

    Account ac9 = New Account();
    ac9.Name = 'Oranges';
    ac9.type = 'prospect';
    
    AccList.add(ac8);
    AccList.add(ac9);
    
    Insert AccList;
    
    List<contact> ContactList = new list <contact>();
    Contact Con1 = New Contact();
    Con1.FirstName = 'Tomatoes';
    Con1.LastName = 'Rotten';
    Con1.LeadSource = 'Phone Inquiry';
    Con1.accountid = Acclist[0].id;
    
    Contact Con2 = New Contact();
    Con2.FirstName = 'G2';
    Con2.LastName = 'Gatorade';
    Con2.LeadSource = 'Partner Referral';
    //Con2.accoundid = AccList[1].id;
    
    ContactList.add(Con1);
    ContactList.add(Con2);
    
    Insert ContactList;
    
}

}

 
Best Answer chosen by ShayKhan
sateesh m 2sateesh m 2
Refer this hope it helps!
Account a=new Account(Name='po');
insert a;
Contact c=new Contact(Lastname='op');
c.accountid=a.id;
insert c;
Contact d=[select Account.name from Contact where accountid=:a.id Limit 1];
d.account.type='Installation Partner';
update d.account;

All Answers

sateesh m 2sateesh m 2
It should work! Do you have any doubt mention clearly.
Shruti SShruti S
Please let me know what exactly you want to do. Do you want to update the related account's type to "Installation Partner"?
ShayKhanShayKhan
Thanks for the prompt response both of you! Yes Shruti, the related account type should be updated to "installation partner" I know I have to use the "UPDATE" SOQL syntax but I am still learning how to understand how to format the query within the code. 

Sateesh, the code works fine upto inserting both the account and contact list it's the last part of the requirement I am stuck on. I just need to see how UPDATE works As the examples in the documentations were a bit confusing for me.

 
sateesh m 2sateesh m 2
Refer this hope it helps!
Account a=new Account(Name='po');
insert a;
Contact c=new Contact(Lastname='op');
c.accountid=a.id;
insert c;
Contact d=[select Account.name from Contact where accountid=:a.id Limit 1];
d.account.type='Installation Partner';
update d.account;
This was selected as the best answer
ShayKhanShayKhan
Sateesh! 
I was able to fulfill the requirement using the example you provided A++++ for you!