You need to sign in to do that
Don't have an account?
Ivan Winzer
call to magento API from salesforce
So im in a pickle. The magento developer is asking for my contact/account creation trigger to call their API and below is what i need to add ino my trigger but not sure where and how to get it to fire the request. This what I have currently in my trigger:
and this is what i need to add into it somewhere to make this call to magento:
Can anyone help me..
Thanks,
Ivan
trigger MainContactTrigger on Contact (before update, before insert, after update, after insert) { DuplicateEmailFilter def = new DuplicateEmailFilter(); Map<id,Contact> oldMap = trigger.oldMap; CreateAccountFromContact accountCreation = New CreateAccountFromContact(); if(oldMap == null){ oldMap = new Map<Id,Contact>(); } if(Trigger.isBefore){ // // For the before creation we check if the contact is getting created by a real user then we use // the Duplicate Email Filter to generate an error preventing the contact from being created. // if(!IntegrationUtils.isIntegrationUser()){ def.processContacts(trigger.new, oldMap); } if( Trigger.isInsert && !IntegrationUtils.ignoreForAutoAccountUser() ) { accountCreation.ProcessBatchContactsWEmail(trigger.new,trigger.oldMap); } else if( Trigger.isInsert || Trigger.isUpdate ) { accountCreation.ProcessBatchContacts(trigger.new,trigger.oldMap); } } else{ // // This is the After trigger - we just need to check if the email address is already // in use in the duplicate email filter. // if it is being created by the integration user then in the DEF we flag the contct as having // a duplicate. Leaving it up to the Salesforce Admin to clean it up. // if(IntegrationUtils.isIntegrationUser()){ def.processContacts(trigger.new, oldMap); } } }
and this is what i need to add into it somewhere to make this call to magento:
Salesforce to Magento Customer update endpoint url: https://hallwinesm2-uat.smarterspecies.com/rest/V1/hall/salesforce/customer/sync Content-Type:application/json POST: { "customer": { "firstname": "Maciej", "lastname": "Garycki", "email": "mgarycki+sf5333@gorillagroup.com", "telephone": "6924609876", "contact_sf_id": "1234567890", "sfdc_updated_at": "182736", "rms_account_no": "12345", "customer_group_code": "GENERAL", "revel_id": "", "website_id": "1", "store_id": "1", "uuid": "", "addresses": [] } }
Can anyone help me..
Thanks,
Ivan
I've put together some code that should get your most of the way done. I couldn't test it.
Make sure to do the following:
1. Create a Custom Metadata type called "Magento Setting" (Magento Settings in the plural field)
2. In the Custom Metadata Type, create a String field called "Value" and make it 255 chars and required
3. Create the following values by going to "Manage Magento Settings"
a. Username (put the username for Magento integation)
b. Password (put the password for Magento integration)
c. Token Endpoint (put the endpoint for getting the token)
d. Sync Enpoint (put the endpoint that has "sync" at the end)
4. Create an Apex class called "MagentoUtility" and put the following:
Here is your trigger with some code to get you started on working with the Magento Utility class. I'm not sure how you are providing the fields like "RMS Account No" so I'll let you put that code in. I commented those lines out. Also, make sure to build you addresses the way the vendor wants to recieve them.
In the event that your code is not receiving the token before the trigger attempts to POST to their API, you may need to implement functionality that waits for the token.
You will want to write a Test Class for the Magento Utility class, but I'd focus on getting this working first.
If this answer suits your needs, feel free to mark it as the best answer. :) Good luck!
All Answers
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_http_httprequest.htm#apex_classes_restful_http_httprequest
You may want to find out about how the endpoint authenticates your request. You might need to pass credentials in the request or use a token retrieved from their API.
Your such a life saver for responding. Thank you for any help you give.
So updated my code and not am getting a debug message :"Consumer is not authorized to access %resources","parameters":{"resources":"Hall_Salesforce::salesforce_api"}} which makes me to believe that Magento is not allowing my callout to process thru. Not that the credentials in my code did not work correct?
If access denied is an error from the vendor's API, then I suggest asking them what they are recieving from your callouts. It could tell you if your request isn't being received correctly.
Additional Considerations:
I'd print the Response Status and Response Code. It is possible you might get back a 200 or some other reponse code that could be a valid scenario.
I'd also print out the Request to help with your troubleshooting.
https://devdocs.magento.com/guides/v2.2/get-started/authentication/gs-authentication-token.html
This might also be helpful: https://devdocs.magento.com/guides/v2.2/get-started/rest_front.html
So i heard back from the magento development team and they did confirm that i need to do the call and send to get the authentication keys. They send back some examples of curl code but this is something new to me. Would you be able to help em figure out how to incorporate this into my code:
Step 1. Get a token
CLI command to make a request and response:
Step 2. Send Request with a token
Use received token l21x2wbqq9ukrpmmrhs60px8smqrc66k to make a Sync request
The same example can be represented in HTTP RAW requests and responses.
Step 1. Request.
Step 2. Request
Im going to attempt but shooting in the dark so any help is greatly apprecaited.
Ivan
I've put together some code that should get your most of the way done. I couldn't test it.
Make sure to do the following:
1. Create a Custom Metadata type called "Magento Setting" (Magento Settings in the plural field)
2. In the Custom Metadata Type, create a String field called "Value" and make it 255 chars and required
3. Create the following values by going to "Manage Magento Settings"
a. Username (put the username for Magento integation)
b. Password (put the password for Magento integration)
c. Token Endpoint (put the endpoint for getting the token)
d. Sync Enpoint (put the endpoint that has "sync" at the end)
4. Create an Apex class called "MagentoUtility" and put the following:
Here is your trigger with some code to get you started on working with the Magento Utility class. I'm not sure how you are providing the fields like "RMS Account No" so I'll let you put that code in. I commented those lines out. Also, make sure to build you addresses the way the vendor wants to recieve them.
In the event that your code is not receiving the token before the trigger attempts to POST to their API, you may need to implement functionality that waits for the token.
You will want to write a Test Class for the Magento Utility class, but I'd focus on getting this working first.
If this answer suits your needs, feel free to mark it as the best answer. :) Good luck!
If at the below line of code. Any idea why it doens tlike this string created for the token?
If its a public string from the class i dont have to define it within the trigger do i? Other than that everything saved well and my custom metadata types are in and ready to test.
Ivan
ClassContactTriggerTestSuite
Method NametestCreateAccountFromContact
Pass/FailFail
Error MessageSystem.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, MainContactTrigger: execution of AfterInsert
caused by: System.QueryException: List has no rows for assignment to SObject
Class.MagentoUtility.getToken: line 25, column 1
Trigger.MainContactTrigger: line 41, column 1: []
Stack TraceClass.ContactTriggerTestSuite.testCreateAccountFromContact: line 8, column 1
This is my test script nothing big so thinking may need to add more points to test:
And so i went in an tried to void out the getToken method in the class
but got a new error that says " Void method must not return a value at line 56 column 9" which is here:
I've probably done this wrong when you said void :( so were you saying to take out the entire section of code since it isnt needed?
I tried to comment it out but go a whole new error so figured it had to stay. Is it giving it becasue of the void call? Should i comment out the entire call itself since im not returning the token?
to:
So i was able to figure this out last night by making a few changes but this is where im stalled at now. So first i realized that i needed to change the DeveloperName = to the metadata field names i created in the Magento settings (were different from what you had provided) so that resolved my token issues. But then a new error was recieved with the message "Callout from triggers are currently not supported.: Class.MagentoUtility.getToken: line 48, column 1". So i did some research and found out that i may need to create a @future (callout=true) class in order to do the http request that i have inside. So it now does not like this section of code:
So im wondering do i need to essentially take this out and place into this future callout class and then call that class from this one to have it connect. Sorry this @future function is new to me and not sure where its needed.
I tried just added it to the class itself but get errors about it needing to be defined for some reason (read an article that if you simply add it to your class it could work "not with my luck anywhere i place it there is an error"
This is the class you provided with where i thought the call out could go. Any suggestions: