1

I am looking to get the users record type from a certain permission set. I need to first get the userId and then I have a picklist on the user field which can be used to communicate with a crosswalk table to link to the record type.

I have a feeling I'm supposed to use $user but I keep getting errors. Also, program__c is a picklist so I'm not sure if that returns a string.

<script src="/soap/ajax/30.0/connection.js"></script>

<script src="/soap/ajax/30.0/apex.js"></script>

<script type="text/javascript">
    setTimeout(function() {

        var query1 = sforce.connection.query(select program__c from user where id =: $user.userid);
        var query1Result = query1.getArray("records");

        var query2 = sforce.connection.query(select Program_record_type__c from  Fields_to_Program__c where program__c = query1Result[0].program__c);

        var query2Result = query2.getArray("records");

        sforce.one.createRecord('Activity__c', query2Result[0]);

        if(typeof sforce != 'undefined' && sforce != null) {
            sforce.one.createRecord('Activity__c', query2Result[0]);
        } else {
            window.open('/a01/e', '_self');
        }

    }, 3);
</script>
6

You can't use "bind expressions" in JavaScript. You have to actually use normal strings. This should be approximately correct:

setTimeout(function() {

    var query1 = sforce.connection.query("select program__c from user where id = '{!$user.userid}'");
    var query1Result = query1.getArray("records");
    var query2 = sforce.connection.query("select Program_record_type__c from  Fields_to_Program__c where program__c = '"+query1Result[0].program__c+"'");
    var query2Result = query2.getArray("records");
    sforce.one.createRecord('Activity__c', query2Result[0]);
    if(typeof sforce != 'undefined' && sforce != null) {
        sforce.one.createRecord('Activity__c', query2Result[0]);
    } else {
        window.open('/a01/e', '_self');
    }
}, 3);

You should note that JavaScript is also case sensitive, so make sure your field names, when referenced in JavaScript, are exactly correct.

  • I'm getting this error in the debugger. Also I needed to change {!$user.userid} to {!user.id} to not get an error: faultcode:'sf:INVALID_SESSION_ID', faultstring:'INVALID_SESSION_ID: Invalid Session ID found in SessionHeader: Illegal Session', detail:{UnexpectedErrorFault:{exceptionCode:'INVALID_SESSION_ID', exceptionMessage:'Invalid Session ID found in SessionHeader: Illegal Session', }, }, } – tjcinnamon Aug 11 '16 at 19:47
  • 1
    @tjcinnamon See this answer to solve INVALID_SESSION_ID. – sfdcfox Aug 11 '16 at 19:52
  • very close now. Looks like I can't pass a var to sforce.one.createRecord(EntityName[,record type id]). Do I need to cast as a string? Cannot read property 'createRecord' of undefined – tjcinnamon Aug 11 '16 at 20:08
  • @tjcinnamon sforce.one is for Salesforce1. If you're not using it, then you can use sforce.connection.create instead. It accepts a single parameter, an array, that contains sobjects. See this documentation for specific examples. – sfdcfox Aug 11 '16 at 20:29
  • 1
    @tjcinnamon Probably. It'd help to see where you're stuck. – sfdcfox Aug 11 '16 at 20:38

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.