How to insert a lead through forcetk in salesforce - javascript

js to get data from salesforce .The following function fetches data of a current logged In user from salesforce .
function init() {
// Get an instance of the REST API client and set the session ID
var client = new forcetk.Client();
client.setSessionToken(getValueFromCookie("sid"));
// Retrieve the data representing the current user
client.currentUser(function(response){
var user = response;
console.log(user);
// Find cases that belong to the current user
client.query("SELECT Name FROM User", function(response){
console.log(response);
Tinycon.setBubble(response.totalSize);
});
});
}
Now i want to insert a lead in salesforce through forcetk in current user which is logged In.

Related

Why is firebase creating a random identifier between parent and child?

I'm playing around with a firebase web app and having some difficulty diagnosing where something is coming from.
I am trying to simply push some data to my project under the heading of the uid created when authentication takes place. The authentication works fine and it is returning the uid correctly however, when values are passed it seems to be adding a second layer before the actual values.
function registerAccount() {
var firebase = app_firebase;
var firebaseRef = app_firebase.database(); //database reference
var user = firebase.auth().currentUser;
uid = user.uid;
var ref = firebaseRef.ref('User').child(uid); //referencing node
var userName = document.getElementById("txtUsernameInput").value;
if (user) {
// User is signed in.
var data = { //data being added
Username: userName,
}
window.location = 'myHome.aspx';
} else {
// No user is signed in.
console.log("Cannot get UID");
}
ref.push(data);
}
I am expecting the data entry to show with the child of user to be the uid taken from the authentication (this is working) then have the passed values immediately in the uid without the seemingly auto generated child between the uid and the values.
Image shows the unwanted field being generated
[See here][1]
André Kool's suggestion in a comment:
Change ref.push(data); to ref.set(data);
worked.

Firebase Realtime Database Show Users Name - Web Page

I have to show data belonging to a user that logged in.
My database structure is as follows:
Users/Drivers/Uid/name,phone...
How can I show the data of a user that logged in by using JavaScript?
If you have a structure like that, you can display all data for the currently logged in user with:
var currentUser = firebase.auth().currentUser;
var userRef = firebase.database().ref("Users/Drivers").child(currentUser.uid);
userRef.on("value").then(function(snapshot) {
console.log(snapshot.val());
})

#ionic change the data key in firebase

I'm trying to update/add data on firebase. I used the Facebook login and I want to use the UserID as a key for the new data aded.
(check pict below)
The userID that I want to use it:
I want to replace that key with the userID:
fblogin(){
this.facebook.login(['email'])
.then(res=> {
const fc = firebase.auth.FacebookAuthProvider.credential(res.authResponse.accessToken);
firebase.auth().signInWithCredential(fc)
.then(fs => {
this.facebook.api('me?fields=id,name,email,first_name,picture.width(720).height(720).as(picture_large)', []).then(profile => {
this.newuser = {name: profile['first_name'] ,email: profile['email'],picture: profile['picture_large']['data']['url'],phone:''}
this.navCtrl.push(HomePage);
console.log(fs.uid);
this.db.list('/users/'+ fs.uid).update(this.newuser);
});
I got this error in compilation:
supplied parameters do not matchany signature of call target
In this line: this.db.list('/users/'+ fs.uid).update(this.newuser);
Any help?
The FB error looks correct. You cant update on the uid as the user has been saved with a unique FB id
You do not show the code that created the users record in the database, but what i think you want to do is set and object when you first save the users record. However this could be an issue because the user could be saved before the return of the uid. I cant tell with your code snippet. Regardless, I will write the code that i think will work if the users/ record is created at the time that of registration.
The service
async signupUser(email: string, password: string) {
try {
const result = await this.afA.auth.createUserWithEmailAndPassword(email, password);
return result;
} catch (err) {
console.log('error', err);
}
}
So this initially creates a user without facebook, The key here is that the users FB uid was created and is held in the returned result
The component
this.authData.signupUser(email,password).then(userData => {
console.log(userData) // <- this is result
}
Then we create a record in FB with the uid returned
this.db.object(`users/${userData.uid}/`).set(data);
.set(data) is whatever data you want to save in the users/uid namespace.
So basically you need to create that user table with its uid namespace when the user first registers. Then you can update the user with the uid returned from the facebook fs.uid
With your current code you could find the user based on the email ( because the email should be unique to all users) and then update ...
with lodash is it just
let foundUser = find(this.db.list('users'),{ 'email' : fs.email }
// and then update based on the object key
this.db.list('/users/'+ Object.keys(foundUser)).update(this.newuser);
i fixed the problem by using:
this.db.object('/users/'+ fs.uid).update(this.newuser);
instead of :
this.db.list('/users/'+ fs.uid).update(this.newuser);
And it works correctly !
Thanks all for help.

Append item to array of fetched item Parse Cloud Code

Javascript
Parse Cloud Code
In my app a User can request to join another Users account. The pseudo code for this is as follows:
send up the username of the account we want to join
search to see if the username exist in the database
if no return
if yes create a new AccountRequest object
add the newly created AccountRequest object to the user we were searching for.
I'm able to do steps 1-4 however I'm having trouble accomplishing #5.
Here is my code that I'm working with.
Parse.Cloud.define("sendAccountAdditionRequest", function(request, response) {
console.log("-sendAccountAdditionRequest");
// Create the query on the User class
var query = new Parse.Query(Parse.User);
// Set our parameters to search on
query.equalTo("username", request.params.adminUsername);
// Perform search
query.find({
// We found a matching user
success: function(results) {
Parse.Cloud.useMasterKey();
var fetchedUser = results[0]
console.log("--found user");
console.log("--creating new AccountRequest");
// Create a new instance of AccountRequest
var AccountRequestClass = Parse.Object.extend("AccountRequest");
var accountRequest = new AccountRequestClass();
// Set the User it is related to. Our User class has a 1..n relationship to AccountRequest
accountRequest.set("user", fetchedUser);
// Set out other values for the AccountRequest
accountRequest.set("phone", request.params.adminUsername);
accountRequest.set("dateSent", Date.now());
// Save the new AccountRequest
accountRequest.save(null,{
// Once the new AccountRequest has been saved we need to add it to our fetched User
success:function(savedRequest) {
console.log("---adding AccountRequest to fetched User");
//
// === This is where stuff breaks
//
var requestRelation = fetchedUser.relation("accountRequest");
// Now we need to add the new AccountRequest to the fetched User. The accountRequest property for a User is array...I'm not sure how I'm suppose to append a new item to that. I think I need to somehow cast results[0] to a User object? Maybe?
requestRelation.add(savedRequest);
// We perform a save on the User now that the accountRequest has been added.
fetchedUser.save(null, {
success:function(response) {
console.log("----AccountRequest complete");
response.success("A request has been sent!");
},
error:function(error) {
// This is printing out: ParseUser { _objCount: 2, className: '_User', id: 'QjhQxWCFWs' }
console.log(error);
response.error(error);
}
});
//
// ================================
//
//response.success("A request has been sent!");
},
// There was an error saving the new AccountRequest
error:function(error) {
response.error(error);
}
});
},
// We were not able to find an account with the supplied username
error: function() {
response.error("An account with that number does not exist. Please tell your administrator to sign up before you are added.");
}
});
});
I believe my problem is fetching the accountRequest relation from the returned search results from the initial query. Another thing to note is that I have not created the accountRequest property of my PFUser, my understanding is that this will automatically be done by Parse when I perform the save function.
Whether the accountRequest will get created for you depends on whether class level permissions are set to allow the client application to add fields. This is probably set to NO for your Parse.User.
But the relation on Parse.User isn't needed anyway, since you've already established it on the AccountRequest class. Given a user, you can get it's accountRequests with:
PFQuery *query = [PFQuery queryWithClassName:#"AccountRequest"];
[query whereKey:#"user" equalTo:aUser];
query.find()...
This is equivalent to getting the relation on User, getting its query and running it.
A couple notes about your code: (a) findOne will save you a line when you know there's just one result, (b) using Parse.Promise would really tidy things up.

AngularFire simple login

I have a FireBase db with a users store. I also use simple login email/pw. In the User store I save some extra info of a user - e.g. the lastlogin date. This is my workflow - from registering to logging in:
I register a user;
when registered it is added to the simple login email/pw sote;
I also add the registered user (includng the id returned from the simplelogin) in the users store. It is stored under a Firebase generated unique key.
I log in as that new user
When successful I get a user object from the simplelogin store:
email-"testuser1#test.com"
firebaseAuthToken-"eyJ0eXAiOiJKV1QiLCJhbGci...SoXkddR3A88vAkENCy5ilIk"
id-"46"
isTemporaryPassword-false
md5_hash-"6a4b6cb2045fd55f706eaebd6ab5d4f7"
provider-"password"
uid-"simplelogin:46"
Now I want to update the corresponding user in the User store - e.g. set the lastlogin key to now. But I only can update that user when I know the Firebase generated key it's under. How can I access that key?
The only other way to identify the user in the Users store is by retrieving all users in the Users store, looping through all of them and checking : does the current id key value match the id key value of the logged-in user. Looks a bit clumsy to me but I fear this is the only way I can do lookups with firebase?
When you save a registered user you should save them by their uid rather than a generated id. This way when the user logs back in we'll user the uid to get the user from the users node.
var fbRef = new Firebase('https://<YOUR-FIREBASE>.firebaseio.com');
var auth = new FirebaseSimpleLogin(fbRef, function(error, user) {
if (error) {
console.error(error);
} else if (user) {
// when a user logs in we can update their lastLogin here
// set the key to the uid for the user
// this would look like: https://myapp.firebaseio.com/users/1
fbRef.child('users').child(user.uid).update({
lastLogin: Firebase.ServerValue.TIMESTAMP // the time they logged in
});
}
});
// here when we create a user we will set the key to the uid under the users node
auth.createUser(email, password, function(error, user) {
// if there is no error
if (!error) {
// go to the users node, then set a location at the user's uid
// this would look like: https://myapp.firebaseio.com/users/1
fbRef.child('users').child(user.uid).set(user);
}
});
As the users are created our users node will look like this:

Categories

Resources