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
Anjana Sharma 9Anjana Sharma 9 

I want to take argument as Interger, Map<Interger, Interger> and Insert the new Account Records according to the 1st args value then i want to insert Contacts under the Accounts those are inserted using second arg like if {1=>3} like for acc 1 there 3 con

Best Answer chosen by Anjana Sharma 9
Suraj Tripathi 47Suraj Tripathi 47

Hi,

you can start like this.

//@RestResource(UrlMapping='/UserCreation/')
global class YAauraApex {
    
    public static void data(Integer first,Map<Integer,Integer> mapName){
         
        List<Account> accountList=new List<Account>();
        for(Integer i=0;i<first;i++){
            Account ac=new Account();
            ac.Name='Test'+i;
            accountList.add(ac);
        }
        if(accountList.size()>0){
           insert accountList; 
        }
        List<Contact> contactList=new List<Contact>();
            for(Integer j=0;j<mapName.get(first);j++){
                contact con=new contact();
                con.LastName='data';
                contactList.add(con);
            }
        insert contactList;
            
                }
    
}

Please mark it as the Best Answer if it helps you.

Thank You

All Answers

Suraj Tripathi 47Suraj Tripathi 47

Hi,

why are you using this Map<Interger, Interger>

Your questions are not clear

so please explain with example

are you passing argument from  other method?

Anjana Sharma 9Anjana Sharma 9
Actually i want to pass two argument in my method one is integer and the other one is map<Integer, integer> , then i have to insert records in account by using first argument like i inser 3 records in my account now by the map i want to pass the key as account number and the value as contact ...{1=>3} or {2=>5} just for example i take this when during execution when i pass account = 1 as key then 3 contacts add to 1 account ..same like for others
 
Suraj Tripathi 47Suraj Tripathi 47

Hi,

you can start like this.

//@RestResource(UrlMapping='/UserCreation/')
global class YAauraApex {
    
    public static void data(Integer first,Map<Integer,Integer> mapName){
         
        List<Account> accountList=new List<Account>();
        for(Integer i=0;i<first;i++){
            Account ac=new Account();
            ac.Name='Test'+i;
            accountList.add(ac);
        }
        if(accountList.size()>0){
           insert accountList; 
        }
        List<Contact> contactList=new List<Contact>();
            for(Integer j=0;j<mapName.get(first);j++){
                contact con=new contact();
                con.LastName='data';
                contactList.add(con);
            }
        insert contactList;
            
                }
    
}

Please mark it as the Best Answer if it helps you.

Thank You

This was selected as the best answer
Anjana Sharma 9Anjana Sharma 9
Thanku it works..
Anjana Sharma 9Anjana Sharma 9
Actuallly i am new to salesforce and i cant understand how i do this..i make a custom object of name error log and make 4 custom fields in that namly  1.Parent Record Id  and Parent Record Name of text type and  Error Details of  Type  Long Text Area and the last one is  Parent Record which is a  Formula Field: URL.. 
Now i have to pass two arguments in my method one is list<Account > and other is list<contact> ..now what i am doing in my account list i am populated some record but what i need that like i am populating 10 records then some them are populated with id but some of them not..so as we know that every record has id then how can i poluatated record without id .....then after poplating theste records i have to Update that records where Id is populated and insert the records where Id is not populated. (Use Database.upsert ) ... now at last i have to Check the Database.upsert result and if there is any error with any of the record then it create a new record of Error Log (custom object) for each error.
and in my list of contact i have to hold the new contact records and these contact i have to add to those account which are success...like i have 2 contact in my list<contact> then these 2 contact add to all account record like i have 4 succes account record after database.upsert the 2 contact add to each and at last they became 8....then how can i polate record and cause error by which i save these error to my custom object ..for every new error a new record is added ..
Can you please help me in this