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 

Check all Account who's Type = 'Prospect' than update these account Fax Field.

Hello All,
I have a requirement that i seem to have issues with. 
The requirement is to check all accounts that has the account type as 'Prospect' and update these accounts Fax field to '9999999999'

Below is the code i have formulated but it is getting Syntax errors. 
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.accountid = AccList[1].id;
    
    ContactList.add(Con1);
    ContactList.add(Con2);
    
    Insert ContactList;
    
    Contact d=[select Account.name from Contact where accountid=:Acclist[0].id Limit 1];
    d.account.type='Installation Partner';
    update d.account;
    

//This is the code for updating the fax field for account type prospect

List<account> A = [select name, type, fax from Account where type = 'prospect'];
A.account.Fax= '9999999999';
update A.account;
    
    
    
    }
    
    }

 
Best Answer chosen by ShayKhan
Raj VakatiRaj Vakati
Use this code . You are Updating the List wrongly  . 
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.accountid = AccList[1].id;
    
    ContactList.add(Con1);
    ContactList.add(Con2);
    
    Insert ContactList;
    
    Contact d=[select Account.name from Contact where accountid=:Acclist[0].id Limit 1];
    d.account.type='Installation Partner';
    update d.account;
    

//This is the code for updating the fax field for account type prospect

List<account> A = [select name, type, fax from Account where type = 'prospect'];
for(Account a1 :A){
A1.Fax= '9999999999';
}
update A;
    
    
    
    }
    
    }

 

All Answers

Raj VakatiRaj Vakati
Use this code . You are Updating the List wrongly  . 
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.accountid = AccList[1].id;
    
    ContactList.add(Con1);
    ContactList.add(Con2);
    
    Insert ContactList;
    
    Contact d=[select Account.name from Contact where accountid=:Acclist[0].id Limit 1];
    d.account.type='Installation Partner';
    update d.account;
    

//This is the code for updating the fax field for account type prospect

List<account> A = [select name, type, fax from Account where type = 'prospect'];
for(Account a1 :A){
A1.Fax= '9999999999';
}
update A;
    
    
    
    }
    
    }

 
This was selected as the best answer
ShayKhanShayKhan
@rajamohan, i'm fairly new to development and just trying out some use cases for practice purposes. With that said your solution worked like a charm!! 
Thanks alot!
tulasiram chtulasiram ch
Hi, i have one doubt here...
Contact d=[select Account.name from Contact where accountid=:Acclist[0].id Limit 1];
d.account.type='Installation Partner'; update d.account;

Why you guys using above two lines. If you update account.type ='Installation partner'
then how can you update a fax field based on Type = 'Prospect';
account type field is changed to installation Partner right.
I am just asking...Can you clear me the issue