33< head >
44 < meta charset ="UTF-8 " />
55 < meta name ="viewport " content ="width=device-width, initial-scale=1 " />
6- < title > Demo Salesforce - Auto Load</ title >
6+ < title > Demo Salesforce - Auto Load Fixed </ title >
77 < style >
88 body { font-family : Arial, sans-serif; display : flex; justify-content : center; align-items : center; min-height : 100vh ; background : # ffffff ; margin : 0 ; }
99 .box { background : # fff ; padding : 24px ; border : 1px solid # eee ; border-radius : 10px ; box-shadow : 0 10px 30px rgba (0 , 0 , 0 , 0.05 ); text-align : left; width : 350px ; }
1010 h1 { margin : 0 0 20px ; font-size : 24px ; text-align : center; color : # 333 ; }
1111 .info-row { margin-bottom : 10px ; font-size : 16px ; color : # 555 ; border-bottom : 1px solid # f0f0f0 ; padding-bottom : 5px ; }
1212 .info-row strong { color : # 000 ; display : inline-block; width : 100px ; }
13+ .status { font-size : 12px ; text-align : center; color : # 0969da ; margin-top : 15px ; font-weight : bold; }
1314 </ style >
1415</ head >
1516< body >
1617
1718 < div class ="box ">
1819 < h1 > Dati Utente</ h1 >
1920
20- < div class ="info-row "> < strong > name:</ strong > < span id ="disp-name "> </ span > </ div >
21- < div class ="info-row "> < strong > userType:</ strong > < span id ="disp-type "> </ span > </ div >
22- < div class ="info-row "> < strong > nationality:</ strong > < span id ="disp-nat "> </ span > </ div >
23- < div class ="info-row "> < strong > userId:</ strong > < span id ="disp-id "> </ span > </ div >
21+ < div class ="info-row "> < strong > name:</ strong > < span id ="disp-name "> ... </ span > </ div >
22+ < div class ="info-row "> < strong > userType:</ strong > < span id ="disp-type "> ... </ span > </ div >
23+ < div class ="info-row "> < strong > nationality:</ strong > < span id ="disp-nat "> ... </ span > </ div >
24+ < div class ="info-row "> < strong > userId:</ strong > < span id ="disp-id "> ... </ span > </ div >
2425
26+ < div id ="status-msg " class ="status "> In attesa di inizializzazione...</ div >
27+
2528 < p style ="text-align:center; color:#999; font-size:12px; margin-top:20px; ">
2629 Chogan
2730 </ p >
2831 </ div >
2932
30- < script >
31- // --- Variabili Hardcodate ---
32- const name = "Mario Rossi" ;
33- const userType = "consultant" ;
34- const nationality = "IT" ;
35- const userId = "12345" ;
36-
37- // Mostra i dati a video
38- document . getElementById ( 'disp-name' ) . innerText = name ;
39- document . getElementById ( 'disp-type' ) . innerText = userType ;
40- document . getElementById ( 'disp-nat' ) . innerText = nationality ;
41- document . getElementById ( 'disp-id' ) . innerText = userId ;
42- </ script >
43-
4433 < script type ='text/javascript '>
4534 function initEmbeddedMessaging ( ) {
46- console . log ( "--> INIT SALESFORCE <--" ) ;
35+ console . log ( "--> INIT SALESFORCE START <--" ) ;
36+
4737 try {
48- // Settings Lingua e Input
38+ // 1. DEFINIZIONE VARIABILI (Tutto dentro la funzione per sicurezza scope)
39+ // Usiamo un oggetto per pulizia, ma puoi usare const separate
40+ const userData = {
41+ firstName : "Mario Rossi" , // Ho cambiato nome var per evitare conflitti con window.name
42+ userType : "consultant" ,
43+ nationality : "IT" ,
44+ userId : "12345"
45+ } ;
46+
47+ // 2. AGGIORNAMENTO INTERFACCIA (Così vediamo cosa stiamo per inviare)
48+ document . getElementById ( 'disp-name' ) . innerText = userData . firstName ;
49+ document . getElementById ( 'disp-type' ) . innerText = userData . userType ;
50+ document . getElementById ( 'disp-nat' ) . innerText = userData . nationality ;
51+ document . getElementById ( 'disp-id' ) . innerText = userData . userId ;
52+
53+ // --- IMPOSTAZIONI SALESFORCE ---
4954 embeddedservice_bootstrap . settings . language = 'it' ;
5055 embeddedservice_bootstrap . settings . enableUserInputForConversationWithBot = false ;
5156
52- // Quando la chat è pronta (caricata), imposta i campi e basta.
57+ // 3. GESTIONE EVENTO "PRONTO"
5358 window . addEventListener ( "onEmbeddedMessagingReady" , ( ) => {
54- console . log ( "Messaging Ready: Imposto i campi nascosti ..." ) ;
59+ console . log ( ">>> EVENTO: Messaging Ready. Invio parametri ..." ) ;
5560
5661 try {
62+ // Qui usiamo le variabili definite poche righe sopra (Closure corretta)
5763 embeddedservice_bootstrap . prechatAPI . setHiddenPrechatFields ( {
58- "name" : name ,
59- "userType" : userType ,
60- "nationality" : nationality ,
61- "userId" : userId
64+ "name" : userData . firstName ,
65+ "userType" : userData . userType ,
66+ "nationality" : userData . nationality ,
67+ "userId" : userData . userId
6268 } ) ;
63- console . log ( "Parametri Hidden OK:" , { name, userType, nationality, userId } ) ;
69+
70+ console . log ( ">>> Parametri Hidden impostati con successo:" , userData ) ;
71+ document . getElementById ( 'status-msg' ) . innerText = "Chat Pronta & Parametri Inviati!" ;
72+ document . getElementById ( 'status-msg' ) . style . color = "green" ;
73+
6474 } catch ( e ) {
65- console . error ( "Errore campi hidden:" , e ) ;
75+ console . error ( "Errore nel settare i campi hidden:" , e ) ;
76+ document . getElementById ( 'status-msg' ) . innerText = "Errore Parametri" ;
77+ document . getElementById ( 'status-msg' ) . style . color = "red" ;
6678 }
6779 } ) ;
6880
69- // Init Snippet
81+ // 4. INIZIALIZZAZIONE (Bootstrap)
82+ // Nota: Ho mantenuto gli ID specifici del tuo secondo script (devai2)
7083 embeddedservice_bootstrap . init (
7184 '00DUA000003hFV7' ,
7285 'ai_chogangroupspa_com' ,
@@ -77,7 +90,7 @@ <h1>Dati Utente</h1>
7790 ) ;
7891
7992 } catch ( err ) {
80- console . error ( 'Errore Init:' , err ) ;
93+ console . error ( 'Errore critico durante Init:' , err ) ;
8194 }
8295 } ;
8396 </ script >
0 commit comments