I want to upsert some record, and the data stored in array of strings.. for example, the array below has 2 ID, which mean there`s only 2 data that will be updated.. how do I loop the array, so I can upsert the record with the right value.. ?

enter image description here

enter image description here

enter image description here

  • Is the object's External Id that UUID that's prefixing the other data? How are the AVAILABLE and OFFLINE and so on values mapped to the object? – David Reed Oct 21 '18 at 13:57
  • theres an external_id on the custom object.. the external id = userId (first 36 char if you looking to that array).. and theres custom field for each status (away,meal,meeting, and so on) on the custom object.. I want to insert the number value of each status (the number at the end of each array) to the status custom field.. – Arief Gunawan Tjiptorahardja Oct 21 '18 at 14:03
  • my bad @crmprogdev I deleted the post without reading the notification 1st, sorry.. I appreciate your effort.. – Arief Gunawan Tjiptorahardja Oct 21 '18 at 14:09
  • OK, so what have you tried to get this data mapped? Have you looked at String methods like split() and left() to break out the individual values? What kind of data structure might best suit a Mapping of an Id value to an object? – David Reed Oct 21 '18 at 14:11

Iterate strings like that :

 Map<string, CustomObject__c> customObjectByExternalId = new Map<string, CustomObject__c>();
for(string record: records)
     CustomObject__c customObject;
     List<string> data = record.split('_');
        customObject = customObjectByExternalId.get(data[0]);
        customObject = new CustomObject__c();
        customObject.ExternalId__c = data[0];
     customObject.put(data[1]+'_sum__c', decimal.valueof(integer.valueof(data[2])/1000)); 
     customObjectByExternalId.put(data[0], customObject); 
upsert customObjectByExternalId.values();
| improve this answer | |
  • Thanks Pragati, but the result only showed 1 Id (the second Id) .. USER_DEBUG PureCloud__c:{External_ID__c=ab24ddb8-f1ef-4cca-ae72-9f20f5975332, Available_Sum__c=204393, Away_Sum__c=0, Meeting_Sum__c=31, Training_Sum__c=7, Busy_Sum__c=1} .. – Arief Gunawan Tjiptorahardja Oct 21 '18 at 14:36
  • I updated the solution. Please check now. – Pragati Jain Oct 21 '18 at 14:38
  • @AriefGunawanTjiptorahardja above solution resolved your problem? – Pragati Jain Oct 21 '18 at 15:22
  • I tried this, customObject.put(data[1]+'_sum__c', decimal.valueof(integer.valueof(data[2])/1000)); But this code below always giving me error Invalid field OFFLINE for PureCloud__c customObjectByExternalId.put(data[0], customObject); There`s Offline_Sum__c field on the custom object.. – Arief Gunawan Tjiptorahardja Oct 21 '18 at 15:25
  • make sure you updated the above code customObject.put(data[1]+'_sum__c', decimal.valueof(integer.valueof(data[2])/1000)); in both places (If, 'else) – Pragati Jain Oct 21 '18 at 15:31

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.