2

I have two lists.Each list return record with some specific lookup field.

lstCurrentRecord :

(Service__c:{Id=a0G4E000000BDJ6UAO, Name=S-61, Displayed_Code_Label__c=TWZ - TWIZY, Car_Set__c=uma, RecordTypeId=0124E0000004OvhQAE, CurrencyIsoCode=EUR})

lstSelectedRecord :

(Service__c:{ Service__c:{Id=a0G4E000000BrzdUAC, Name=S-134, Displayed_Code_Label__c=92J - LODGY, Car_Set__c=ram, RecordTypeId=0124E0000004OvhQAE, CurrencyIsoCode=EUR})

Here am comparing two lists Displayed_Code_Label__c value.If in lstSelectedRecord does not contains lstCurrentRecord Displayed_Code_Label__c then add that record in one list with lstCurrentRecord carset value. For example

lstCurrentRecord -->Displayed_Code_Label__c=TWZ - TWIZY,     Car_Set__c=uma

lstSelectedRecord --> Displayed_Code_Label__c=92J - LODGY, Car_Set__c=ram,

Now i want to insert Service record as

Displayed_Code_Label__c=92J - LODGY, Car_Set__c=uma. The carset name is changed and inserted as a new record. How to implement this ?

Apex:

public void retrieveService() {
    system.debug('Selected Values >>>>' + strCarsetOption + strNWservice + strCBservice + strOtherservice + strContreOption);
    if (strNWservice == 'true') {
        List < Service__c > lstSNWService = [select id, name, Displayed_Code_Label__c, Car_Set__c from Service__c where Service_Type__c = 'Simple product'
            and Simple_service_type__c = 'Network'
            and Car_Set__c =: strCarsetOption
        ];
        List < Service__c > lstCNWService = [select id, name, Displayed_Code_Label__c, Car_Set__c from Service__c where Service_Type__c = 'Simple product'
            and Simple_service_type__c = 'Network'
            and Car_Set__c =: strCurrentRecId
        ];
        lstService = getUniqueRecords(lstCNWService, lstSNWService);
        upsert lstService;
        system.debug('lstNWService >>>>' + lstService);
    }
}

public List < service__c > getUniqueRecords(List < service__c > lstCurrentRecord, List < service__c > lstSelectedRecord) {
    system.debug('lstCurrentRecord<<<<' + lstCurrentRecord);
    system.debug('lstSelectedRecord <<<<' + lstSelectedRecord);
    mapUniqueService = new map < String, List < Service__c >> ();
    List < Service__c > lstServiceAdd = new List < Service__c > ();
    if (lstCurrentRecord.size() > 0) {
        for (Service__c objService: lstCurrentRecord) {
            mapUniqueService.put(objService.Displayed_Code_Label__c, new List < Service__c > {
                objService
            });
        }
    }
    system.debug('<<<<mapUniqueService' + mapUniqueService);
    if (lstSelectedRecord.size() > 0) {
        for (Service__c objService1: lstSelectedRecord) {
            system.debug('objService1<<<' + objService1);
            if (!mapUniqueService.containsKey(objService1.Displayed_Code_Label__c) && mapUniqueService.size() > 0) {
                system.debug('<<<<objService1 check' + objService1 + mapUniqueService);
                objService1.Car_Set__c = strCurrentRecId;
                lstServiceAdd.add(objService1);
            } else if (mapUniqueService.size() == 0) {
                objService1.Car_Set__c = strCurrentRecId;
                lstServiceAdd.add(objService1);
            }
        }
    }
    system.debug('<<<<lstServiceAdd' + lstServiceAdd);
    return lstServiceAdd;
}

In lstServiceAdd i get the values.Here how to update the carset field which is lookup to service.

Thanks in advance !!

1

Like you said in your question.

You have two list

lstCurrentRecord -->Displayed_Code_Label__c=TWZ - TWIZY,     Car_Set__c=uma

lstSelectedRecord --> Displayed_Code_Label__c=92J - LODGY, Car_Set__c=ram,

so you can create a map so that map will contain Displayed_Code_Label__c as key and service as record

map<String, Service__c > mapUniqueService = new map<String, Service__c >();

so you can assign the all records i.e. lstCurrentRecord to this map

now check lstSelectedRecord list contain existing Displayed_Code_Label__c values if not then clone that record and change the Car_Set__c value for newly record

List<Service__c> lstServiceAdd = new List<Service__c>();
for(Service__c sObjService: lstSelectedRecord ) { 
    if(!mapUniqueService.containsKey(sObjService.Displayed_Code_Label__c ) ) { 
        Service__c serv = sObjService.clone(); 
        serv.Car_Set__c = strCurrentRecId; 
        lstServiceAdd.add(serv); 
    }
}
insert lstServiceAdd;
1

Well, you have two lists of same object. For field values in list A you want to search field value from list B element, if it's not exist you want to store element from list B. If I'm right, you should use Set collection instead of Map:

public List <service__c> getUniqueRecords
    (List <service__c> lstCurrentRecord, List <service__c> lstSelectedRecord){

//populate set with unique displayed codes
//string.isblank tells that value is empty or null
set<String> codes = new set<string>();
for (Service__c objService: lstCurrentRecord) {
    if (!string.isblank(objService.Displayed_Code_Label__c)) {
        codes.add(objService.Displayed_Code_Label__c);
    }
}

//for each service in lstSelectedRecord, check if its code is presented in codes
list <Service__c> servicesToUpdate = new list<>(Service__c);

for (Service__c objService: lstSelectedRecord) {
    if (!codes.contains(objService.Displayed_Code_Label__c)){
        //if not presented, update service
        objService.Car_Set__c = strCurrentRecId;
        servicesToUpdate.add(objService);
    }
}

//you can update your records here
//update servicesToUpdate;
return servicesToUpdate;
}

Please become familiar with Collections

  • Can you summarize your understanding of OP question in a sentence or two? It's tough to follow on its own, and so it is difficult to ascertain how your answer relates. – Adrian Larson May 10 '16 at 22:55
  • @adrian-larson, please find a moment to check my summary – Ilya Lepesh May 11 '16 at 3:11
  • A little tough for me to follow, but that's the right direction! – Adrian Larson May 11 '16 at 3:13

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.