Avoid duplicate items when creating contact in Php-Ews - php

I'm using https://github.com/Garethp/php-ews/ library to access to my public contact folder on Exchange server.
This is how i create a contact.
$api = API::withUsernameAndPassword($server, $user, $pass);
$folder = $api->getFolderByDisplayName('Public', Enumeration\DistinguishedFolderIdNameType::PUBLICFOLDERSROOT);
$contattiTotali = $api->getFolderByDisplayName('Contacts', $folder->getFolderId());
$id=$contattiTotali->getFolderId()->getId();
$api->setFolderId($contattiTotali->getFolderId());
$api->createContacts(array(
'GivenName' => 'Homer',
'Surname' => 'Simpson',
'EmailAddresses' => array(
'Entry' => array('Key' => Enumeration\EmailAddressKeyType::EMAIL_ADDRESS_1, '_value' => 'h.simpson#gmail.com')
),
//Creating multiple entries
'PhoneNumbers' => array(
'Entry' => array(
array('Key' => Enumeration\PhoneNumberKeyType::HOME_PHONE, '_value' => '000'),
array('Key' => Enumeration\PhoneNumberKeyType::BUSINESS_PHONE, '_value' => '111'),
)
),
'PhysicalAddresses' => array(
'Entry' => array(
'Key' => Enumeration\PhysicalAddressKeyType::HOME,
'street' => '123 Street',
'city' => '123 City',
'state' => '123 State',
'countryOrRegion' => '123 Country',
'postalCode' => '12345',
)
),
));
The code, actually, works fine, but if i execute it several times, it duplicates the contact.
Is there a way to check if the contact (email address is good enough) already exists before creating a new one?

The easiest way to telling if a contact already exists with a particular email address is to use the ResolveName operation eg
$request = new EWSType_ResolveNamesType();
$request->UnresolvedEntry = "address#domain.com";
$request->ReturnFullContactData = true;
$return = $ews->ResolveNames($request);
if ($return->ResponseMessages->ResolveNamesResponseMessage->ResponseCode == "NoError") {
return $return->ResponseMessages->ResolveNamesResponseMessage->ResolutionSet->Resolution->Mailbox->EmailAddress;
}

Related

Problem while adding Journal Entry with customer in Quickbook - Message: Passed array has no key for 'Value' when contructing an ReferenceType

I go through this documentation "https://developer.intuit.com/app/developer/qbo/docs/api/accounting/all-entities/journalentry"
And tried to add journal entry with line having customer as shown in the below code:
$journal_entry_create['TxnDate'] = date('Y-m-d');
$journal_entry_line = array(
'Amount' => $amount,
'DetailType' => 'JournalEntryLineDetail',
'JournalEntryLineDetail' => array(
'PostingType' => Credit,
'Entity' => array(
'Type' => 'Customer',
'EntityRef' => array(
'type' => 'Customer',
'value' => "2",
'name' => 'Abc'
)
),
'AccountRef' => array(
'name' => 'Account Name',
'value' => '1'
),
)
);
$journal_entry_lines[] = $journal_entry_line;
$journal_entry_create['Line'] = $journal_entry_lines;
$journal_entry_receipt_create = QBJournalEntry::create($journal_entry_create);
$journal_entry_receipt_create_result = $dataService->Add($journal_entry_receipt_create);
Without EntityRef its working fine but when I add EntityRef its giving me error "Message: Passed array has no key for 'Value' when contructing an ReferenceType"
Only just came across this problem myself. They did fix this issue but didn't seem to document it at all or tell anyone. Found the fix in the source code. You need to use "JournalEntryEntity" instead of "Entity" under "JournalEntryLineDetail", like so:
'JournalEntryLineDetail' => array(
'PostingType' => "Credit",
'JournalEntryEntity' => array(
'Type' => 'Customer',
'EntityRef' => array(
'value' => "2"
)
),
'AccountRef' => array(
'name' => 'Account Name',
'value' => '1'
),
)
Also I used the FacadeHelper from the V3 of the PHP SDK (I'm not sure if it'll work with the method you were using):
use QuickBooksOnline\API\Facades\FacadeHelper;
...
$journal_entry_receipt_create = FacadeHelper::reflectArrayToObject('JournalEntry', $journal_entry_create);
$journal_entry_receipt_create_result = $dataService->Add($journal_entry_receipt_create);
I know this is probably too late for you OP but hopefully it helps someone else!

Receiving "You passed an empty string" error when updating connected account identity information using Stripe Connect Custom API (PHP)

I'm making a form to update/add identity information using the Stripe Connect Custom API. But I am receiving a "You passed an empty string" error from the API for fields that I am not submitting.
$legal_entity = array(
'first_name'=>$first_name,
'last_name'=>$last_name,
'maiden_name'=>$maiden_name,
'personal_id_number'=>$personal_id_number,
'dob' => array(
'day' => $dob_day,
'month' => $dob_month,
'year' => $dob_year
),
'personal_address' => array(
'line1' => $address_line1,
'line2' => $address_line2,
'city' => $address_city,
'state' => $address_state,
'country' => $address_country,
'postal_code' => $address_postal_code
),
);
$account = \Stripe\Account::retrieve($stripe_account_id);
$account->legal_entity = $legal_entity;
$account->save();
You passed an empty string for 'legal_entity[type]'. We assume empty values are an attempt to unset a parameter; however 'legal_entity[type]' cannot be unset. You should remove 'legal_entity[type]' from your request or supply a non-empty value.
As you can see; I don't define the [type] member at all. If I add it; I then get the following error instead:
You passed an empty string for 'legal_entity[address]'. We assume empty values are an attempt to unset a parameter; however 'legal_entity[address]' cannot be unset. You should remove 'legal_entity[address]' from your request or supply a non-empty value.
EDIT: If I try retrieving the [legal_entity] and simply updating it and resaving it; like this:
$account = \Stripe\Account::retrieve($stripe_account_id);
$legal_entity = $account->legal_entity;
$legal_entity['first_name'] = $first_name;
$legal_entity['last_name'] = $last_name;
$legal_entity['maiden_name'] = $maiden_name;
$legal_entity['personal_id_number'] = $personal_id_number;
$legal_entity['dob'] = array(
'day' => $dob_day,
'month' => $dob_month,
'year' => $dob_year
);
$legal_entity['personal_address'] = array(
'line1' => $address_line1,
'line2' => $address_line2,
'city' => $address_city,
'state' => $address_state,
'country' => $address_country,
'postal_code' => $address_postal_code
);
$account->legal_entity = $legal_entity;
$account->save();
I get the following error:
Once a full set of basic legal entity information has been provided (type, business_name, first_name, last_name, and address), you cannot unset any of it, only update it.
How do I update the account holder or additional owners?
Not sure that my suggestion will work but it looks like you completely override legal_entity with new data. Try to merge existing data with new one. Something like:
$legal_entity = array(
'first_name'=>$first_name,
'last_name'=>$last_name,
'maiden_name'=>$maiden_name,
'personal_id_number'=>$personal_id_number,
'dob' => array(
'day' => $dob_day,
'month' => $dob_month,
'year' => $dob_year
),
'personal_address' => array(
'line1' => $address_line1,
'line2' => $address_line2,
'city' => $address_city,
'state' => $address_state,
'country' => $address_country,
'postal_code' => $address_postal_code
),
);
$account = \Stripe\Account::retrieve($stripe_account_id);
$account->legal_entity = array_merge($account->legal_entity, $legal_entity);
$account->save();
Omit $account->legal_entity = $legal_entity; in your updated example.
This, for example, works just fine!
// see https://stripe.com/docs/api/php#update_account for all options
$account = \Stripe\Account::create(array(
"type" => "custom",
"country" => "US",
"email" => "test#example.com"
));
// retrieve and update the account
$account = \Stripe\Account::retrieve($account->id);
$legal_entity = $account->legal_entity;
$legal_entity['type'] = "individual";
$legal_entity['first_name'] = "Jane";
$legal_entity['last_name'] = "Allen";
$legal_entity['personal_id_number'] = "000000000";
$legal_entity['dob'] = array(
'day' => 1,
'month' => 1,
'year' => 1901
);
$legal_entity['address'] = array(
'line1' => "1 Main St",
'line2' => "Suite 111",
'city' => "San Francisco",
'state' => "CA",
'country' => "US",
'postal_code' => "94103"
);
$account->tos_acceptance = array(
'date' => time(),
"ip" => "10.10.0.10"
);
echo $account->save();

Invalid parameter Facebook Lead Leadgen Form in Facebook Marketing API

I am getting error from Facebook Marketing API when creating Leadgen Form. I followed Facebook guide but nothing worked yet. API documentation link
In previous I was successfully create leadgen legal content, leadgen context cards, leadgen conditional questions group and conditional questions group csv. After that I am trying to create Leadgen Form but API returns Invalid parameter
$config = Config::get('facebook');
$data['account_id'] = 'act_'.$config['ad_account_id'];
$data['page_id'] = $config['page_id'];
$api = Api::init($config['app_id'], $config['app_secret'], $config['access_token']);
// Init facebook
$legal_params = array(
'privacy_policy' => array(
'url' => 'http://example.com/privacy-policy',
'link_text' => 'Privacy Policy'
),
'custom_disclaimer' => array(
'title' => 'Terms and Conditions',
'body' => array(
'text' => 'My custom disclaimer',
'url_entities' => array(
array("offset" => 3, "length" => 6, "url" => 'http://example.com/privacy-policy')
),
),
'checkboxes' => array(array(
"is_required" => false,
"is_checked_by_default" => false,
"text" => "Allow to contact you",
"key" => "checkbox_1",
))
),
);
$legal_res = Api::instance()->call('/'.$data['page_id'].'/leadgen_legal_content',
RequestInterface::METHOD_POST,
$legal_params)->getContent();
$legal_content_id = $legal_res['id'];
$contex_params = array(
'title' => 'Thank You',
'style' => 'LIST_STYLE',
'content' => array(
'Easy sign-up flow',
'Submit your info to have a chance to win',
),
'button_text' => 'Get started',
);
$contex_res = Api::instance()->call('/'.$data['page_id'].'/leadgen_context_cards',
RequestInterface::METHOD_POST,
$contex_params)->getContent();
$context_card_id = $contex_res['id'];
// Conditional Questions
$conditional_request = Api::instance()->prepareRequest(
'/'.$data['page_id'].'/leadgen_conditional_questions_group',
RequestInterface::METHOD_POST);
$conditional_request->getFileParams()->offsetSet(
'conditional_questions_group_csv',
(new FileParameter(public_path().'/facebook/lead-csv-form.csv'))->setMimeType("text/csv"));
$csv_data = Api::instance()->executeRequest($conditional_request)->getContent();
$questions_group_id = $csv_data['id'];
// Create Lead Form
$form = new LeadgenForm(null, $data['page_id']);
$form->setData(array(
LeadgenFormFields::NAME => 'Test LeadAds Form Name',
LeadgenFormFields::FOLLOW_UP_ACTION_URL => 'https://www.facebook.com/examplepage',
LeadgenFormFields::QUESTIONS => array(
(new LeadGenQuestion())->setData(array(
LeadgenQuestionFields::TYPE => 'EMAIL',
)),
(new LeadGenQuestion())->setData(array(
LeadgenQuestionFields::TYPE => 'CUSTOM',
LeadgenQuestionFields::LABEL => 'Country',
'conditional_questions_group_id' => $questions_group_id,
'dependent_conditional_questions' => array(
array('name' => 'State'),
array('name' => 'City'),
)
)),
),
'block_display_for_non_targeted_viewer' => true,
'context_card_id' => $context_card_id,
'legal_content_id' => $legal_content_id,
));
$form->create();

Elastica add documents overrides existing ones

When i try to add new documents to an index type , i loose existing documents which are overwritten by the new added ones . The problem can be related to the id of each added document ??
Here is the code :
$elasticaClient = new \Elastica\Client(array(
'host' => $this->container->getParameter('elastic_host'),
'port' => $this->container->getParameter('elastic_port')
));
$elasticaIndex = $elasticaClient->getIndex('app');
$elasticaIndex->create(
array(
'number_of_shards' => 4,
'number_of_replicas' => 1,
'analysis' => array(
'analyzer' => array(
'indexAnalyzer' => array(
'type' => 'custom',
'tokenizer' => 'standard',
'filter' => array('lowercase', 'mySnowball')
),
'searchAnalyzer' => array(
'type' => 'custom',
'tokenizer' => 'standard',
'filter' => array('standard', 'lowercase', 'mySnowball')
)
),
'filter' => array(
'mySnowball' => array(
'type' => 'snowball',
'language' => 'German'
)
)
)
),
true
);
$elasticaType = $elasticaIndex->getType('type');
$mapping = new \Elastica\Type\Mapping();
$mapping->setType($elasticaType);
$mapping->setParam('index_analyzer', 'indexAnalyzer');
$mapping->setParam('search_analyzer', 'searchAnalyzer');
$mapping->setProperties(array(
'id' => array('type' => 'string'),
'title' => array('type' => 'string'),
'duration' => array('type' => 'string'),
'start' => array('type' => 'string'),
'end' => array('type' => 'string'),
));
// Send mapping to type
$mapping->send();
$documents = array();
foreach($medias as $media) {
$id = uniqid() ;
$documents[] = new \Elastica\Document(
$id,
array(
'id' => $id,
'title' => $media['title'],
'duration' => $media['duration'],
'start' => $media['start'],
'end' => $media['end'],
)
);
}
$elasticaType->addDocuments($documents);
$elasticaType->getIndex()->refresh();
Please i need your help . Thank you
PHP does not recommend using uniqid for this use case. Since you are wanting a random, safe id, let Elasticsearch do it for you. The Elastica Document construct method notes that the id field is optional. So don't pass it and let Elasticsearch issue the id.
Several things
$elasticaIndex->create (....) you only have to enter it once. Index is unique after creating the index that you can comment or generate a different index and other things. I leave an example that works.
class PersistencyElastic
{
private $conection;
public function __construct()
{
$this->conection = new \Elastica\Client(['host' => '127.0.0.1', 'port' => 9200]);
}
public function save($ msg)
{
// $ msg is an array with whatever you want inside
$index = $this->conection->getIndex('googlephotos');
// This is the index I created, it's called googlephotos
// $index->create(array (), true);
$type = $index->getType('googlephotos');
$type->addDocument(new Document (uniqid ('id _', false), $msg, $type, $index));
$index->refresh();
}
}

SOAP 4 set_relationship not working

After battling for getting more than 20 entries with get_entry_list, i'm now trying to use the SOAP API on SugarCRM 6.5 to set a relationship between two elements, created from a form on the user-land website.
The set_relationship method is described as following in the devs blog :
$response = set_relationship(session, module_name, module_id, link_field_name, related_ids, name_value_list, delete);
So here is the code which handles the request, assuming that another part of the code handles the security.
$values = array( 'id_frame' => $_POST['id_frame'],
'id_battery' => $_POST['id_battery'],
'reseller' => $_POST['reseller'],
'date_purchase' => $_POST['date_purchase'],
'products_versionning' => $_POST['product_purchased'],
'first_name' => $_POST['first_name'],
'last_name' => $_POST['name'],
'phone_home' => $_POST['phone'],
'email' => $_POST['email'],
'primary_address_street' => $_POST['address'],
'primary_address_street_2' => $_POST['address2'],
'primary_address_street_city' => $_POST['city'],
'primary_address_street_postalcode' => $_POST['zip'],
);
try{
$prod_register = $soapClient->set_entry(
$sessid,
'myco_product_register',
array( array('name' => 'id_frame', 'value' => $values['id_frame']),
array('name' => 'id_battery', 'value' => $values['id_battery']),
array('name' => 'date_purchase', 'value' => $values['date_purchase']),
array('name' => 'first_name', 'value' => $values['first_name']),
array('name' => 'last_name', 'value' => $values['last_name']),
array('name' => 'phone_home', 'value' => $values['phone_home']),
array('name' => 'email', 'value' => $values['email']),
array('name' => 'primary_address_street', 'value' => $values['primary_address_street']),
array('name' => 'primary_address_street_city', 'value' => $values['primary_address_street_city']),
array('name' => 'primary_address_street_postalcode','value' => $values['primary_address_street_postalcode']),
array('name' => 'description','value' => "Modèle : " . $values['products_versionning'] . "\nAcheté le " . $values['date_purchase'] . " à " . $values['reseller']),
)
);
$client = $soapClient->set_entry(
$sessid,
'Accounts',
array( array('name' => 'name', 'value' => $values['first_name'] . ' ' . $values['last_name']),
array('name' => 'billing_address_street', 'value' => $values['primary_address_street']),
array('name' => 'billing_address_city', 'value' => $values['primary_address_street_city']),
array('name' => 'billing_address_postalcode', 'value' => $values['primary_address_street_postalcode']),
)
);
$entry_id = $prod_register->id;
$relationship_parameters = array(
"module1" => "myco_product_register",
"module1_id" => array($entry_id),
"module2" => "myco_products_versionning",
"module2_id" => array($values['products_versionning'])
);
//Now i'm setting the relationships
$response = $soapClient->set_relationship($sessid, "myco_product_register", $entry_id,
'myco_products_versionning_id_c', $values['products_versionning'], array(), 0);
$response = $soapClient->set_relationship($sessid, "myco_product_register", $entry_id,
'myco_resellers_id_c', $values['reseller'], array(), 0);
Both set_entry requests work and return a working id, but no one of the relationships work ($responses contains a failed equal to 1). So that's not a connection problem or such.
Talking about the relationships, one guy from the devblog said that
there has to be a relationship between the two modules
there has to be at least one related field in the module which handle the relation, whose name you can find in the module's vardefs.php
and i have
A One-to-one relationship between the product_register module and both products_versionning and resellers
A related field in product_register for each related module.
What may i be missing ?
try checking the log file of SugarCRM, you should find some mistakes. I used this API to create a relationship between Contacts and Accounts, and working properly. On the logs might find the answer to the problem.
try {
$result = $this->soapClient->set_relationship($this->sessionId,
$accountModuleName, $accountId, $relationName, $contactsId);
$this->doEvent(self::EVENT_WS_OPERATION_CALL);
if ($result->created > 0 && $result->failed === 0) {
return true;
} else {
return false;
}

Categories