Skip to main content
Srikanth Banoth (IS) a posé une question dans #Integration

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 réponse
  1. 21 oct. 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