+ Start a Discussion
SF_MonkeySF_Monkey 

assign list of string to name of list of an object

I am trying to insert multiple records with assigned list of names and accountid lookup. The list of string contains the name of the record then assign each of those records with accountID. I guess that I have to use a for loop; however, I am unable to execute this. Any help would be appreciated thanks!
@AuraEnabled
    public static void stackup(id accountid, List<String> itemsValue){
        List<Acap_Checklist__c> nList = new List<Acap_Checklist__c>();

        for(Acap_Checklist__c n : nList){
            n.account__c = accountid;

            for(String c : itemsValue){
                n.Name = c;
                }
            
            nList.add(n);
             }
        insert nList;
    }
Best Answer chosen by SF_Monkey
Raj VakatiRaj Vakati
Here is the correct code
 
@AuraEnabled
    public static void stackup(id accountid, List<String> itemsValue){
        List<Acap_Checklist__c> nList = new List<Acap_Checklist__c>();

        for(String n : itemsValue){
			Acap_Checklist__c c = new Acap_Checklist__c();
            c.account__c = accountid;
			c.name = n ; 
            
            nList.add(c);
             }
        insert nList;
    }

 

All Answers

Raj VakatiRaj Vakati
Here is the correct code
 
@AuraEnabled
    public static void stackup(id accountid, List<String> itemsValue){
        List<Acap_Checklist__c> nList = new List<Acap_Checklist__c>();

        for(String n : itemsValue){
			Acap_Checklist__c c = new Acap_Checklist__c();
            c.account__c = accountid;
			c.name = n ; 
            
            nList.add(c);
             }
        insert nList;
    }

 
This was selected as the best answer
SF_MonkeySF_Monkey
Thank you so much Raj. It worked perfectly. I am getting DML exception because accountId is coming in blank; otherwise, it is doing what it is suppose to. Cheers!
Raj VakatiRaj Vakati
Change like this 
 
@AuraEnabled
    public static void stackup(id accountid, List<String> itemsValue){
        List<Acap_Checklist__c> nList = new List<Acap_Checklist__c>();

        for(String n : itemsValue){
			if(s!=null){
			Acap_Checklist__c c = new Acap_Checklist__c();
            c.account__c = accountid;
			c.name = n ; 
            
            nList.add(c);
			}
             }
        insert nList;
    }

 
SF_MonkeySF_Monkey
You didnt declare s variable. Should it be accountID?
Raj VakatiRaj Vakati
My  bad ..its N use this 
 
@AuraEnabled
    public static void stackup(id accountid, List<String> itemsValue){
        List<Acap_Checklist__c> nList = new List<Acap_Checklist__c>();

        for(String n : itemsValue){
			if( n !=null){
			Acap_Checklist__c c = new Acap_Checklist__c();
            c.account__c = accountid;
			c.name = n ; 
            
            nList.add(c);
			}
             }
        insert nList;
    }