0

I have posted this question two times but i'm not getting the solution for that reason i'm again posting this question.

First i have done the reading of two sobject fields dynamically.

The two sObjects are Case and Incident__c.

Dynamically reading fields from objects and stored in below two statements

 Set<String> incidents = new Set<String>();//adding incident fields 
 List<String> SelectedFields = new List<String>()//adding case fields dynamically

I'm trying to read fields dynamically from incidents and SelectedFields reference objects and mapping incident fields to the Case fields dynamically

Note

First field in incidents reference object is map to the first field in SelectedFields  reference object and vice versa.
  • have you tried FieldSets in salesforce – Rohit Mourya Apr 28 '17 at 6:49
  • the field names of incident and case are same? do you want to do something similar to case.first = incident.first ? – Safiya PV Apr 28 '17 at 6:51
  • No field Names are different but but fields values assign from incidents fields to SelectedFields fields . – Srikar Apr 28 '17 at 6:54
  • can you be little more specific ? your requirement is confusing :) – Safiya PV Apr 28 '17 at 6:56
0

The way I understand this is you have a set of incident object's fields and then you have a list of Case object's fields. Now you want to copy values of incident fields to the case record's fields. AND you are saying the first element in the incident set is mapped to the first element of case fields list.

Set is an un-ordered collection i.e if you add 3, 2, 1 in a set, the set will sort the values and will have 1, 2, 3 in it. So you don't define its order.

List is ordered i.e if you add 3, 2, 1 in a list, the list will hold the values as it is and will have 3, 2, 1 in it. So you define its order. I hope you consider this while putting values into the set and the list.

You can have a counter variable to pull this off.

Integer counter = 0;
for(String incidentField : incidents)
{
  String caseField = SelectedFields[counter];
  yourCaseRecord.put(caseField, yourIncidentRecord.get(incidentField));
  counter++;
}

This is one way to do this.

However, your best bet would have been a Map. A Map of incident field name to case field name.

Map<String, String> incidentToCaseFieldMap = new Map<String, String>();

for(String incidentField : incidentToCaseFieldMap.keySet())
{
   yourCaseRecord.put(incidentToCaseFieldMap.get(incidentField), yourIncidentRecord.get(incidentField));
}
0

@santanu Halder thanks for replying could you please modify this line as dynamic with giving the name statically because from input fields are getting dynamically

yourCaseRecord.put(caseField, yourIncidentRecord.get(incidentField));

Thanks

  • I don't know what this means! If you are unable to comment then edit the question with your comments. – Santanu Halder Apr 28 '17 at 9:21
0

You can try for a utility class where you will store the field mapping and pass the List of incident which returns a list of case;

public class IncidentUtilities
{
    //Prepare the field Mapping between Incident => Case: (Key: IncidentField, Value: CaseField)
    private static final Map<String,String> FILED_MAPING = new Map<String,String>{
                'Name' => 'Subject',
                'Field1__c' => 'CaseField1__c',
                'Field2__c' => 'CaseField2__c'
                };

    //Fetch name of all the fields on Case object. 
    //This will help in preventing error if any field is deleted from the target object but the field-mapping is not updated.
    private static Set<String> caseActiveFields = Schema.SObjectType.Case.fields.getMap().keySet();

    public static List<Case> createCasesList(final List<Incident__c> incidentList)    
    {
        List<Case> resultCases = null;

        if(null != incidentList && incidentList.size() > 0)
        {
            resultCases = new List<Case>();

            for(Incident__c i : incidentList)   
            {
                Case c = new Case();
                for(String incidentFieldName : FILED_MAPING.keySet())
                {
                    String caseFieldName = FILED_MAPING.get(incidentFieldName);
                    Object incidentFieldValue = i.get(incidentFieldName);

                    if(null != caseFieldName && caseActiveFields.contains(caseFieldName.toLowerCase()) && null != incidentFieldValue)
                    {
                        c.put(caseFieldName , incidentFieldValue);
                    }
                }
                resultCases.add(c);
            }   

        }

        return resultCases;

    }       
}

Now call this from the service or controller;

List<Incident__c> incidentList = [Select Name,Field1__c,Field2__c from Incident__c limit 10];
List<Case> caseList = IncidentUtilities.createCasesList(incidentList);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.