0

I am trying to insert a campaign through a Visualforce Page to Salesforce db. After insert, user must be directed to the inserted Campaign's detail page. However, the command button is not directing me to the controller action method for insert when I click it. I am not even getting the first debug statement in the method. Below is the relevant parts of my code.

VF page

<apex:form >
                <apex:actionStatus id="renderLightningSpinner" rendered="true" onstart="showSpinner();" onstop="hideSpinner();" >
        <apex:facet name="start">
            <div class="slds-spinner_container slds-hide">
                <div class="slds-spinner--brand slds-spinner slds-spinner--medium" aria-hidden="false" role="alert">
                    <div class="slds-spinner__dot-a"></div>
                    <div class="slds-spinner__dot-b"></div>
                </div>
            </div>
        </apex:facet>
    </apex:actionStatus>
        <apex:actionFunction name="CallApexMethod" action="{!initialiseMandatoryFieldsBlock}" reRender="requiredFieldsBlock,hiddenBlock">
        <apex:param id="mandatoryfield" name="mandatoryfieldwrapper" value="" />
        </apex:actionFunction>
          <apex:outputpanel id="requiredFieldsBlock">
  <apex:pageBlock title="{!$Label.MandatoryFieldsPrompt}" rendered="{!isCampaignSelected}">
  <apex:pageBlockTable id="fieldsTable" title="Mandatoryfields" value="{!MandatoryFieldsWrapperList}" var="mandatoryFieldWrapper">

    <apex:column headerValue="{!$Label.MandatoryFields}" value="{!mandatoryFieldWrapper.fieldLabel}" style="width:500px; height: 28px; text-align:center"/> 

    <apex:column headerValue="{!$Label.Value}">
      <apex:inputField value="{!campaignToInsert[mandatoryFieldWrapper.fieldApi]}" required="true" style="width: 600px; height: 28px" />
    </apex:column>
  </apex:pageBlockTable>
  <apex:commandButton action="{!insertCampaignToDb}" styleClass="slds-button slds-button--brand slds-m-around--large" reRender="requiredFieldsBlock,hiddenBlock" status="renderLightningSpinner" value="Create Campaign"/>
  <apex:commandButton html-novalidate="novalidate" immediate="true" action="{!back}" styleClass="slds-button slds-button--brand slds-m-around--large slds-float--right" reRender="errorMessageBlock,hiddenBlock" status="renderLightningSpinner" value="{!$Label.Back}"/>
     </apex:pageBlock> 
     </div>
</apex:outputpanel>
<apex:pageBlock id="hiddenBlock" rendered="false"></apex:pageBlock> 
  </apex:form>

Controller

public with sharing class ImportCampaignsAngularPoCCntrl {
public Sobject campaignToInsert {get;set;}
public Campaign finalCampaign {get;set;}
public List<MandatoryFieldsWrapper> mandatoryFieldsWrapperList {get;set;}

 public ImportCampaignsAngularPoCCntrl () {

    campaignToInsert = Schema.getGlobalDescribe().get('Campaign').newSObject(); 
    finalCampaign = new Campaign();
   isCampaignSelected = FALSE;
   mandatoryFieldsWrapperList = new List <MandatoryFieldsWrapper>();
    }

   public PageReference insertCampaignToDb() {

    system.debug('Inside InsertCampaigntoDB');
    system.debug('Generic campaignToInsert inside insertCampaignToDb---->'+campaignToInsert);   
    campaignToInsert = (Campaign)campaignToInsert;
    finalCampaign = (Campaign)campaignToInsert;
    system.debug('finalCampaign inside insertCampaignToDb----->'+finalCampaign);
    try {
        DMLManager.insertAsUser(finalCampaign);
        system.debug('Inside try block---->');
        PageReference campaignPage = new PageReference('/' + finalCampaign.id);
        campaignPage.setRedirect(true);
        return campaignPage;
    }        
    catch(DMLManager.CRUDException crudExep) {
        system.debug('Inside Catch block for CRUDException');
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,Label.CrudExpInsertCampaigns));
        LoggerRepository.createErrorLog(finalCampaign.crAccount__c,Label.CrudExpInsertCampaigns,crudExep.getMessage(),crudExep.getStackTraceString());
        throw new loli_utils_Exceptions.CrudException(Label.CrudExpInsertCampaigns);
    }
    catch(DmlException dmlExep) {
        system.debug('Inside Catch block for DMLEXception');
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,Label.DmlException));
        LoggerRepository.createErrorLog(finalCampaign.crAccount__c,Label.DmlException,dmlExep.getMessage(),dmlExep.getStackTraceString());
       throw new DmlException(Label.DmlException);
    }
}
public with sharing class MandatoryFieldsWrapper {
    public String fieldLabel {get;set;}
    public String fieldApi {get;set;}
    public Campaign importedcampaign {get;set;}

    public MandatoryFieldsWrapper (String fieldLabel, String fieldApi, Campaign importedcampaign) {
        this.fieldLabel = fieldLabel;
        this.fieldApi = fieldApi;
        this.importedcampaign = importedcampaign;        
    }    
   }
  }
  • are you getting any errors? – night crawler Jun 22 '17 at 16:36
  • @nightcrawler. No errors. The page is staying as such without any message/errors – Vishnu J Menon Jun 22 '17 at 16:48
  • @nightcrawler. If I am using <div ng-show ="showRequiredFieldsBlock"> instead of rendered="{!isCampaignSelected}", I am able to insert to db. However, this impairs my UX and I cant skip HTML validations in the page if the user try to refresh the form by trying to import another campaign – Vishnu J Menon Jun 22 '17 at 16:55
  • What happen when you use this PageReference campPage = new ApexPages.StandardController(finalCampaign).view(); campPage.setRedirect(true); – Avijit Chakraborty Jun 22 '17 at 17:37
  • @AvijitChakraborty. Setting immediate = "true" doesn't skip the validations. I cant select another campaign for import without entering mandatory fields for the already imported campaign – Vishnu J Menon Jun 22 '17 at 17:43

Your Answer

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

Browse other questions tagged or ask your own question.