-
ChatterFeed
-
0Best Answers
-
0Likes Received
-
0Likes Given
-
6Questions
-
9Replies
Can you retrieve the Async limits from a class?
Hello,
I can see there is a REST API that can be called so we can see where we are currently with all of the Salesforce limits. https://instance.salesforce.com/services/data/v37.0/limits/
We would like to be able to track the limits periodically using a trigger. Inside this trigger we would like to view the Daily Async Apex Executions, and do something if the number left is below a threshold
The only method that I can see of grabbing this number real time is with that REST API. Is there another way to grab that number? e.g. is there an object that I can query using SOQL? Alternatively, am I wrong in thinking it's impossible to grab the results from that REST API in a trigger?
I have searched for all resources on the topic and have come up blank. If someone could shine a light on this it would be greatly appreciated
Thanks
Mark
I can see there is a REST API that can be called so we can see where we are currently with all of the Salesforce limits. https://instance.salesforce.com/services/data/v37.0/limits/
We would like to be able to track the limits periodically using a trigger. Inside this trigger we would like to view the Daily Async Apex Executions, and do something if the number left is below a threshold
The only method that I can see of grabbing this number real time is with that REST API. Is there another way to grab that number? e.g. is there an object that I can query using SOQL? Alternatively, am I wrong in thinking it's impossible to grab the results from that REST API in a trigger?
I have searched for all resources on the topic and have come up blank. If someone could shine a light on this it would be greatly appreciated
Thanks
Mark
- Mark Mulholland 3
- June 22, 2017
- Like
- 0
Accept json from 3rd party website
Hello
We are using a 3rd party to create eDocuments, and we would like to be able to store credentials for these documents in Salesforce.
I have created a REST API that accepts the json we will be expecting. However I have never set up a connection like this with a 3rd party before. All of the options I have found when searching is to do with fully integrating with a 3rd party and allowing users to login to that site. In my case I just want to make it so this 3rd party can send json via an API call.
I know this will require authorisation like OAuth, but I have no idea where to start. Could someone point me in the right direction please
Mark
We are using a 3rd party to create eDocuments, and we would like to be able to store credentials for these documents in Salesforce.
I have created a REST API that accepts the json we will be expecting. However I have never set up a connection like this with a 3rd party before. All of the options I have found when searching is to do with fully integrating with a 3rd party and allowing users to login to that site. In my case I just want to make it so this 3rd party can send json via an API call.
I know this will require authorisation like OAuth, but I have no idea where to start. Could someone point me in the right direction please
Mark
- Mark Mulholland 3
- January 16, 2017
- Like
- 0
URI in REST API test call not working
Hello,
I have a REST API class with a GET method which finds contacts based on 2 fields that can be passed in the URI. The class itself works when I test it in the Workbench so that doesn't seem to be an issue.
I have created a test class for this and the system.assertequals methods I try keep returning null values, as if the URI is formatted incorrectly. I have checked and rechecked the URI I have used and it should work. I have also checked that the test records I have in the test class are getting created and they are
Can one of you have a look at my test class and see if you can spot what I am missing please. Also I understand that the test class is very basic at the moment, once I fix this issue I will be able to make it more robust
Test Class:
REST API Class:
Thanks very much
I have a REST API class with a GET method which finds contacts based on 2 fields that can be passed in the URI. The class itself works when I test it in the Workbench so that doesn't seem to be an issue.
I have created a test class for this and the system.assertequals methods I try keep returning null values, as if the URI is formatted incorrectly. I have checked and rechecked the URI I have used and it should work. I have also checked that the test records I have in the test class are getting created and they are
Can one of you have a look at my test class and see if you can spot what I am missing please. Also I understand that the test class is very basic at the moment, once I fix this issue I will be able to make it more robust
Test Class:
@istest private class REST_API_IATA_and_Email_v1_Tests { //Declare variables for records that will be used private static Account Agency1; private static Contact Agency1Contact1; public static void createRecords(){ //get Account record Types map<string, id> mapRT = new map<string, id>(); for(RecordType RT : [SELECT id, DeveloperName, Name, SobjectType FROM RecordType WHERE SobjectType = 'Account']){ mapRT.put(RT.DeveloperName, RT.id); } //get Contact record Types map<string, id> ConRT = new map<string, id>(); for(RecordType RT : [SELECT id, DeveloperName, Name, SobjectType FROM RecordType WHERE SobjectType = 'Contact']){ ConRT.put(RT.DeveloperName, RT.id); } //insert Accounts to be used in test Agency1 = new Account(Name = 'Agency Account 1', IATA_Code__c = '12345678', RecordTypeId = mapRT.get('Agency')); insert Agency1; //insert Contacts to be used in test Agency1Contact1 = new Contact(AccountId = Agency1.id, firstName = 'Account1', lastName = 'Contact1', email = 'alex.drew@ttc2.com', RecordTypeId = conRT.get('Travel_Consultant')); insert Agency1Contact1; } @isTest //perform first doGet() call static void callAPIForTestResults(){ //create records createRecords(); test.startTest(); RestRequest req = new RestRequest(); RestResponse res = new RestResponse(); //make the fake callout to trigger the Rest API req.requestURI = 'https://cs86.salesforce.com/services/apexrest/v1/contacts?Email=alex.drew@ttc2.com'; req.httpMethod = 'GET'; RestContext.request = req; RestContext.response = res; //call the GET method on the Rest API class and envolk the ContactWrapper list REST_API_IATA_and_Email_v1.ContactWrapper results = REST_API_IATA_and_Email_v1.doGet(); system.assertEquals(1, results.acctList.size()); //check the GET method was successful with the URI it was given test.stopTest(); } }
REST API Class:
@RestResource(urlMapping='/v1/contacts/*') global with sharing class REST_API_IATA_and_Email_v1 { @HttpGet global static ContactWrapper doGet() { RestRequest req = RestContext.request; RestResponse res = RestContext.response; ContactWrapper response = new ContactWrapper(); String contactId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1); if(doSearch(contactId)) { searchContacts(req, res, response); } return response; } // If the item to the right of the last forward slash is "contacts", the request went to v1/contacts?Name=United // Else the request went to v1/contacts/<something>, which is not a search, but a specific entity private static boolean doSearch(String contactId) { if(contactId == 'contacts') { return true; } return false; } //If the request came to /v1/contacts, then we want to execute a search private static void searchContacts(RestRequest req, RestResponse res, ContactWrapper response) { //Use the RestRequest's params to fetch the IATA and Email parameters String searchTerm1 = req.params.get('IATA'); String searchTerm2 = req.params.get('Email'); if(searchTerm2 == null || searchTerm2 == ''){ if(searchTerm1 == null || searchTerm1 == '') { response.status = 'Error'; response.message = 'You must provide an IATA Code for your search term.'; res.StatusCode = 400; } else { List<Contact> searchResults = [SELECT Id, Name, Phone, Email, Account.IATA_Code__c, Account.Name FROM Contact WHERE Account.IATA_Code__c = :searchTerm1]; if(searchResults != null && searchResults.size() > 0) { response.acctList = searchResults; response.status = 'Success'; response.message = searchResults.size() + ' Contacts were found that matched your search term.'; } else { response.status = 'Error'; response.message = 'No Contacts where found based on that IATA Code, please search again.'; } } } else if(searchTerm1 == null || searchTerm1 == ''){ if(searchTerm2 == null || searchTerm2 == '') { response.status = 'Error'; response.message = 'You must provide an Email for your search term.'; res.StatusCode = 400; } else { List<Contact> searchResults = [SELECT Id, Name, Phone, Email, Account.IATA_Code__c, Account.Name FROM Contact WHERE Email = :searchTerm2]; if(searchResults != null && searchResults.size() > 0) { response.acctList = searchResults; response.status = 'Success'; response.message = searchResults.size() + ' Contacts were found that matched your search term.'; } else { response.status = 'Error'; response.message = 'No Contacts where found based on that Email, please search again.'; } } } else{ List<Contact> searchResults = [SELECT Id, Name, Phone, Email, Account.IATA_Code__c, Account.Name FROM Contact WHERE Account.IATA_Code__c = :searchTerm1 AND Email = :searchTerm2]; if(searchResults != null && searchResults.size() > 0) { response.acctList = searchResults; response.status = 'Success'; response.message = searchResults.size() + ' Contacts were found that matched your search terms.'; } else { response.status = 'Error'; response.message = 'No Contacts where found based on that IATA Code and email, please search again.'; } } } global class ContactWrapper { public List<Contact> acctList; public String status; public String message; public ContactWrapper(){ acctList = new List<Contact>(); } } }
Thanks very much
- Mark Mulholland 3
- October 05, 2016
- Like
- 0
very basic relationship question
If I have a variable of a SObject and I want to check a field on the parent record, how is that written?
I assumed it was in the format of "variable name"."lookup relationship name"."field name"
so if the variable is "Contact1" and I want to see the Account Name through that variable I would write
String AccountName = Contact1.Account.name;
If it's with custom objects then I would write it as
Passenger1.Account__r.name
If I try to do a system.assert on that then the string is returned as Null as if there is no relationship, but I have a feeling I am writing it wrong
I tried looking up the correct format but I could find no resource that has a variable, it's always in a SOQL query so it would just be Account__r.name
Can someone help please
I assumed it was in the format of "variable name"."lookup relationship name"."field name"
so if the variable is "Contact1" and I want to see the Account Name through that variable I would write
String AccountName = Contact1.Account.name;
If it's with custom objects then I would write it as
Passenger1.Account__r.name
If I try to do a system.assert on that then the string is returned as Null as if there is no relationship, but I have a feeling I am writing it wrong
I tried looking up the correct format but I could find no resource that has a variable, it's always in a SOQL query so it would just be Account__r.name
Can someone help please
- Mark Mulholland 3
- September 28, 2016
- Like
- 0
Errors with generating an Apex Rest URI callout for doGet. "attempt to de-reference a null object"
Hello,
I am creating a test class for a Rest API class I have with a GET method.
The class itself is pretty simple, It looks for 1 or 2 values for specific objects, queries the database and returns contacts that meet the criteria.
When I run the test class I am getting the error "System.NullPointerException: Attempt to de-reference a null object" pointing at the line with the URI callout and I cannot figure out why.
The class itself is here:
It's a bit long but mostly taken up by an IF statement checking how many and which variables were passed in the URI
The test class I have created is below
As I said the error I am getting is "System.NullPointerException: Attempt to de-reference a null object" and pointing at the URI call but I cannot see any problems with it. Can anyone help please
Thanks
I am creating a test class for a Rest API class I have with a GET method.
The class itself is pretty simple, It looks for 1 or 2 values for specific objects, queries the database and returns contacts that meet the criteria.
When I run the test class I am getting the error "System.NullPointerException: Attempt to de-reference a null object" pointing at the line with the URI callout and I cannot figure out why.
The class itself is here:
@RestResource(urlMapping='/v1/contacts/*') global with sharing class REST_API_IATA_and_Email_v1 { @HttpGet global static ContactWrapper doGet() { RestRequest req = RestContext.request; RestResponse res = RestContext.response; ContactWrapper response = new ContactWrapper(); String contactId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1); if(doSearch(contactId)) { searchContacts(req, res, response); } return response; } // If the item to the right of the last forward slash is "contacts", the request went to v1/contacts?Name=United // Else the request went to v1/contacts/<something>, which is not a search, but a specific entity private static boolean doSearch(String contactId) { if(contactId == 'contacts') { return true; } return false; } //If the request came to /v1/contacts, then we want to execute a search private static void searchContacts(RestRequest req, RestResponse res, ContactWrapper response) { //Use the RestRequest's params to fetch the IATA and Email parameters String searchTerm1 = req.params.get('IATA'); String searchTerm2 = req.params.get('Email'); if(searchTerm2 == null || searchTerm2 == ''){ if(searchTerm1 == null || searchTerm1 == '') { response.status = 'Error'; response.message = 'You must provide an IATA Code for your search term.'; res.StatusCode = 400; } else { String searchText = '%'+searchTerm1+'%'; List<Contact> searchResults = [SELECT Id, Name, Phone, Email, Account.IATA_Code__c, Account.Name FROM Contact WHERE Account.IATA_Code__c = :searchTerm1]; if(searchResults != null && searchResults.size() > 0) { response.acctList = searchResults; response.status = 'Success'; response.message = searchResults.size() + ' Contacts were found that matched your search term.'; } else { response.status = 'Error'; response.message = 'No Contacts where found based on that IATA Code, please search again.'; } } } else if(searchTerm1 == null || searchTerm1 == ''){ if(searchTerm2 == null || searchTerm2 == '') { response.status = 'Error'; response.message = 'You must provide an Email for your search term.'; res.StatusCode = 400; } else { String searchText = '%'+searchTerm2+'%'; List<Contact> searchResults = [SELECT Id, Name, Phone, Email, Account.IATA_Code__c, Account.Name FROM Contact WHERE Email = :searchTerm2]; if(searchResults != null && searchResults.size() > 0) { response.acctList = searchResults; response.status = 'Success'; response.message = searchResults.size() + ' Contacts were found that matched your search term.'; } else { response.status = 'Error'; response.message = 'No Contacts where found based on that Email, please search again.'; } } } else{ String searchText1 = '%'+searchTerm1+'%'; String searchText2 = '%'+searchTerm2+'%'; List<Contact> searchResults = [SELECT Id, Name, Phone, Email, Account.IATA_Code__c, Account.Name FROM Contact WHERE Account.IATA_Code__c = :searchTerm1 AND Email = :searchTerm2]; if(searchResults != null && searchResults.size() > 0) { response.acctList = searchResults; response.status = 'Success'; response.message = searchResults.size() + ' Contacts were found that matched your search terms.'; } else { response.status = 'Error'; response.message = 'No Contacts where found based on that IATA Code and email, please search again.'; } } } global class ContactWrapper { public List<Contact> acctList; public String status; public String message; public ContactWrapper(){ acctList = new List<Contact>(); } } }
It's a bit long but mostly taken up by an IF statement checking how many and which variables were passed in the URI
The test class I have created is below
@istest private class REST_API_IATA_and_Email_v1_Tests { //Declare variables for records that will be used private static Account Agency1; private static Contact Agency1Contact1; public static void createRecords(){ //get Account record Types map<string, id> mapRT = new map<string, id>(); for(RecordType RT : [SELECT id, DeveloperName, Name, SobjectType FROM RecordType WHERE SobjectType = 'Account']){ mapRT.put(RT.DeveloperName, RT.id); } //get Contact record Types map<string, id> ConRT = new map<string, id>(); for(RecordType RT : [SELECT id, DeveloperName, Name, SobjectType FROM RecordType WHERE SobjectType = 'Contact']){ ConRT.put(RT.DeveloperName, RT.id); } //insert Accounts to be used in test Agency1 = new Account(Name = 'Agency Account 1', IATA_Code__c = '12345678', RecordTypeId = mapRT.get('Agency')); insert Agency1; //insert Contacts to be used in test Agency1Contact1 = new Contact(firstName = 'Account1', lastName = 'Contact1', email = 'alex.drew@ttc2.com', RecordTypeId = conRT.get('Travel_Consultant')); insert Agency1Contact1; //There are more records, most have been removed to save space } @isTest //perform first doGet() call static void callAPIForTestResults(){ //create records createRecords(); test.startTest(); RestRequest req = RestContext.request; RestResponse res = RestContext.response; req.requestURI = 'https://XXXX.salesforce.com/services/apexrest/v1/contacts?IATA=12345678&email=alex.drew@ttc2.com'; req.httpMethod = 'GET'; RestContext.request = req; RestContext.response = res; REST_API_IATA_and_Email_v1.ContactWrapper results = REST_API_IATA_and_Email_v1.doGet(); system.assertEquals('success', results.status); system.assertEquals(1, results.acctList.size()); test.stopTest(); } }
As I said the error I am getting is "System.NullPointerException: Attempt to de-reference a null object" and pointing at the URI call but I cannot see any problems with it. Can anyone help please
Thanks
- Mark Mulholland 3
- September 22, 2016
- Like
- 0
SOQL search using a list. Want to use the equals operator with an Apex variable
Hello all,
I am trying to create a simple query against Accounts and Contacts, trying to see if a contact has a selected Email address and the parent Account has a specific IATA number. I am getting into some basic API creation and am using files from a Developerforce conference from 2 years ago. I modified the code to use field specific to my org
The SOQL query returns values only if I use the LIKE operator and doesn't return any results if I use the equals operator. See below for what I mean
Because I used LIKE in the SOQL query I get results returned. However if I modify the code as you can see in the next fragment I get no results
However, if you do not use a List for the results and simply use a String, then you can use the equals operator.
Is this expected behaviour? and if so is there any way I can made the search more selective? or make it pseudo equals?
Thanks
I am trying to create a simple query against Accounts and Contacts, trying to see if a contact has a selected Email address and the parent Account has a specific IATA number. I am getting into some basic API creation and am using files from a Developerforce conference from 2 years ago. I modified the code to use field specific to my org
The SOQL query returns values only if I use the LIKE operator and doesn't return any results if I use the equals operator. See below for what I mean
String searchText = '%'+searchTerm1+'%'; List<Contact> searchResults = [SELECT Id, Name, Phone, Email, Account.IATA_Code__c FROM Contact WHERE Account.IATA_Code__c LIKE :searchText]; if(searchResults != null && searchResults.size() > 0) { response.acctList = searchResults; response.status = 'Success'; response.message = searchResults.size() + ' Contacts were found that matched your search term.'; } else { response.status = 'Error'; response.message = 'No Contacts where found based on that IATA Code, please search again.'; }
Because I used LIKE in the SOQL query I get results returned. However if I modify the code as you can see in the next fragment I get no results
String searchText = '%'+searchTerm1+'%'; List<Contact> searchResults = [SELECT Id, Name, Phone, Email, Account.IATA_Code__c FROM Contact WHERE Account.IATA_Code__c = :searchText]; if(searchResults != null && searchResults.size() > 0) { response.acctList = searchResults; response.status = 'Success'; response.message = searchResults.size() + ' Contacts were found that matched your search term.'; } else { response.status = 'Error'; response.message = 'No Contacts where found based on that IATA Code, please search again.'; }
However, if you do not use a List for the results and simply use a String, then you can use the equals operator.
Is this expected behaviour? and if so is there any way I can made the search more selective? or make it pseudo equals?
Thanks
- Mark Mulholland 3
- September 19, 2016
- Like
- 0
Can you retrieve the Async limits from a class?
Hello,
I can see there is a REST API that can be called so we can see where we are currently with all of the Salesforce limits. https://instance.salesforce.com/services/data/v37.0/limits/
We would like to be able to track the limits periodically using a trigger. Inside this trigger we would like to view the Daily Async Apex Executions, and do something if the number left is below a threshold
The only method that I can see of grabbing this number real time is with that REST API. Is there another way to grab that number? e.g. is there an object that I can query using SOQL? Alternatively, am I wrong in thinking it's impossible to grab the results from that REST API in a trigger?
I have searched for all resources on the topic and have come up blank. If someone could shine a light on this it would be greatly appreciated
Thanks
Mark
I can see there is a REST API that can be called so we can see where we are currently with all of the Salesforce limits. https://instance.salesforce.com/services/data/v37.0/limits/
We would like to be able to track the limits periodically using a trigger. Inside this trigger we would like to view the Daily Async Apex Executions, and do something if the number left is below a threshold
The only method that I can see of grabbing this number real time is with that REST API. Is there another way to grab that number? e.g. is there an object that I can query using SOQL? Alternatively, am I wrong in thinking it's impossible to grab the results from that REST API in a trigger?
I have searched for all resources on the topic and have come up blank. If someone could shine a light on this it would be greatly appreciated
Thanks
Mark
- Mark Mulholland 3
- June 22, 2017
- Like
- 0
URI in REST API test call not working
Hello,
I have a REST API class with a GET method which finds contacts based on 2 fields that can be passed in the URI. The class itself works when I test it in the Workbench so that doesn't seem to be an issue.
I have created a test class for this and the system.assertequals methods I try keep returning null values, as if the URI is formatted incorrectly. I have checked and rechecked the URI I have used and it should work. I have also checked that the test records I have in the test class are getting created and they are
Can one of you have a look at my test class and see if you can spot what I am missing please. Also I understand that the test class is very basic at the moment, once I fix this issue I will be able to make it more robust
Test Class:
REST API Class:
Thanks very much
I have a REST API class with a GET method which finds contacts based on 2 fields that can be passed in the URI. The class itself works when I test it in the Workbench so that doesn't seem to be an issue.
I have created a test class for this and the system.assertequals methods I try keep returning null values, as if the URI is formatted incorrectly. I have checked and rechecked the URI I have used and it should work. I have also checked that the test records I have in the test class are getting created and they are
Can one of you have a look at my test class and see if you can spot what I am missing please. Also I understand that the test class is very basic at the moment, once I fix this issue I will be able to make it more robust
Test Class:
@istest private class REST_API_IATA_and_Email_v1_Tests { //Declare variables for records that will be used private static Account Agency1; private static Contact Agency1Contact1; public static void createRecords(){ //get Account record Types map<string, id> mapRT = new map<string, id>(); for(RecordType RT : [SELECT id, DeveloperName, Name, SobjectType FROM RecordType WHERE SobjectType = 'Account']){ mapRT.put(RT.DeveloperName, RT.id); } //get Contact record Types map<string, id> ConRT = new map<string, id>(); for(RecordType RT : [SELECT id, DeveloperName, Name, SobjectType FROM RecordType WHERE SobjectType = 'Contact']){ ConRT.put(RT.DeveloperName, RT.id); } //insert Accounts to be used in test Agency1 = new Account(Name = 'Agency Account 1', IATA_Code__c = '12345678', RecordTypeId = mapRT.get('Agency')); insert Agency1; //insert Contacts to be used in test Agency1Contact1 = new Contact(AccountId = Agency1.id, firstName = 'Account1', lastName = 'Contact1', email = 'alex.drew@ttc2.com', RecordTypeId = conRT.get('Travel_Consultant')); insert Agency1Contact1; } @isTest //perform first doGet() call static void callAPIForTestResults(){ //create records createRecords(); test.startTest(); RestRequest req = new RestRequest(); RestResponse res = new RestResponse(); //make the fake callout to trigger the Rest API req.requestURI = 'https://cs86.salesforce.com/services/apexrest/v1/contacts?Email=alex.drew@ttc2.com'; req.httpMethod = 'GET'; RestContext.request = req; RestContext.response = res; //call the GET method on the Rest API class and envolk the ContactWrapper list REST_API_IATA_and_Email_v1.ContactWrapper results = REST_API_IATA_and_Email_v1.doGet(); system.assertEquals(1, results.acctList.size()); //check the GET method was successful with the URI it was given test.stopTest(); } }
REST API Class:
@RestResource(urlMapping='/v1/contacts/*') global with sharing class REST_API_IATA_and_Email_v1 { @HttpGet global static ContactWrapper doGet() { RestRequest req = RestContext.request; RestResponse res = RestContext.response; ContactWrapper response = new ContactWrapper(); String contactId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1); if(doSearch(contactId)) { searchContacts(req, res, response); } return response; } // If the item to the right of the last forward slash is "contacts", the request went to v1/contacts?Name=United // Else the request went to v1/contacts/<something>, which is not a search, but a specific entity private static boolean doSearch(String contactId) { if(contactId == 'contacts') { return true; } return false; } //If the request came to /v1/contacts, then we want to execute a search private static void searchContacts(RestRequest req, RestResponse res, ContactWrapper response) { //Use the RestRequest's params to fetch the IATA and Email parameters String searchTerm1 = req.params.get('IATA'); String searchTerm2 = req.params.get('Email'); if(searchTerm2 == null || searchTerm2 == ''){ if(searchTerm1 == null || searchTerm1 == '') { response.status = 'Error'; response.message = 'You must provide an IATA Code for your search term.'; res.StatusCode = 400; } else { List<Contact> searchResults = [SELECT Id, Name, Phone, Email, Account.IATA_Code__c, Account.Name FROM Contact WHERE Account.IATA_Code__c = :searchTerm1]; if(searchResults != null && searchResults.size() > 0) { response.acctList = searchResults; response.status = 'Success'; response.message = searchResults.size() + ' Contacts were found that matched your search term.'; } else { response.status = 'Error'; response.message = 'No Contacts where found based on that IATA Code, please search again.'; } } } else if(searchTerm1 == null || searchTerm1 == ''){ if(searchTerm2 == null || searchTerm2 == '') { response.status = 'Error'; response.message = 'You must provide an Email for your search term.'; res.StatusCode = 400; } else { List<Contact> searchResults = [SELECT Id, Name, Phone, Email, Account.IATA_Code__c, Account.Name FROM Contact WHERE Email = :searchTerm2]; if(searchResults != null && searchResults.size() > 0) { response.acctList = searchResults; response.status = 'Success'; response.message = searchResults.size() + ' Contacts were found that matched your search term.'; } else { response.status = 'Error'; response.message = 'No Contacts where found based on that Email, please search again.'; } } } else{ List<Contact> searchResults = [SELECT Id, Name, Phone, Email, Account.IATA_Code__c, Account.Name FROM Contact WHERE Account.IATA_Code__c = :searchTerm1 AND Email = :searchTerm2]; if(searchResults != null && searchResults.size() > 0) { response.acctList = searchResults; response.status = 'Success'; response.message = searchResults.size() + ' Contacts were found that matched your search terms.'; } else { response.status = 'Error'; response.message = 'No Contacts where found based on that IATA Code and email, please search again.'; } } } global class ContactWrapper { public List<Contact> acctList; public String status; public String message; public ContactWrapper(){ acctList = new List<Contact>(); } } }
Thanks very much
- Mark Mulholland 3
- October 05, 2016
- Like
- 0
very basic relationship question
If I have a variable of a SObject and I want to check a field on the parent record, how is that written?
I assumed it was in the format of "variable name"."lookup relationship name"."field name"
so if the variable is "Contact1" and I want to see the Account Name through that variable I would write
String AccountName = Contact1.Account.name;
If it's with custom objects then I would write it as
Passenger1.Account__r.name
If I try to do a system.assert on that then the string is returned as Null as if there is no relationship, but I have a feeling I am writing it wrong
I tried looking up the correct format but I could find no resource that has a variable, it's always in a SOQL query so it would just be Account__r.name
Can someone help please
I assumed it was in the format of "variable name"."lookup relationship name"."field name"
so if the variable is "Contact1" and I want to see the Account Name through that variable I would write
String AccountName = Contact1.Account.name;
If it's with custom objects then I would write it as
Passenger1.Account__r.name
If I try to do a system.assert on that then the string is returned as Null as if there is no relationship, but I have a feeling I am writing it wrong
I tried looking up the correct format but I could find no resource that has a variable, it's always in a SOQL query so it would just be Account__r.name
Can someone help please
- Mark Mulholland 3
- September 28, 2016
- Like
- 0
Errors with generating an Apex Rest URI callout for doGet. "attempt to de-reference a null object"
Hello,
I am creating a test class for a Rest API class I have with a GET method.
The class itself is pretty simple, It looks for 1 or 2 values for specific objects, queries the database and returns contacts that meet the criteria.
When I run the test class I am getting the error "System.NullPointerException: Attempt to de-reference a null object" pointing at the line with the URI callout and I cannot figure out why.
The class itself is here:
It's a bit long but mostly taken up by an IF statement checking how many and which variables were passed in the URI
The test class I have created is below
As I said the error I am getting is "System.NullPointerException: Attempt to de-reference a null object" and pointing at the URI call but I cannot see any problems with it. Can anyone help please
Thanks
I am creating a test class for a Rest API class I have with a GET method.
The class itself is pretty simple, It looks for 1 or 2 values for specific objects, queries the database and returns contacts that meet the criteria.
When I run the test class I am getting the error "System.NullPointerException: Attempt to de-reference a null object" pointing at the line with the URI callout and I cannot figure out why.
The class itself is here:
@RestResource(urlMapping='/v1/contacts/*') global with sharing class REST_API_IATA_and_Email_v1 { @HttpGet global static ContactWrapper doGet() { RestRequest req = RestContext.request; RestResponse res = RestContext.response; ContactWrapper response = new ContactWrapper(); String contactId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1); if(doSearch(contactId)) { searchContacts(req, res, response); } return response; } // If the item to the right of the last forward slash is "contacts", the request went to v1/contacts?Name=United // Else the request went to v1/contacts/<something>, which is not a search, but a specific entity private static boolean doSearch(String contactId) { if(contactId == 'contacts') { return true; } return false; } //If the request came to /v1/contacts, then we want to execute a search private static void searchContacts(RestRequest req, RestResponse res, ContactWrapper response) { //Use the RestRequest's params to fetch the IATA and Email parameters String searchTerm1 = req.params.get('IATA'); String searchTerm2 = req.params.get('Email'); if(searchTerm2 == null || searchTerm2 == ''){ if(searchTerm1 == null || searchTerm1 == '') { response.status = 'Error'; response.message = 'You must provide an IATA Code for your search term.'; res.StatusCode = 400; } else { String searchText = '%'+searchTerm1+'%'; List<Contact> searchResults = [SELECT Id, Name, Phone, Email, Account.IATA_Code__c, Account.Name FROM Contact WHERE Account.IATA_Code__c = :searchTerm1]; if(searchResults != null && searchResults.size() > 0) { response.acctList = searchResults; response.status = 'Success'; response.message = searchResults.size() + ' Contacts were found that matched your search term.'; } else { response.status = 'Error'; response.message = 'No Contacts where found based on that IATA Code, please search again.'; } } } else if(searchTerm1 == null || searchTerm1 == ''){ if(searchTerm2 == null || searchTerm2 == '') { response.status = 'Error'; response.message = 'You must provide an Email for your search term.'; res.StatusCode = 400; } else { String searchText = '%'+searchTerm2+'%'; List<Contact> searchResults = [SELECT Id, Name, Phone, Email, Account.IATA_Code__c, Account.Name FROM Contact WHERE Email = :searchTerm2]; if(searchResults != null && searchResults.size() > 0) { response.acctList = searchResults; response.status = 'Success'; response.message = searchResults.size() + ' Contacts were found that matched your search term.'; } else { response.status = 'Error'; response.message = 'No Contacts where found based on that Email, please search again.'; } } } else{ String searchText1 = '%'+searchTerm1+'%'; String searchText2 = '%'+searchTerm2+'%'; List<Contact> searchResults = [SELECT Id, Name, Phone, Email, Account.IATA_Code__c, Account.Name FROM Contact WHERE Account.IATA_Code__c = :searchTerm1 AND Email = :searchTerm2]; if(searchResults != null && searchResults.size() > 0) { response.acctList = searchResults; response.status = 'Success'; response.message = searchResults.size() + ' Contacts were found that matched your search terms.'; } else { response.status = 'Error'; response.message = 'No Contacts where found based on that IATA Code and email, please search again.'; } } } global class ContactWrapper { public List<Contact> acctList; public String status; public String message; public ContactWrapper(){ acctList = new List<Contact>(); } } }
It's a bit long but mostly taken up by an IF statement checking how many and which variables were passed in the URI
The test class I have created is below
@istest private class REST_API_IATA_and_Email_v1_Tests { //Declare variables for records that will be used private static Account Agency1; private static Contact Agency1Contact1; public static void createRecords(){ //get Account record Types map<string, id> mapRT = new map<string, id>(); for(RecordType RT : [SELECT id, DeveloperName, Name, SobjectType FROM RecordType WHERE SobjectType = 'Account']){ mapRT.put(RT.DeveloperName, RT.id); } //get Contact record Types map<string, id> ConRT = new map<string, id>(); for(RecordType RT : [SELECT id, DeveloperName, Name, SobjectType FROM RecordType WHERE SobjectType = 'Contact']){ ConRT.put(RT.DeveloperName, RT.id); } //insert Accounts to be used in test Agency1 = new Account(Name = 'Agency Account 1', IATA_Code__c = '12345678', RecordTypeId = mapRT.get('Agency')); insert Agency1; //insert Contacts to be used in test Agency1Contact1 = new Contact(firstName = 'Account1', lastName = 'Contact1', email = 'alex.drew@ttc2.com', RecordTypeId = conRT.get('Travel_Consultant')); insert Agency1Contact1; //There are more records, most have been removed to save space } @isTest //perform first doGet() call static void callAPIForTestResults(){ //create records createRecords(); test.startTest(); RestRequest req = RestContext.request; RestResponse res = RestContext.response; req.requestURI = 'https://XXXX.salesforce.com/services/apexrest/v1/contacts?IATA=12345678&email=alex.drew@ttc2.com'; req.httpMethod = 'GET'; RestContext.request = req; RestContext.response = res; REST_API_IATA_and_Email_v1.ContactWrapper results = REST_API_IATA_and_Email_v1.doGet(); system.assertEquals('success', results.status); system.assertEquals(1, results.acctList.size()); test.stopTest(); } }
As I said the error I am getting is "System.NullPointerException: Attempt to de-reference a null object" and pointing at the URI call but I cannot see any problems with it. Can anyone help please
Thanks
- Mark Mulholland 3
- September 22, 2016
- Like
- 0
SOQL search using a list. Want to use the equals operator with an Apex variable
Hello all,
I am trying to create a simple query against Accounts and Contacts, trying to see if a contact has a selected Email address and the parent Account has a specific IATA number. I am getting into some basic API creation and am using files from a Developerforce conference from 2 years ago. I modified the code to use field specific to my org
The SOQL query returns values only if I use the LIKE operator and doesn't return any results if I use the equals operator. See below for what I mean
Because I used LIKE in the SOQL query I get results returned. However if I modify the code as you can see in the next fragment I get no results
However, if you do not use a List for the results and simply use a String, then you can use the equals operator.
Is this expected behaviour? and if so is there any way I can made the search more selective? or make it pseudo equals?
Thanks
I am trying to create a simple query against Accounts and Contacts, trying to see if a contact has a selected Email address and the parent Account has a specific IATA number. I am getting into some basic API creation and am using files from a Developerforce conference from 2 years ago. I modified the code to use field specific to my org
The SOQL query returns values only if I use the LIKE operator and doesn't return any results if I use the equals operator. See below for what I mean
String searchText = '%'+searchTerm1+'%'; List<Contact> searchResults = [SELECT Id, Name, Phone, Email, Account.IATA_Code__c FROM Contact WHERE Account.IATA_Code__c LIKE :searchText]; if(searchResults != null && searchResults.size() > 0) { response.acctList = searchResults; response.status = 'Success'; response.message = searchResults.size() + ' Contacts were found that matched your search term.'; } else { response.status = 'Error'; response.message = 'No Contacts where found based on that IATA Code, please search again.'; }
Because I used LIKE in the SOQL query I get results returned. However if I modify the code as you can see in the next fragment I get no results
String searchText = '%'+searchTerm1+'%'; List<Contact> searchResults = [SELECT Id, Name, Phone, Email, Account.IATA_Code__c FROM Contact WHERE Account.IATA_Code__c = :searchText]; if(searchResults != null && searchResults.size() > 0) { response.acctList = searchResults; response.status = 'Success'; response.message = searchResults.size() + ' Contacts were found that matched your search term.'; } else { response.status = 'Error'; response.message = 'No Contacts where found based on that IATA Code, please search again.'; }
However, if you do not use a List for the results and simply use a String, then you can use the equals operator.
Is this expected behaviour? and if so is there any way I can made the search more selective? or make it pseudo equals?
Thanks
- Mark Mulholland 3
- September 19, 2016
- Like
- 0