function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
NikiG22NikiG22 

Eliminate Dups on VP

Hello -

I am having issues with validating data on my VP. I have an object called resume subscriptions that have  can be activated with one user (contact) assigned to it. So it is a one to one match. But there can not be more then 1 active resume on the user (contact).

 

the issue im facing is if the sales rep go's to my Mass edit VP they can check all 5 resume subscriptions to be active and assign them all to the same user and hit save and the page will allow all the resume subs to be active with that 1 user, in return messing up the connection to our backoffice.

 

so im looking to run some java to catch the active checkbox and see what user is being assigned, then throw an error if there are more then i resume being active with the same user. The fun part is the resume sub can be assigned to that user but not activated.

 

I have been struggling with this project for 3 weeks now so any help would be AWESOME!!!

 

here is my VP:

<apex:page standardcontroller="Resume_Subscriptions__c" recordSetVar="unused" sidebar="false">

<!--Validation Script-->
<script type="text/javascript">


</script>

<!--Start Form-->
    
<apex:includeScript value="{!$Resource.UtilJS}" />
<apex:form id="massedit">
<br/>
<font size="4px" Color="black">Resume Subscription Edit Page</font>
<apex:pageBlock >
<apex:pageMessages />

Note: All modifications made on the page will be lost if Cancel button is clicked without clicking the Save button first. 

<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!save}" />
<apex:commandButton value="Cancel" action="{!cancel}"/>
</apex:pageBlockButtons>

<!--Start Edit Fields-->
<apex:pageBlockTable value="{!selected}" var="RS" id="table">

<apex:column headerValue="Resume Sub Number">
<apex:inputField value="{!RS.Name}"/>
</apex:column>
<apex:column headerValue="Product Name">
<apex:inputField value="{!RS.Product_Name__c}"/>
</apex:column>

<apex:column headerValue="Active" id="activefield" >
<apex:inputField value="{!RS.Active__c}"/>
</apex:column>
<apex:column headerValue="User" id="user">
<apex:inputField value="{!RS.User__c}"/>
</apex:column>
<apex:column headerValue="Daily View Limit">
<apex:inputField value="{!RS.Daily_View_Limit3__c}"/>
</apex:column>
<apex:column headerValue="Daliy View Limit Enforced">
<apex:inputField value="{!RS.Daily_View_Limit_Enforced__c}"/>
</apex:column>
<apex:column headerValue="Views Purchased">
<apex:inputField value="{!RS.Views_Purchased3__c}"/>
</apex:column>
<apex:column headerValue="View Remaining">
<apex:inputField value="{!RS.View_Remaining__c}"/>
</apex:column>
<apex:column headerValue="Price">
<apex:inputField value="{!RS.Price__c}"/>
</apex:column>
<apex:column headerValue="Subscription Term">
<apex:inputField value="{!RS.Subscription_Term3__c}"/>
</apex:column>
<apex:column headerValue="Purchase Date">
<apex:inputField value="{!RS.Purchase_Date2__c}"/>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>

</apex:form>
</apex:page>

 

 

Best Answer chosen by Admin (Salesforce Developers) 
ClintLeeClintLee

Have you tried creating a controller extension and overriding the save method?

 

Something like this.

 

public with sharing class ResumeExtension {

            List<Resume_Subscription__c> subscriptions { get; set; }

         

             public ResumeExtension( ApexPages.StandardSetController stdController ) {

                         subscriptions = ( List<Resume_Subscription__c> ) stdController.getSelected();

             }

 

            public PageReference newSave() { 

                        Set<Id> activeUserIds = new Set<Id>();

 

                        for( Resume_Subscription__c rs : subscriptions ) {

                              if( rs.Active__c && activeUserIds.contains( rs.User__c ) )  rs.Active__c = false;

                              else if( rs.Active__c )                                                                  activeUserIds.add( rs.User__c );

                       }

 

                       upsert subscriptions;

                       return null;                                  // change this to return the page you want.

           }

}

 

You can associate this extension to your VF page using the extensions="ResumeExtension" attribute in the <apex:page> tag, and change the action on your Save button to {!newSave}.

 

Hope that helps!

 

~ Clint

 

 

All Answers

ClintLeeClintLee

Have you tried creating a controller extension and overriding the save method?

 

Something like this.

 

public with sharing class ResumeExtension {

            List<Resume_Subscription__c> subscriptions { get; set; }

         

             public ResumeExtension( ApexPages.StandardSetController stdController ) {

                         subscriptions = ( List<Resume_Subscription__c> ) stdController.getSelected();

             }

 

            public PageReference newSave() { 

                        Set<Id> activeUserIds = new Set<Id>();

 

                        for( Resume_Subscription__c rs : subscriptions ) {

                              if( rs.Active__c && activeUserIds.contains( rs.User__c ) )  rs.Active__c = false;

                              else if( rs.Active__c )                                                                  activeUserIds.add( rs.User__c );

                       }

 

                       upsert subscriptions;

                       return null;                                  // change this to return the page you want.

           }

}

 

You can associate this extension to your VF page using the extensions="ResumeExtension" attribute in the <apex:page> tag, and change the action on your Save button to {!newSave}.

 

Hope that helps!

 

~ Clint

 

 

This was selected as the best answer
NikiG22NikiG22

thank you so much for your feedback. i get the following error? any idea?

Error	Error: Compile Error: Illegal assignment from SOBJECT:Resume_Subscriptions__c to LIST<Resume_Subscriptions__c> at line 8 column 15

 

ClintLeeClintLee

I updated the code above, it should be getRecords() instead of getRecord().

 

~ Clint

NikiG22NikiG22

I still get this?

Incompatible types since an instance of LIST<SObject> is never an instance of SOBJECT:Resume_Subscriptions__c at line 8 column 32

 Thanks again for your help

ClintLeeClintLee

I updated it again, you have to set the cast to ( List<Resume_Subscriptions__c> ).

 

~ Clint

NikiG22NikiG22

Perfect i got it to save. i just need to get the error page to work now. thanks again for all your help

NikiG22NikiG22

So it is saved but when i test it it dosent work?

 

I test it by activating (checking the active box) and adding a user to it. i do this with two records, waite for the error and it saves with both active?

ClintLeeClintLee

Did you associate the extension to your page and change your save button to this - <apex:commandButton value="Save" action="{!newSave}" />?

 

Also, try changing the value on your table from "{!selected}" to "{!subscriptions}".

NikiG22NikiG22

ya im special. that worked!

 

now im running into the extention looking at ALL resume subs and not just the ones in the page.

ClintLeeClintLee

I updated the constructor in the original code I posted. 

 

Changing to controller.getSelected() should give you only the selected records.

 

 

NikiG22NikiG22

You have no idea how much i appriciate your help. that worked perfectlly!  i do want to bug for 1 more thing.

 

in this part can we also add an error message with the field update. and then add a Done button to my page so they can exit.

 

 if( rs.Active__c && activeUserIds.contains( rs.User__c ) ) rs.Active__c = false;  rs.addError('You can not save Umbrella Record associated with a contact'); 

                             else if( rs.Active__c ) activeUserIds.add( rs.User__c );

 

ClintLeeClintLee

Try doing it this way...

 

if( rs.Active__c && activeUserIds.contains( rs.User__c ) ) {

   rs.Active__c = false;

   rs.Active__c.addError( 'You cannot save Umbrella Record associated with a contact.' );

} else if( rs.Active__c ) {

   activeUserIds.add( rs.User__c );

}

 

// remaining code

 

 

 

NikiG22NikiG22

It worked!!!!! thank you so much!

 

Niki

ClintLeeClintLee

For your Done button you could do the following.

 

Add this to your page under the existing buttons:

 

<apex:commandButton value="Done" action="{!done}" />

 

Add this method to your extension:

 

public PageReference done() {

          return new PageReference( '/' + // add an Id or page name here depending on where you want to send the user );

}

 

If you only want the Done button to show after the updates are complete then you can add this instance variable to your extension class.

 

public boolean done   { get; private set; }

 

Then, add this line to your newSave() method just before the return statement.

 

done = true;

 

Finally, add a rendered attribute to your button.

 

<apex:commandButton value="Done" action="{!done}" rendered = "{done}"  />