• Dominik Pataky
  • NEWBIE
  • 0 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 1
    Replies
Hello,
I have a List of custom SObjects that need to be bulk inserted or updated, but the logs do not show any useful information, why the Apex job does not finish. The job stays at "processing" - even over night! This morning I got one result back, First error: Insert failed. First exception on row 0; first error: UNABLE_TO_LOCK_ROW, unable to obtain exclusive access to this record or 23 records and solved it with using FOR UPDATE in my SOQL queries. But now I am back to no result.
  • Any idea how I can change the debug log levels when I run the class from the dev console?
  • Should I look into any other blocking operations I would need to solve? (Triggers?)
Thanks!
Hello everyone,
after I posted this wrongly on https://success.salesforce.com/answers?id=9063A000000p6oO I now repost it here and still seek advice!

We are trying to sync some JSON objects (that came from a parser) with our database. There's a list of these objects that is iterated over which has an identifier that is mapped with Customer_Rating objects (via two other objects) from another list. That way, the sync should distinguish between "update an existing object in the database with new values" or "create a new database object with all values".

Firstly we collect all existing Customer_Ratings:
List<Customer_Rating__c> existingRatingList = new List<Customer_Rating__c>(); 
existingRatingList = [SELECT Creation_Date__c, Reply__c, Opportunity__c FROM Customer_Rating__c];
The following list is used to create a new mapping between Opportunity identifiers with the objects above:
Map<String, Customer_Rating__c> existingRatingOppMap = new Map<String, Customer_Rating__c>(); 
for (Customer_Rating__c rating : existingRatingList) { 
    existingRatingOppMap.put(rating.Opportunity__c, rating); 
}
And finally, there's another mapping from Invoice_Number to Opportunities:
Map<Id, Invoice__c> invoiceMap = new Map<Id, Invoice__c>([SELECT id, name, Invoice_Number__c, Opportunity__c FROM Invoice__c]); 
Map<String, String> invoiceOppMap = new Map<String, String>(); 
for (Invoice__c invoice : invoiceMap.values()) { 
    invoiceOppMap.put(invoice.Invoice_Number__c, invoice.Opportunity__c); 
}
Using these three code blocks, we have a link between the Invoice_Number and a possibly existing rating.

Now we iterate over a list of objects that create a link to Invoices via the Invoice_Number and in this block the code should make the difference mentioned above:
if (invoiceOppMap.keySet().contains(invoice_num)) {
    String oppId = invoiceOppMap.get(invoice_num);
    Customer_Rating__c rating;
               
    if (existingRatingOppMap.keySet().contains(oppId)) {
         // Case: an Opportunity with rating with this order_id already exists, so an update is needed.
         rating = existingRatingOppMap.remove(oppId);
         rating.Creation_Date__c = feedback.getSubmitted();      
         update rating;
     } else {
         // Case: no Opportunity (via the invoice referenced by the order_id)
         // with an existing rating was found, a new one is created.
         rating = new Customer_Rating__c();
         rating.Comment__c = feedback.review;
         rating.Opportunity__c = oppId;
         rating.Rating__c = feedback.rating;
         rating.Creation_Date__c = feedback.getSubmitted();
                    
         insert rating;
     }
}
Now the blocker we tried to solve: when running this whole function via the Apex anonymous execution window it starts, holds at "Unpacking results" and does not finish. The log shows, that both - inserts and updates - were done, but they are not commited to the database.
If we then remove the update path and run only the inserts it works as expected.
We also looked at the activated triggers, but they are all activated in the insert path. Update does not fire any triggers.

Any ideas? Any race conditions we overlooked? 
Tips to create better queries are also very welcome!
Hello everyone,
after I posted this wrongly on https://success.salesforce.com/answers?id=9063A000000p6oO I now repost it here and still seek advice!

We are trying to sync some JSON objects (that came from a parser) with our database. There's a list of these objects that is iterated over which has an identifier that is mapped with Customer_Rating objects (via two other objects) from another list. That way, the sync should distinguish between "update an existing object in the database with new values" or "create a new database object with all values".

Firstly we collect all existing Customer_Ratings:
List<Customer_Rating__c> existingRatingList = new List<Customer_Rating__c>(); 
existingRatingList = [SELECT Creation_Date__c, Reply__c, Opportunity__c FROM Customer_Rating__c];
The following list is used to create a new mapping between Opportunity identifiers with the objects above:
Map<String, Customer_Rating__c> existingRatingOppMap = new Map<String, Customer_Rating__c>(); 
for (Customer_Rating__c rating : existingRatingList) { 
    existingRatingOppMap.put(rating.Opportunity__c, rating); 
}
And finally, there's another mapping from Invoice_Number to Opportunities:
Map<Id, Invoice__c> invoiceMap = new Map<Id, Invoice__c>([SELECT id, name, Invoice_Number__c, Opportunity__c FROM Invoice__c]); 
Map<String, String> invoiceOppMap = new Map<String, String>(); 
for (Invoice__c invoice : invoiceMap.values()) { 
    invoiceOppMap.put(invoice.Invoice_Number__c, invoice.Opportunity__c); 
}
Using these three code blocks, we have a link between the Invoice_Number and a possibly existing rating.

Now we iterate over a list of objects that create a link to Invoices via the Invoice_Number and in this block the code should make the difference mentioned above:
if (invoiceOppMap.keySet().contains(invoice_num)) {
    String oppId = invoiceOppMap.get(invoice_num);
    Customer_Rating__c rating;
               
    if (existingRatingOppMap.keySet().contains(oppId)) {
         // Case: an Opportunity with rating with this order_id already exists, so an update is needed.
         rating = existingRatingOppMap.remove(oppId);
         rating.Creation_Date__c = feedback.getSubmitted();      
         update rating;
     } else {
         // Case: no Opportunity (via the invoice referenced by the order_id)
         // with an existing rating was found, a new one is created.
         rating = new Customer_Rating__c();
         rating.Comment__c = feedback.review;
         rating.Opportunity__c = oppId;
         rating.Rating__c = feedback.rating;
         rating.Creation_Date__c = feedback.getSubmitted();
                    
         insert rating;
     }
}
Now the blocker we tried to solve: when running this whole function via the Apex anonymous execution window it starts, holds at "Unpacking results" and does not finish. The log shows, that both - inserts and updates - were done, but they are not commited to the database.
If we then remove the update path and run only the inserts it works as expected.
We also looked at the activated triggers, but they are all activated in the insert path. Update does not fire any triggers.

Any ideas? Any race conditions we overlooked? 
Tips to create better queries are also very welcome!
Hello everyone,
after I posted this wrongly on https://success.salesforce.com/answers?id=9063A000000p6oO I now repost it here and still seek advice!

We are trying to sync some JSON objects (that came from a parser) with our database. There's a list of these objects that is iterated over which has an identifier that is mapped with Customer_Rating objects (via two other objects) from another list. That way, the sync should distinguish between "update an existing object in the database with new values" or "create a new database object with all values".

Firstly we collect all existing Customer_Ratings:
List<Customer_Rating__c> existingRatingList = new List<Customer_Rating__c>(); 
existingRatingList = [SELECT Creation_Date__c, Reply__c, Opportunity__c FROM Customer_Rating__c];
The following list is used to create a new mapping between Opportunity identifiers with the objects above:
Map<String, Customer_Rating__c> existingRatingOppMap = new Map<String, Customer_Rating__c>(); 
for (Customer_Rating__c rating : existingRatingList) { 
    existingRatingOppMap.put(rating.Opportunity__c, rating); 
}
And finally, there's another mapping from Invoice_Number to Opportunities:
Map<Id, Invoice__c> invoiceMap = new Map<Id, Invoice__c>([SELECT id, name, Invoice_Number__c, Opportunity__c FROM Invoice__c]); 
Map<String, String> invoiceOppMap = new Map<String, String>(); 
for (Invoice__c invoice : invoiceMap.values()) { 
    invoiceOppMap.put(invoice.Invoice_Number__c, invoice.Opportunity__c); 
}
Using these three code blocks, we have a link between the Invoice_Number and a possibly existing rating.

Now we iterate over a list of objects that create a link to Invoices via the Invoice_Number and in this block the code should make the difference mentioned above:
if (invoiceOppMap.keySet().contains(invoice_num)) {
    String oppId = invoiceOppMap.get(invoice_num);
    Customer_Rating__c rating;
               
    if (existingRatingOppMap.keySet().contains(oppId)) {
         // Case: an Opportunity with rating with this order_id already exists, so an update is needed.
         rating = existingRatingOppMap.remove(oppId);
         rating.Creation_Date__c = feedback.getSubmitted();      
         update rating;
     } else {
         // Case: no Opportunity (via the invoice referenced by the order_id)
         // with an existing rating was found, a new one is created.
         rating = new Customer_Rating__c();
         rating.Comment__c = feedback.review;
         rating.Opportunity__c = oppId;
         rating.Rating__c = feedback.rating;
         rating.Creation_Date__c = feedback.getSubmitted();
                    
         insert rating;
     }
}
Now the blocker we tried to solve: when running this whole function via the Apex anonymous execution window it starts, holds at "Unpacking results" and does not finish. The log shows, that both - inserts and updates - were done, but they are not commited to the database.
If we then remove the update path and run only the inserts it works as expected.
We also looked at the activated triggers, but they are all activated in the insert path. Update does not fire any triggers.

Any ideas? Any race conditions we overlooked? 
Tips to create better queries are also very welcome!