function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
chantspelchantspel 

Best way to store one unique entry containing user/pass

 Hi,


I'm working on a simple data push from salesforce into a custom application. The apex class that will recieve the "trigger" to do the data push needs to retrieve login paramaters, user/pass, to authenticate to our custom app.


What would be the best way/where to store this information. I can build a simple custom object to model the data, but I don't want to have the ability for multiple records to exist. There should be only 1 user/pass combination to be used by my apex class.

Hope this makes sense. Thanks.

Best Answer chosen by Admin (Salesforce Developers) 
Ispita_NavatarIspita_Navatar

You can handle this in two ways:-

1. Handle it at the database level - Have triggers (as validation rules will not serve the purpose) which will prevent user creation if the count of record having a composite key of(Username+ password) greated than 0.

2. You can also take it over to the business logic and have code in your apex class to prevent creation of user with same (username+ password) combination . You can issue a query and the result is greater than zero that means the key exists and the creation of the instance can be skipped.

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.

All Answers

Ispita_NavatarIspita_Navatar

You can handle this in two ways:-

1. Handle it at the database level - Have triggers (as validation rules will not serve the purpose) which will prevent user creation if the count of record having a composite key of(Username+ password) greated than 0.

2. You can also take it over to the business logic and have code in your apex class to prevent creation of user with same (username+ password) combination . You can issue a query and the result is greater than zero that means the key exists and the creation of the instance can be skipped.

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.

This was selected as the best answer
chantspelchantspel

Thank you for your reply. I think the trigger type validation should be sufficient for me needs. Just a quick followup on that. What would be the specific code that prevents the record creation?

 

For example:

 

trigger triggerName on ObjectName (trigger_events) {
//query to determine if record is unique
if(!unique)
//what code goes here to prevent record creation?
}
chantspelchantspel

I figured this out. Basically I decided to allow more than one record to exist but have field to mark the primary, and block any new users being created with primary checked. I know I'll need to and a similar rule for updating but this is the gist:

 

 

trigger uniqueOnly on API_USER__c (before insert) {
    
    API_USER__c[] users = Trigger.new;
    
    List<API_USER__c> apiUsers = [select Id from API_USER__c where Primary_user__c = true];
	if(apiUsers.size() >= 1){
		 for(API_USER__c user: users)
    	{
    		if(user.Primary_User__c){//primary checked
    			user.addError('Primary User already exists');
    		}	
   		} 
	}
}

 

 

Ispita_NavatarIspita_Navatar

Thats nice .. :)