4

i have a custom button on a case layout page .... when clicked on button I NEED TO open a subtab with a name for the subtab ......this is to be done by using the onclick javascript ... anyone could u please help its very urgent ...

function openNewSubtab() {
    if (sforce.console.isInConsole()) {
        sforce.console.getEnclosingPrimaryTabId(openSubtab);
    }
}

var opensubtab = function openSubtab(result) {
    var primaryTabId = result.id;
    alert(result.id);
    var URLValue = url;
    alert("Value of the URLValue......" + URLValue);
    sforce.console.openSubtab(result.id, URLValue, true, 'offers', null, openSuccess);
}

var openSuccess = function openSuccess(result) {
    if (result.success == true) {
        alert('subtab successfully opened');
    } else {
        alert('subtab cannot be opened');
    }
};


//to set tab title on load
var pageLoad = window.onload;
window.onLoad = function () {
    if (pageLoad) {
        pageLoad();
    }
    openSubtab();

}

I CHANGED BY CODE , BUT I AM UNABLE TO GET THE TAB TITLE IT IS STILL GIVING AS EXTERNAL PAGE ONLY

2

Here's a VF page I created that opens subtabs immediately. Then I added the VF page to the Console as one of the side bars (so it can execute). I hope the example helps!


<apex:page standardController="Account"  extensions="ConsoleCntrlr_AcctOpenSubtabs" showHeader="false" sidebar="false">
    <apex:includeScript value="/soap/ajax/27.0/connection.js"/>
    <apex:includeScript value="/support/console/27.0/integration.js"/>
    <script type="text/javascript">  
        function openNewSubtab() {
            sforce.connection.sessionId = '{!$Api.Session_ID}';
            if (sforce.console.isInConsole()) { 
                sforce.console.getEnclosingPrimaryTabId(openSubtabs);
            }
        }

        var openSubtabs = function openSubtabs(result) {
            sforce.console.openSubtab(result.id,'/'+'{!PrContact1.Id}',false,'PR Contact 1',null);
            sforce.console.openSubtab(result.id,'/'+'{!LastPR.id}',false,'Last Ran PR',null);
            sforce.console.openSubtab(result.id,'/'+'{!UpcomingPR.id}',false,'Upcoming PR',null);
        };  

        var previousOnload = window.onload;
            window.onload = function() {
            if(previousOnload) {
                previousOnload();
            }
            openNewSubtab();
        }
    </script>
</apex:page>

My Controller:

public with sharing class ConsoleCntrlr_AcctOpenSubtabs {

    public Contact PrContact1 {get;set;}
    public Payrolls__c LastPR {get;set;}
    public Payrolls__c UpcomingPR {get;set;}
    Account Acct;

    public ConsoleCntrlr_AcctOpenSubtabs(ApexPages.standardController con){
        this.Acct=(Account)con.getRecord();
        Account tempAcct = [SELECT id, Payroll_Contact_1__c, Payroll_Contact_2__c
                            FROM Account
                            WHERE id=:Acct.id];
        this.PrContact1 = [SELECT id,name FROM Contact WHERE id=:tempAcct.Payroll_Contact_1__c];

        this.LastPR = [SELECT id 
                       FROM Payrolls__c 
                       WHERE Account__c=:Acct.id
                       AND Processing_Complete__c=true
                       ORDER BY PR_date__c DESC
                       LIMIT 1];

        this.UpcomingPR = [SELECT id 
                           FROM Payrolls__c 
                           WHERE Account__c=:Acct.id
                           AND Processing_Complete__c=FALSE
                           ORDER BY PR_date__c ASC
                           LIMIT 1];
    }//END ConsoleCntrlr_AcctOpenSubtabs



    //%%%%%%%%%%%%%%%%%%%%%  TEST  %%%%%%%%%%%%%%%%%%%%%%%%%
    private static testMethod void testController(){
        List<Account> Accts = new list<Account>{new Account(Name='Test Acct')};
        INSERT Accts;

        list<Payrolls__c> Prs = new list<Payrolls__c>();
        Payrolls__c PR1 = new Payrolls__c(Name='PR1',Account__c=Accts[0].id,PR_Date__c=date.newInstance(2012,3,1),Processing_Complete__c=false);
        Prs.add(PR1);
        Payrolls__c PR2 = new Payrolls__c(Name='PR2',Account__c=Accts[0].id,PR_Date__c=date.newInstance(2012,4,1),Processing_Complete__c=false);
        Prs.add(PR2);
        Payrolls__c PR3 = new Payrolls__c(Name='PR3',Account__c=Accts[0].id,PR_Date__c=date.newInstance(2012,1,1),Processing_Complete__c=true);
        Prs.add(PR3);
        Payrolls__c PR4 = new Payrolls__c(Name='PR4',Account__c=Accts[0].id,PR_Date__c=date.newInstance(2012,2,1),Processing_Complete__c=true);
        Prs.add(PR4);
        INSERT Prs;

        test.startTest();

        test.setCurrentPageReference(page.ServCon_AcctOpenMultTabs);
        ConsoleCntrlr_AcctOpenSubtabs controller = new ConsoleCntrlr_AcctOpenSubtabs(new ApexPages.standardController(Accts[0]));
        system.assertEquals(controller.LastPR.id,PR4.id);
        system.assertEquals(controller.UpcomingPR.id,PR1.id);
        system.assert(!string.ISBLANK(string.valueof(controller.PrContact1.id)));

        test.stopTest();
    }//END testController

}//END Class
  • Can you please tell me why we need to obtain the session id before getting the primary tab id? – Richard N Oct 14 '14 at 14:16
  • 1
    You need to set the sessionId to use the API – Scott Pelak Oct 14 '14 at 15:47
  • Ok thanks. I asked as I did not see this in the official examples for these methods. – Richard N Oct 14 '14 at 19:42

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.