Update existing Contact from Salesforce Pre-Chat Form

Update existing Contact from Salesforce Pre-Chat Form

If the Salesforce Pre-Chat code finds the existing contact and if you want to update the existing contact with the latest details from the Pre-Chat, we can make use of a Lightning Web Component on the Chat Transcript Lightning Record Page. In the Lightning Web Component, we can use getRecord, getFieldValue and updateRecord.

We can totally avoid apex if we use getRecord, getFieldValue and updateRecord in the Lightning Web Component.

Please check the following sample Lightning Web Component. When the Chat finds the existing Contact from the Pre-Chat details, it will update it with the latest details only if there are any changes found.

Sample Lightning Web Component:

HTML:

<template>   
</template>

JavaScript:

import { LightningElement, api, wire } from 'lwc';
import { getRecord, getFieldValue, updateRecord } from 'lightning/uiRecordApi';
import { RefreshEvent } from "lightning/refresh";
import CHAT_CONTACT_PHONE from "@salesforce/schema/LiveChatTranscript.Contact.Phone";
import CHAT_PHONE from "@salesforce/schema/LiveChatTranscript.Phone__c";
import CHAT_CONTACT_LANGUAGE from "@salesforce/schema/LiveChatTranscript.Contact.Language__c";
import CHAT_LANGUAGE from "@salesforce/schema/LiveChatTranscript.Language__c";
import CONTACTID_FIELD from "@salesforce/schema/LiveChatTranscript.ContactId";
import ID_FIELD from "@salesforce/schema/Contact.Id";
import CONTACT_LANGUAGE from "@salesforce/schema/Contact.Language__c";
import CONTACT_PHONE from "@salesforce/schema/Contact.Phone";

export default class UpdateContactFromChat extends LightningElement {

    @api recordId;

    @wire( 
        getRecord, { 
            recordId: '$recordId', 
            fields: [
                CHAT_CONTACT_PHONE, 
                CHAT_PHONE,
                CHAT_CONTACT_LANGUAGE, 
                CHAT_LANGUAGE,
                CONTACTID_FIELD
            ] 
        } 
    ) wiredRecord( { error, data } ) {

        if ( error ) {

            let message = 'Unknown error';
            if ( Array.isArray(error.body ) ) {
                message = error.body.map(e => e.message).join( ', ' );
            } else if ( typeof error.body.message === 'string' ) {
                message = error.body.message;
            }
            console.error(
                message
            );

        } else if ( data ) {

            let contactPhone = getFieldValue( 
                data, CHAT_CONTACT_PHONE 
            );
            console.log(
                'contactPhone is',
                contactPhone
            );
            let chatPhone = getFieldValue( 
                data, CHAT_PHONE 
            );
            console.log(
                'chatPhone is',
                chatPhone
            );
            let contactLanguage = getFieldValue( 
                data, CHAT_CONTACT_LANGUAGE 
            );
            console.log(
                'contactLanguage is',
                contactLanguage
            );
            let chatLanguage = getFieldValue( 
                data, CHAT_LANGUAGE 
            );
            console.log(
                'chatLanguage is',
                chatLanguage
            );
            let contactId = getFieldValue( 
                data, CONTACTID_FIELD 
            );
            console.log(
                'contactId is',
                contactId
            );

            const fields = {};

            if ( 
                chatLanguage != contactLanguage &&
                chatLanguage.trim().length > 0
            ) {

                fields[ CONTACT_LANGUAGE.fieldApiName ] = chatLanguage;

            }

            if ( 
                chatPhone != contactPhone &&
                chatPhone.trim().length > 0
            ) {

                fields[ CONTACT_PHONE.fieldApiName ] = chatPhone;

            }

            if ( 
                Object.keys( fields ).length > 0 
            ) {

                fields[ ID_FIELD.fieldApiName ] = contactId;
                
                const recordInput = { fields };

                updateRecord( recordInput )
                .then( () => {

                    console.log(
                        'Contact updated successfully'
                    );
                    this.dispatchEvent(
                        new RefreshEvent()
                    );

                } )
                .catch( ( error ) => {

                    console.error(
                        error
                    );

                } );

            }

        }
    }

}

js-meta.xml:

<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>61.0</apiVersion>
    <isExposed>true</isExposed>  
    <targets>  
        <target>lightning__RecordPage</target>  
    </targets>  
</LightningComponentBundle>

Add the Lightning Web Component to the Chat Transcript Lightning Record Page. You can set Filter Status Equal to In Progress to render the Lightning Web Component only when the Agent Accepts the Chat from the Omni-Channel Widget.

Leave a Reply