1

I am trying to create a community user programmatically using the nforce node.js wrapper library. I'm running this as sysadmin privilege. This is what I am using:

var user = nforce.createSObject('User', {
        LastName: "Chen2",
        FirstName: "Andy2",
        Alias: "ac2",
        Username: "hello@gmail.com",
        Email: "hello@gmail.com",
        ProfileId: "00e36000000YIZ6AAO", // ProfileId for Customer Community User
        CommunityNickname: "nickname2",
        TimeZoneSidKey: "America/New_York",
        LocaleSidKey: "en_US",
        EmailEncodingKey: "UTF-8",
        LanguageLocaleKey: "en_US"
    });

This user object is then passed into org.insert(). But I am getting the following error:

{"errorCode":"INVALID_CROSS_REFERENCE_KEY","body":[{"message":"Cannot create a portal user without contact","errorCode":"INVALID_CROSS_REFERENCE_KEY","fields":["ContactId"]}],"statusCode":400}

I am not sure what to put in for the ContactId field here??

2

you can not create community user without contact id below is the code to create a community user

Id profileId = [select id from profile where name='Partner Community User'].id;


Account objAcc = new Account(name ='Test Account');
insert objAcc; 

Contact objContact = new Contact(LastName ='testCon',AccountId = objAcc.Id);
insert objContact;  

User user = new User(alias = 'TestUser', email='test123@test.com',
        emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
        localesidkey='en_US', profileid = profileId, country='United States',IsActive =true,
        ContactId = objContact.Id,
        timezonesidkey='America/Los_Angeles', username='tester@123.com');

insert user;
1

Was able to solve this:

// Inserts an sobject and returns promise with the object id (if successful)
function insertSObject(obj) {
    return new Promise(function(resolve) {
        org.insert({sobject: obj}).then(function(resp) {
            console.log(resp);
            // return id to the next .then call
            resolve(resp.id)
        }).catch(function(err) {
            console.error('--> unable to insert sobject');
            console.error('--> ' + JSON.stringify(err));
            throw err;
        });
    });
}

function insertAccount(accountName) {
    console.log('attempting to insert account');
    var acct = nforce.createSObject('Account', {
        Name: accountName
    });
    return insertSObject(acct);
}

function insertContact(firstName, lastName, accountId) {
    console.log('attempting to insert contact');
    var contact = nforce.createSObject('Contact', {
        FirstName: firstName,
        LastName: lastName,
        AccountId: accountId 
    });
    return insertSObject(contact);
}

function insertCommunityUser(firstName, lastName, alias, username, email, nickname, contactId) {
    console.log('attempting to insert user');
    var user = nforce.createSObject('User', {
        FirstName: firstName,
        LastName: lastName,
        Alias: alias,
        Username: username,
        Email: email,
        CommunityNickname: nickname,
        ContactId: contactId,
        ProfileId: "00e36000000YIZ6AAO",  // ProfileId for the Customer Community User profile
        TimeZoneSidKey: "America/New_York",
        LocaleSidKey: "en_US",
        EmailEncodingKey: "UTF-8",
        LanguageLocaleKey: "en_US"
    });
    return insertSObject(user);
}

insertAccount("AnotherAccount1").then(function(id) {
            return insertContact("Adam", "Murphy", id);
        }).then(function(id) {
            return insertCommunityUser("Another", "User", "au1", "au1@test.com", "abc@gmail.com", "aunick", id);
        }).then(function(id) {
            console.log("Successfully inserted userid " + id);
        }).catch(function(err) {
            console.log("Failed to insert community user. Err " + err);
        });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.