Skip to main content

#DML0 discutindo

There appears to be a difference in how child record deletions behave when they're deleted manually as opposed to when they're deleted by a trigger or a flow. My particular use case has a parent object with one or more children in a required lookup from the child to the parent. Since the field requires a value, you can't delete the parent until the children are deleted.

 

When I delete the child record(s) manually they're sent to the Recycle Bin as expected. But a before delete trigger on the parent that deletes the child/children to remove the dependency (or a flow that does the same thing for users with a lower privilege level) nukes the child record(s) out of existence with no way to recover them.

 

I don't expect this to present a huge problem, but I haven't found any documentation or search results in the Salesforce diaspora that explain this difference in behavior.

 

My best guess is that when you delete the record manually, it's using Database.delete rather than a DML delete operation, but why that would make a difference is a mystery to me. Especially since the parent record is soft deleted -- by the flow.

#Apex #DML #Flow

4 respostas
  1. 8 de dez. de 2023, 17:51

     Found the answer while looking for something else.

     

    https://help.salesforce.com/s/articleView?id=000384460&type=1

     

    Under Related Information at the bottom of the page, we find:

    1. If a child record (like a Contact or Opportunity) is deleted and the parent record is subsequently deleted (Like the Account), the child record is permanently deleted. Even if the parent record is undeleted, the child record cannot be recovered, but if the child record is undeleted first this won’t happen.

     

    Because of the relationship between the parent and child in my use case, the child record must be deleted first, and it is not possible to undelete the child first for the same reason. So this is expected (if inconvenient) behavior, and in this context unavoidable as well.

0/9000

I've created a custom external id field in leads i'm using that field to upsert leads coming from external sources

But I've a scenario i need to import leads from csv but when i try to import leads using data import wizard and i cannot see my external id custom field in "match by" field

 

Import leads based on external id using data import wizard

 

How to overcome this or do we need to use data loader ?

 

#Sales Cloud #Salesforce Developer #DML #Lead Assignment Rules

1 resposta
  1. Smitha Thomas (CIBC) Forum Ambassador
    7 de nov. de 2023, 16:06
0/9000

I'm getting lead from external system into salesforce using rest api and i marked a custom text field as extenalId. I'm upserting lead records based on the externalId the problem i'm facing is when any lead field value is null from external system and it's overwriting the salesforce value due upsert operation

How can avoid this or say upsert the fields only whose value is not null ?

 

#Integration #DML #Sales Cloud #Apex #Flows

1 resposta
  1. 21 de out. de 2023, 15:12

    trigger LeadUpsertTrigger on Lead (before insert, before update) {

        

        Map<String, Lead> externalIdToLeadMap = new Map<String, Lead>();

     

        for (Lead lead : Trigger.new) {

          

            if (lead.External_Field__c != null) {

               

                externalIdToLeadMap.put(lead.External_Field__c, lead);

            }

        }

     

        List<Lead> existingLeads = [SELECT Id, External_Field__c, Custom_Field__c FROM Lead WHERE External_Field__c IN :externalIdToLeadMap.keySet()];

     

        for (Lead existingLead : existingLeads) {

            Lead newLead = externalIdToLeadMap.get(existingLead.External_Field__c);

            if (newLead != null && existingLead.Custom_Field__c != newLead.External_Field__c) {

               

                existingLead.Custom_Field__c = newLead.External_Field__c;

            }

        }

    }

0/9000