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
Satish PrajapatSatish Prajapat 

How to insert/update Map<String, String> into Account

I have Map<String, String> m1 = ........;/(Map Contain only one record information)
And I want to update the record into account How can I do this?
 
Best Answer chosen by Satish Prajapat
Satish PrajapatSatish Prajapat
Hello Sagarika Rout,
Your post is helpful if we make some changes:
 
Account acc_Object = new Account();
SObject sobj = acc_Object;
for(String Field_API_Name: m1.keySet())
{
      if(sobj.get(Field_API_Name).isEditable())
      {
            sobj.put(Field_API_Name, m1.get(Field_API_Name));    
      }
}
Account update_Acc = (Account)sobj;
update update_Acc;

But there is a problem to update non-editable fields.
Please help me to update the record...!!!!

All Answers

NagendraNagendra (Salesforce Developers) 
Hi Satish,

Please find the sample code for updating a related record with a Map.
trigger IndustrytoAcctOpp on Customer_Product_Line_Item__c ( before insert, before update )
{
&nbsp;&nbsp;&nbsp; Map<String,String> imap = new Map<String,String>();

&nbsp;&nbsp;&nbsp; for ( Industry_Definition__c i :
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [&nbsp;&nbsp; SELECT&nbsp; Id, Market_Segment__c, Strategic_Industry__c
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FROM&nbsp;&nbsp;&nbsp; Industry_Definition__c
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ]
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; )
&nbsp;&nbsp;&nbsp; {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; imap.put( i.Market_Segment__c, i.Strategic_Industry__c );
&nbsp;&nbsp;&nbsp; }

&nbsp;&nbsp;&nbsp; Map<Id,Customer_Product_Line_Item__c> map_AccountID_CPLI =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; new Map<Id,Customer_Product_Line_Item__c>();

&nbsp;&nbsp;&nbsp; for ( Customer_Product_Line_Item__c cpli : trigger.new )
&nbsp;&nbsp;&nbsp; {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cpli.Strategic_Industry__c = imap.get( cpli.Industry_Segment__c );
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; map_AccountID_CPLI.put( cpli.Account__c, cpli );
&nbsp;&nbsp;&nbsp; }

&nbsp;&nbsp;&nbsp; List<Account> list_AccountsToUpdate = new List<Account>();
&nbsp;&nbsp;&nbsp; for ( Account a :
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [&nbsp;&nbsp; SELECT&nbsp; Id, Strategic_Industry__c
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FROM&nbsp;&nbsp;&nbsp; Account
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WHERE&nbsp;&nbsp; Id IN :map_AccountID_CPLI.keySet()
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ]
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; )
&nbsp;&nbsp;&nbsp; {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a.Strategic_Industry__c = map_AccountID_CPLI.get( a.Id ).Strategic_Industry__c;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; list_AccountsToUpdate.add( a );
&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; update list_AccountsToUpdate;
}
Hope this helps.

Please mark this as solved if it's resolved so that it gets removed from the unanswered queue which results in helping others who are encountering a similar issue.

Thanks,
Nagendra
 
Satish PrajapatSatish Prajapat

Hello Nagendra,
This code is not helping to solve my doubt because second for loop is getting the value from Trigger.New which is sObject Type(Customer_Product_Line_Item__c Object) and in the line number 19
<<<cpli.Strategic_Industry__c = imap.get( cpli.Industry_Segment__c );>>>
cpli.Industry_Segment__c is confusing because the imap contain the only two field which is not it.



So, This is not helpful for me.

 

Satish PrajapatSatish Prajapat
This is sample value of Map<String,String>:
their are more than 50 fields,
Object is Account Object,
Map contain the single record:
{accountnumber=null, accountsource=null, active__c=null, annualrevenue=null, billingaddress=null, billingcity=null, billingcountry=null, billinggeocodeaccuracy=null, billinglatitude=null, billinglongitude=null, ...}
Sagarika RoutSagarika Rout
Hi ,

if it is only one record the how is the MAP structured ? If it carry record ID and record detail then you can use map.keyset() for record ID and map.values() for record details then you can associate the ID to account object and udate it directly 

Map<String,String> map_Account // contains record ID and record details 

Account acc = map_Account.values();
acc.id = map_Account.keyset();
update acc;


Thanks ,
Sagarika
Satish PrajapatSatish Prajapat
Hello Sagarika,
Account acc = map_Account.values();  //Error -->Illegal assignment from List<String> to Account 
acc.id = map_Account.keyset(); //Error --> Illegal assignment from Set<String> to Id

It is not working....
Satish PrajapatSatish Prajapat

Hello All,
Can we do like this?

List<Account> myList = new List<Account>();
Account acc_Object = new Account();
for(String Field_API_Name: m1.keySet())
{
     acc_Object.Field_API_Name = m1.get(Field_API_Name); // Getting error --->>Variable does not exist: Field_API_Name
}

myList.add(acc_Object);
update myList;


Is it possible do this????????

Sagarika RoutSagarika Rout
List<Account> myList = new List<Account>();
Account acc_Object = new Account();
SObject sobj = acc_Object;
for(String Field_API_Name: m1.keySet())
{
     sobj.get(Field_API_Name) = m1.get(Field_API_Name); 
}

Account update_Acc = (Account)sobj;
//As only single record , so no list required .
//myList.add(acc_Object);
update update_Acc;

I think above will work.
martha simonsmartha simons
not work :(
Sagarika RoutSagarika Rout
Sorry i misunderstood your requirement 

if you are getting map<string,string> , then i think you can simply assign the value using get method 

Account acc = new Account();
acc.name = m1.get('name');
acc.Id = m1.get('Id');

but in this case you need to put all field name with case sensitive as per it defined in the map . It maynot recomended but it may solve your problem.
Satish PrajapatSatish Prajapat
Hello Sagarika Rout,
Your post is helpful if we make some changes:
 
Account acc_Object = new Account();
SObject sobj = acc_Object;
for(String Field_API_Name: m1.keySet())
{
      if(sobj.get(Field_API_Name).isEditable())
      {
            sobj.put(Field_API_Name, m1.get(Field_API_Name));    
      }
}
Account update_Acc = (Account)sobj;
update update_Acc;

But there is a problem to update non-editable fields.
Please help me to update the record...!!!!
This was selected as the best answer