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 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 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 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 at 14:11
up vote 1 down vote accepted

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('_');
     if(customObjectByExternalId.containsKey(data[0]))
     {
        customObject = customObjectByExternalId.get(data[0]);
     }
     else
     {
        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();
  • 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 at 14:36
  • I updated the solution. Please check now. – Pragati Jain Oct 21 at 14:38
  • @AriefGunawanTjiptorahardja above solution resolved your problem? – Pragati Jain Oct 21 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 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 at 15:31

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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