0

What am I doing wrong? I hope this is just a simple mistake and me writing it out will even help spur some thought.

I have a VFP embedded in a related list on the Lead object. When a user clicks a button, an error message should appear. I have in the code the apex error message logic and on the VFP the tag. I can tell with my debugs that the apex error is setting correctly, so my thought was that I had to rerender the VFP to see the error displayed, but this does not render the error either. Is there something with iFrames and error messages?

APEX

global class EquifaxCreditPullController {

//I am returning an custom error dependent on error code returned from service
@RemoteAction
global static String pullCred(String leadId, String contactId, String creditReviewId) {
    String status;
    ApexPages.Message msg;

    String jsonBody = '{"LeadId" : "' + leadId + '" }';
    EquifaxCreditPull__c post = EquifaxCreditPull__c.getInstance();
    String endpoint = post.Endpoint__c;
    HttpRequest req = new HttpRequest();
    req.setEndpoint(endpoint);
    req.setMethod('POST');
    req.setBody(jsonBody);
    req.setHeader('Content-Type', 'application/json');
    string d = getAuth();
    string e = 'token ';
    string f = e + d;
    req.setheader('Authorization', f);
    try {
        Http h = new Http();
        HttpResponse res = h.send(req);
        Consumer_Credit_Summary__c c = new Consumer_Credit_Summary__c(FirstName__c = 'test1', Lead__c = leadId, FICO__c = 123.0);
        insert c;
        JSONParser parser = JSON.createParser(res.getBody()); 
        while (parser.nextToken() != null) {
            if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'ResultStatus')) {
                parser.nextToken();
                status = parser.getText();
            }
        }
        if(status == 'Success'){
            system.debug('got into Success:: ' + status);
        }
        else if(status == 'CriticalError'){
            system.debug('got into CriticalError');
            msg=new ApexPages.Message(ApexPages.Severity.ERROR, 'CriticalError');
            ApexPages.addMessage(msg);
        }
        else if(status == 'NotImplemented'){
            system.debug('got into NotImplemented');
            msg=new ApexPages.Message(ApexPages.Severity.ERROR, 'NotImplemented');
            ApexPages.addMessage(msg);
        }
        else {
            system.debug('something went wrong');
            msg=new ApexPages.Message(ApexPages.Severity.ERROR, 'TBD');
            ApexPages.addMessage(msg);
        }
        return status;
    }
    catch (System.CalloutException z) {
        system.debug('z: ' + z);
        return null;
    }
}

public PageReference errorHandling(){
    Id currentPageId = ApexPages.currentPage().getParameters().get('id');
    pullCred(currentPageId, null, null);
    //refreshing the page to display the newly populated data
    PageReference tempPage = ApexPages.currentPage();
    tempPage.setRedirect(true);
    return tempPage;
}
}

VFP

<apex:page id="EquifaxCreditPullPage" showHeader="true" standardController="Lead" extensions="EquifaxCreditPullController">
<head>
    <apex:stylesheet value="{!URLFOR($Resource.SLDS2018, 'styles/salesforce-lightning-design-system.css')}" />
    <apex:includeScript value="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" />

    <script>
    function pullCredit() {
        var leadId = '{!Lead.Id}';
        var contactId = null;
        var creditReviewId = null;
        Visualforce.remoting.Manager.invokeAction(
            '{!$RemoteAction.EquifaxCreditPullController.pullCred}',
            leadId, contactId, creditReviewId,
            function(result, event){
                console.log('result:::::: ' + result);
            }
        );
    };
    </script>
</head>

<apex:form id="theForm">
<apex:messages/>
<!--<div id="errorMessage" style="display:none">There has been an error</div>--><!--will need to hide and show a spinner when the button is thinking and an error message if there is a returned error from the webservice-->

    <table class="slds-table slds-table--bordered slds-table--cell-buffer">
    <thead>
    <tr class="slds-text-title--caps">
        <th scope="col">
            <div class="slds-truncate" title="First Name">Name</div>
        </th>
        <th scope="col">
            <div class="slds-truncate" title="Action">Action</div>
        </th>
    </tr>
    </thead>
    <tbody>
    <apex:repeat value="{!leadCCSMap}" var="contactOrLead">
        <tr>
            <th scope="row" data-label="Name">
                <div class="slds-truncate" title="Name">{!contactOrLead.Name}</div>
            </th>
            <td data-label="Action" id="pullCreditButton">
                <apex:commandButton onClick="pullCredit()" value="Pull Credit"/>
            </td>
        </tr>
    </apex:repeat>
    </tbody>
</table>
</apex:form>
</apex:page>
0

Olivia,

Javascript Remoting performs calls to methods written in Apex and processes them asynchronously.

Additionally, JavaScript Remoting directly passes the parameters to the respective Apex method and does not have access to content maintained within the Visualforce Pages's viewstate.

This is specifically done to improve performance and reduce page response times by avoiding the overhead of serializing and deserializing content passed to the server side Apex method.

The below code leverages an and to call a server side action and displays the error message using a partial page refresh using AJAX.

Apex Controller

global class EquifaxCreditPullController {

        public ApexPages.StandardController leadStdController {get; set;}

        public EquifaxCreditPullController(ApexPages.StandardController pLeadController){
            this.leadStdController = pLeadController;
        }

        public void doSomething(){
            System.debug('****Call to doSomething()');
            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, 'TBD');
            ApexPages.addMessage(msg);
            System.debug('***msg => ' + msg);
        }

        @RemoteAction
        global static void pullCred(String leadId, String contactId, String creditReviewId) {
            /*ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, 'TBD');
            ApexPages.addMessage(msg);
            System.debug('***msg => ' + msg);*/
        }
}//EquifaxCreditPullController ends

Visualforce Code

<apex:page id="EquifaxCreditPullPage" showHeader="true" standardController="Lead" extensions="EquifaxCreditPullController">

    <script>

        function pullCredit() {
            //var leadId = '{!Lead.Id}';
            //var contactId = null;
            //var creditReviewId = null;
            Visualforce.remoting.Manager.invokeAction(
                '{!$RemoteAction.EquifaxCreditPullController.pullCred}',
                '1', //leadId, 
                '2', //contactId, 
                '3', //creditReviewId,
                function(result, event){
                    console.log('result:: ' + result + ' event: ' + event);
                    alert('completed');
                }
            );
        };
    </script>

    <apex:form >
        <apex:outputPanel id="msgpanel">
            <apex:pageMessages />
        </apex:outputPanel>

        <apex:commandButton action="{!doSomething}" onclick="pullCredit()" reRender="msgpanel" value="Pull Credit"/>
    </apex:form>

</apex:page>
  • why would we be calling both "action" and onclick" on the commandButton? – Olivia Jun 4 '18 at 19:58
  • The code above is a sample and hence included both the @RemoteAction invokes from the client side onclick as well as the server-side action. You are correct, you will not need both on the same button.If you are inclined towards using a Visualforce Remoting call then you can also, call the hide and show functions within Javascript to display the error message within the callback. – Jigar Shah Jun 4 '18 at 20:10
  • So if I wanted to go the Remoting route...I would have a commandButton with a onClick to my JS method, I would remote into my APEX method, I would return a value to my JS, my callback function would handle the response, and I would show or hide with jQuery dependent on the value of the response. Does this sound correct? – Olivia Jun 5 '18 at 18:09
0

You need to have a apex:form and apex:commandbutton then only it will render properly or from the function show hide error div

<apex:page id="EquifaxCreditPullPage" showHeader="true" standardController="Lead" extensions="EquifaxCreditPullController">
    <script>
    function pullCredit() {
        var leadId = '{!Lead.Id}';
        var contactId = null;
        var creditReviewId = null;
        Visualforce.remoting.Manager.invokeAction(
            '{!$RemoteAction.EquifaxCreditPullController.pullCred}',
            leadId, contactId, creditReviewId,
            function(result, event){
                console.log('result:: ' + result + ' event: ' + event);
                alert('completed');
            }
        );
    };
    </script>
<apex:form>
    <apex:pageMessages/>
   <apex:commandbutton />
</apex:form>
</apex:page>
  • when I do it this way with the commandButton and javascript, there is no result returned. I am not sure what I am doing wrong... – Olivia Jun 5 '18 at 18:44

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.