+ Start a Discussion
RamyaKrishnaRamyaKrishna 

Reg: Upsert the records through map values

Constructing a Dynamic VF page with Custom Object Fields based on Row Number and Col Number:

1. Created a custom object with the fields,
    FieldName__c, ObjectName__c,RowNumber__c,ColNumber__c.
  In FieldName__c and ObjectName__c, add the field api name and Object api name of another custom object.
To build the dynamic VF page, i used the following scenario.
Step1:
Here i prepared 2 maps to the following.
Map1: In Map1 i got all the field api names & object api names, whose contains the colNumber__c value '1'
Map2: In Map2 i got all the field api names & object api names, whose contains the colNumber__c value '2'
Step2:
I could display dynamically all the fields using these 2 maps, from the custom object on a VF page.
(Till the designing part completed)
Step3:
There is a problem while working with Save functionality.
(Previous, I build Dynamic VF page with only one Map. At that time i did not face this problem. All records are upserting very fine)
But in this scenario i used 2 maps, so i am getting a problem while upserting the records.
Map1 Returns:
system.debug('*****map1*****'+lstDynamicWrapper1);
result:
******map1****{Product_Attribute__c=DynaPageController.DynamicWrapper:[fields=(ProductName__c, Type__c), objectname=Product_Attribute__c, record=nara__Product_Attribute__c:{}]}
Map2 Returns:
system.debug('****map2*****'+lstDynamicWrapper2);
result:
******map2*****{Product_Attribute__c=DynaPageController.DynamicWrapper:[fields=(Color__c, Size__c), objectname=Product_Attribute__c, record=nara__Product_Attribute__c:{}]}
Save:-
public pageReference save() 
       { 
                       for(DynamicWrapper item:lstDynamicWrapper1.values())
                        {   
                            upsert item.record;
                        }
                        for(DynamicWrapper item:lstDynamicWrapper2.values())
                        {   
                            upsert item.record;
                        }
                        return null;
        }
When i wrote the above code in save(). It creates 2 records.
first record with map1 values and second record with map2 values.
But my requirement is, i need to create only one record with these 2 map values.
i.e i need to get these 2 map values at a time with in a single for loop.
(I used list<sobject> but it is also creating 2 records)
can you please suggest me.
Thanks,
RKrish

 

Best Answer chosen by Admin (Salesforce Developers) 
RamyaKrishnaRamyaKrishna

Here, map is a type of,

public map<string,DynamicWrapper> lstDynamicWrapper1 { get; set; }

public map<string,DynamicWrapper> lstDynamicWrapper2 { get; set; }
thanks,
RKrishna

 

All Answers

RamyaKrishnaRamyaKrishna

Here, map is a type of,

public map<string,DynamicWrapper> lstDynamicWrapper1 { get; set; }

public map<string,DynamicWrapper> lstDynamicWrapper2 { get; set; }
thanks,
RKrishna

 

This was selected as the best answer
Ritesh AswaneyRitesh Aswaney

If you're looking for the values in your two separate maps to match, then you need to specify at least a common external id in both, else there will be no way for upsert to de-dupe.

Ritesh AswaneyRitesh Aswaney

as an aside, you're executing dml statements in a for loop, which has the potential for exhausting governor limits for max dml operations. dml / soql should be aggregated in a for loop and executed outside it.