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
keshin okawakeshin okawa 

N>Help with Field Mapping Error

Good day everyone I have some question in salesforce. Suppose i've created a map example:
map<String,String> testVariable =  new map<String,String>  // where the map key is an api key of a certain object field and map value is a possible field value;

Now what I want is to create an object through looping the map.Ex.

Account acctVariable = new Account();
for(String variable1 : testVariable.keyset()){
    acctVariable.variable1 = 'sample data';

But an error say that "Invalid field variable1". What will I do in order for the system to detect that variable1 is a variable and not an object field ?This is just a part of the whole code. Please help .thank you.
Ajay K DubediAjay K Dubedi
The problem here is that you are trying to enter a value as the variable1 field in Account instance. But that can't be done as there is no such field on Account. if you want to insert your map value in an account field, then you can do it like this:
Account acctVariable = new Account();
for(String variable1 : testVariable.keyset()){ = testVariable.get(variable1);

Let me know if this helps.
Hi miczster masher,

As per your code you have consructed a map with key as string and value as string .I am sure how you are constructing the map .
Secondly you are iterating with keyset and creating account and assigning the  value to account field .

As Ajay said is correct you can assign from the amp to Account Name or any other field you need .

But if you will create insnace of account outside the loop then finally it will assign only only last map value to Account .

So try like below .create Account instance inside the loop and add to alist .
List<Account> accList=new List<Account>();
for(String variable1 : testVariable.keyset()){
    Account acctVariable = new Account();
    acctVariable.Name = testVariable.get(variable1);

Let me know if any issue .