0

Created one Detailed page button in Account object, By clicking this button that calls a visualforce page which has a controller that makes an external HTTP call, Based on the HTTP call response we will redict the page .

The Current issue am facing is its been lauching in same page, want to open in New tab same window

Current Behavior of the button is Display in existing window without sidebar or header, I tried with other Behavior too no luck.

Visual force Page

<apex:page standardController="Account" extensions="CreateOrder" action="{!getRedir}">  
<apex:form >
    <apex:pageBlock >
        <apex:pageBlockSection title="Error Messages" collapsible="false" columns="1" >
            <apex:pageMessages id="errorMessages" />
            <apex:outputLink value="/{!$CurrentPage.parameters.id}">&lt; Return to Account Page</apex:outputLink>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>
</apex:page>

Apex Class:

public class CreateOrder {
private Boolean Failed = false;
private Integer responseCode;  
private string responseBody;
private string AtlasUserToken;
private string SalesOrderUrl;
private string auth_token;

public CreateOrder (ApexPages.StandardController controller) {
    Http h = new Http();
    String url ='https:/test.com/users/token';    
    //this.leadId =  ApexPages.currentPage().getParameters().get('ID');
    HttpRequest  req = buildWebServiceRequest(url);
    HttpResponse  res = invokeWebService(h,req);        
    responseCode=res.getStatusCode();
    responseBody =res.getBody().trim();  

    if (responseCode == 200){           
        Map<String, Object> jsonMap = new Map<String, Object>();
        try{
            jsonMap = (Map<String, Object>)System.JSON.deserializeUntyped(res.getBody());
        }catch(System.JSONException e){
            this.Failed =false; 
        }
        auth_token = (String)jsonMap.get('auth_token');

        Lead l = [SELECT Company,FirstName,LastName, Street, City, State, PostalCode, Phone, Email FROM Lead WHERE id=: ApexPages.currentPage().getParameters().get('id')];
        SalesOrderUrl='https://test.com/salesforce/proposal?&request_for=create_order&api_call=true&address1='+l.Street + '&address2=&city='+l.City+'&state='+l.State+ '&zipcode=' + l.PostalCode + '&temp_auth_token=' +auth_token;
        this.Failed =true; 
    }else{           
        this.Failed =false;            
    }
}

public HttpRequest buildWebServiceRequest(String url) {
    //Build HTTP Request object
    JSONGenerator gen = JSON.createGenerator(true);    
    gen.writeStartObject();   
    //UserInfo.getUserId();        
    User u = [select Atlas_User_Token__c from user where id=:userinfo.getuserid()];
    AtlasUserToken = u.Atlas_User_Token__c;         
    String jsonS;        
    jsonS = JSON.serialize(
        new Map<String, String> {                     
            'user_token' => AtlasUserToken
                });

    // Sending the http body with JSON 
    HttpRequest req = new HttpRequest();
    req.setEndpoint(url);
    req.setMethod('POST');
    req.setHeader('Content-Type', 'application/json;charset=UTF-8');
    req.setbody(jsonS); 
    System.debug(req);
    return req;
}

public HttpResponse invokeWebService(Http h, HttpRequest req) {
    //Invoke Web Service
    HttpResponse res = h.send(req); 
    return res;        
}


public PageReference getRedir() {  
    HttpRequest  req;
    HttpResponse  res;
    if (!this.Failed) {
        // ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Sorry.  We got ' + responseCode + 'Response Code from atlas. <Br/>Current PageId==>'  + ApexPages.currentPage().getParameters().get('ID') +'Current UserId ==>' + UserInfo.getUserId()+ ' Atlas User Token==>' +AtlasUserToken +'  Please try again later.'));
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Sorry.  We got '+ responseCode + ' error Response from atlas. Please Contact System Administrator or try again later.'));
        return null;
    } else {       
        //ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Sorry.  We got '+ auth_token + ' error Response from atlas. Please Contact System Administrator or try again later.'));
        PageReference newPage = new PageReference(SalesOrderUrl);
        newPage.setRedirect(true);
        return newPage;     
        //return null;           
    }

}
}
0

You don't need to change visualforce or apex code.

Need to change the code behind custom button as follows to add _parent parameter which will open the page in new tab:

window.open('/apex/yourvisualforce', '_parent'); 

Here, it is assumed that you are not trying open in a console tab

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.