@@ -41,11 +41,23 @@ public function generateReqDataFromFieldMap($data, $fieldMap)
4141 return $ dataFinal ;
4242 }
4343
44- public function insertContact ($ finalData )
44+ public function insertContact ($ finalData, $ update = false )
4545 {
4646 $ apiEndpoint = $ this ->_apiDomain . '/services/data/v37.0/sobjects/Contact ' ;
4747
48- return HttpHelper::post ($ apiEndpoint , wp_json_encode ($ finalData ), $ this ->_defaultHeader );
48+ $ response = HttpHelper::post ($ apiEndpoint , wp_json_encode ($ finalData ), $ this ->_defaultHeader );
49+
50+ $ duplicateRecordId = isset ($ response [0 ]->duplicateResult ->matchResults [0 ]->matchRecords [0 ]->record ->Id )
51+ ? $ response [0 ]->duplicateResult ->matchResults [0 ]->matchRecords [0 ]->record ->Id
52+ : null ;
53+
54+ if (!$ update || !$ duplicateRecordId ) {
55+ return $ response ;
56+ }
57+
58+ $ apiEndpoint = $ apiEndpoint . '/ ' . $ duplicateRecordId ;
59+
60+ return apply_filters ('btcbi_salesforce_update_record ' , $ response , $ apiEndpoint , $ finalData , $ this ->_defaultHeader );
4961 }
5062
5163 public function insertRecord ($ finalData , $ action )
@@ -138,26 +150,28 @@ public function createCase($finalData, $actionsData)
138150 return HttpHelper::post ($ apiEndpoint , wp_json_encode ($ finalData ), $ this ->_defaultHeader );
139151 }
140152
141- public function execute (
142- $ integrationDetails ,
143- $ fieldValues ,
144- $ fieldMap ,
145- $ actions ,
146- $ tokenDetails
147- ) {
153+ public function execute ($ integrationDetails , $ fieldValues , $ fieldMap , $ actions )
154+ {
148155 $ actionName = $ integrationDetails ->actionName ;
156+ $ update = isset ($ actions ->update ) ? $ actions ->update : false ;
157+
149158 if ($ actionName === 'contact-create ' ) {
150159 $ finalData = $ this ->generateReqDataFromFieldMap ($ fieldValues , $ fieldMap );
151- $ insertContactResponse = $ this ->insertContact ($ finalData );
152- if (\is_object ($ insertContactResponse ) && property_exists ($ insertContactResponse , 'id ' )) {
153- LogHandler::save ($ this ->_integrationID , wp_json_encode (['type ' => $ actionName , 'type_name ' => 'Contact-create ' ]), 'success ' , wp_json_encode (wp_sprintf (__ ('Created contact id is : %s ' , 'bit-integrations ' ), $ insertContactResponse ->id )));
154- } else {
155- LogHandler::save ($ this ->_integrationID , wp_json_encode (['type ' => 'Contact ' , 'type_name ' => 'Contact-create ' ]), 'error ' , wp_json_encode ($ insertContactResponse ));
160+ $ response = $ this ->insertContact ($ finalData , $ update );
161+
162+ $ responseType = empty ($ response ) || isset ($ response ->id ) ? 'success ' : 'error ' ;
163+ $ typeName = !$ update || isset ($ response ->id ) ? 'Contact-create ' : 'Contact-update ' ;
164+ $ message = $ responseType === 'success ' ? wp_json_encode (wp_sprintf (__ ('Created contact id is : %s ' , 'bit-integrations ' ), $ response ->id )) : wp_json_encode ($ response );
165+
166+ if ($ responseType === 'success ' && $ update ) {
167+ $ message = __ ('Contact Updated Successfully ' , 'bit-integrations ' );
156168 }
169+
170+ LogHandler::save ($ this ->_integrationID , wp_json_encode (['type ' => 'Contact ' , 'type_name ' => $ typeName ]), $ responseType , $ message );
157171 } elseif ($ actionName === 'lead-create ' ) {
158172 $ finalData = $ this ->generateReqDataFromFieldMap ($ fieldValues , $ fieldMap );
159173
160- $ finalData = apply_filters ('btcbi_salesforce_add_lead_utilities ' , $ finalData , $ integrationDetails -> actions );
174+ $ finalData = apply_filters ('btcbi_salesforce_add_lead_utilities ' , $ finalData , $ actions );
161175
162176 $ insertLeadResponse = $ this ->insertLead ($ finalData );
163177
@@ -169,14 +183,15 @@ public function execute(
169183 } elseif ($ actionName === 'account-create ' ) {
170184 $ finalData = $ this ->generateReqDataFromFieldMap ($ fieldValues , $ fieldMap );
171185
172- if (!empty ($ integrationDetails -> actions ->selectedAccType )) {
173- $ finalData ['Type ' ] = $ integrationDetails -> actions ->selectedAccType ;
186+ if (!empty ($ actions ->selectedAccType )) {
187+ $ finalData ['Type ' ] = $ actions ->selectedAccType ;
174188 }
175- if (!empty ($ integrationDetails -> actions ->selectedOwnership )) {
176- $ finalData ['Ownership ' ] = $ integrationDetails -> actions ->selectedOwnership ;
189+ if (!empty ($ actions ->selectedOwnership )) {
190+ $ finalData ['Ownership ' ] = $ actions ->selectedOwnership ;
177191 }
178192
179193 $ createAccountResponse = $ this ->createAccount ($ finalData );
194+
180195 if (\is_object ($ createAccountResponse ) && property_exists ($ createAccountResponse , 'id ' )) {
181196 LogHandler::save ($ this ->_integrationID , wp_json_encode (['type ' => 'Account ' , 'type_name ' => 'Account-create ' ]), 'success ' , wp_json_encode (wp_sprintf (__ ('Created account id is : %s ' , 'bit-integrations ' ), $ createAccountResponse ->id )));
182197 } else {
@@ -214,11 +229,11 @@ public function execute(
214229 LogHandler::save ($ this ->_integrationID , wp_json_encode (['type ' => 'Task ' , 'type_name ' => 'Task-create ' ]), 'error ' , wp_json_encode ($ apiResponse ));
215230 }
216231 } elseif ($ actionName === 'opportunity-create ' ) {
217- $ opportunityTypeId = empty ($ integrationDetails -> actions ->opportunityTypeId ) ? null : $ integrationDetails -> actions ->opportunityTypeId ;
218- $ opportunityStageId = empty ($ integrationDetails -> actions ->opportunityStageId ) ? null : $ integrationDetails -> actions ->opportunityStageId ;
219- $ opportunityLeadSourceId = empty ($ integrationDetails -> actions ->opportunityLeadSourceId ) ? null : $ integrationDetails -> actions ->opportunityLeadSourceId ;
220- $ accountId = empty ($ integrationDetails -> actions ->accountId ) ? null : $ integrationDetails -> actions ->accountId ;
221- $ campaignId = empty ($ integrationDetails -> actions ->campaignId ) ? null : $ integrationDetails -> actions ->campaignId ;
232+ $ opportunityTypeId = empty ($ actions ->opportunityTypeId ) ? null : $ actions ->opportunityTypeId ;
233+ $ opportunityStageId = empty ($ actions ->opportunityStageId ) ? null : $ actions ->opportunityStageId ;
234+ $ opportunityLeadSourceId = empty ($ actions ->opportunityLeadSourceId ) ? null : $ actions ->opportunityLeadSourceId ;
235+ $ accountId = empty ($ actions ->accountId ) ? null : $ actions ->accountId ;
236+ $ campaignId = empty ($ actions ->campaignId ) ? null : $ actions ->campaignId ;
222237 $ finalData = $ this ->generateReqDataFromFieldMap ($ fieldValues , $ fieldMap );
223238 $ opportunityResponse = $ this ->createOpportunity ($ finalData , $ opportunityTypeId , $ opportunityStageId , $ opportunityLeadSourceId , $ accountId , $ campaignId );
224239 if (\is_object ($ opportunityResponse ) && property_exists ($ opportunityResponse , 'id ' )) {
@@ -227,9 +242,9 @@ public function execute(
227242 LogHandler::save ($ this ->_integrationID , wp_json_encode (['type ' => 'Opportunity ' , 'type_name ' => 'Opportunity-create ' ]), 'error ' , wp_json_encode ($ opportunityResponse ));
228243 }
229244 } elseif ($ actionName === 'event-create ' ) {
230- $ contactId = empty ($ integrationDetails -> actions ->contactId ) ? null : $ integrationDetails -> actions ->contactId ;
231- $ accountId = empty ($ integrationDetails -> actions ->accountId ) ? null : $ integrationDetails -> actions ->accountId ;
232- $ eventSubjectId = empty ($ integrationDetails -> actions ->eventSubjectId ) ? null : $ integrationDetails -> actions ->eventSubjectId ;
245+ $ contactId = empty ($ actions ->contactId ) ? null : $ actions ->contactId ;
246+ $ accountId = empty ($ actions ->accountId ) ? null : $ actions ->accountId ;
247+ $ eventSubjectId = empty ($ actions ->eventSubjectId ) ? null : $ actions ->eventSubjectId ;
233248 $ finalData = $ this ->generateReqDataFromFieldMap ($ fieldValues , $ fieldMap );
234249 $ createEventResponse = $ this ->createEvent ($ finalData , $ contactId , $ accountId , $ eventSubjectId );
235250 if (\is_object ($ createEventResponse ) && property_exists ($ createEventResponse , 'id ' )) {
@@ -238,15 +253,15 @@ public function execute(
238253 LogHandler::save ($ this ->_integrationID , wp_json_encode (['type ' => 'Event ' , 'type_name ' => 'Event-create ' ]), 'error ' , wp_json_encode ($ createEventResponse ));
239254 }
240255 } elseif ($ actionName === 'case-create ' ) {
241- $ actionsData ['ContactId ' ] = empty ($ integrationDetails -> actions ->contactId ) ? null : $ integrationDetails -> actions ->contactId ;
242- $ actionsData ['AccountId ' ] = empty ($ integrationDetails -> actions ->accountId ) ? null : $ integrationDetails -> actions ->accountId ;
243- $ actionsData ['Status ' ] = empty ($ integrationDetails -> actions ->caseStatusId ) ? null : $ integrationDetails -> actions ->caseStatusId ;
244- $ actionsData ['Origin ' ] = empty ($ integrationDetails -> actions ->caseOriginId ) ? null : $ integrationDetails -> actions ->caseOriginId ;
245- $ actionsData ['Priority ' ] = empty ($ integrationDetails -> actions ->casePriorityId ) ? null : $ integrationDetails -> actions ->casePriorityId ;
246- $ actionsData ['Reason ' ] = empty ($ integrationDetails -> actions ->caseReason ) ? null : $ integrationDetails -> actions ->caseReason ;
247- $ actionsData ['Type ' ] = empty ($ integrationDetails -> actions ->caseType ) ? null : $ integrationDetails -> actions ->caseType ;
248- $ actionsData ['PotentialLiability__c ' ] = empty ($ integrationDetails -> actions ->potentialLiabilityId ) ? null : $ integrationDetails -> actions ->potentialLiabilityId ;
249- $ actionsData ['SLAViolation__c ' ] = empty ($ integrationDetails -> actions ->slaViolationId ) ? null : $ integrationDetails -> actions ->slaViolationId ;
256+ $ actionsData ['ContactId ' ] = empty ($ actions ->contactId ) ? null : $ actions ->contactId ;
257+ $ actionsData ['AccountId ' ] = empty ($ actions ->accountId ) ? null : $ actions ->accountId ;
258+ $ actionsData ['Status ' ] = empty ($ actions ->caseStatusId ) ? null : $ actions ->caseStatusId ;
259+ $ actionsData ['Origin ' ] = empty ($ actions ->caseOriginId ) ? null : $ actions ->caseOriginId ;
260+ $ actionsData ['Priority ' ] = empty ($ actions ->casePriorityId ) ? null : $ actions ->casePriorityId ;
261+ $ actionsData ['Reason ' ] = empty ($ actions ->caseReason ) ? null : $ actions ->caseReason ;
262+ $ actionsData ['Type ' ] = empty ($ actions ->caseType ) ? null : $ actions ->caseType ;
263+ $ actionsData ['PotentialLiability__c ' ] = empty ($ actions ->potentialLiabilityId ) ? null : $ actions ->potentialLiabilityId ;
264+ $ actionsData ['SLAViolation__c ' ] = empty ($ actions ->slaViolationId ) ? null : $ actions ->slaViolationId ;
250265
251266 $ finalData = $ this ->generateReqDataFromFieldMap ($ fieldValues , $ fieldMap );
252267 $ createCaseResponse = $ this ->createCase ($ finalData , $ actionsData );
0 commit comments