We are trying to change contact owner from trigger - salesforce

We are trying to update the contact owner depending on a checkbox value.
If the checkbox if true, the user should be allowed to change owner, otherwise the user should try to change owner.
It should update the old owner value only.
public void setContactOwner(List <Contact> conList ,Map <id,Contact> oldMap){
system.debug ('newMap '+conList );
system.debug ('inside setcontact function');
List <Contact> newCon = new List<Contact>();
for (Contact con : conList){
system.debug ('Override'+oldMap.get(con.Id).OwnerID__c );
if(con.Override__c == false){
con.OwnerID__c =oldMap.get(con.Id).OwnerID__c ;
}
}
}

Related

How to call parameterized controller in apex command button in visualforce?

I have a apex class of the following type
public standardcontroller(){
List<account> accountList = [SELECT Name FROM Account LIMIT 20];
ApexPages.StandardSetController ssc = new ApexPages.StandardSetController(accountList);
List<Account> selected=ssc.getSelected();
}
public PageReference save(List<Account> selected){
if(code logic)
}
The visual force page contains a command button
<apex:commandButton action="{!save}" value="Save" id="theButton"/>
How to pass the parameter List selected when calling save method from vf page
You have to retrieve the Id of that record (In case of the detail button)
Id recordId = standardController.getId();
Account record = (Account) standardController.getRecord();
PageReference pdfPage = new PageReference('/apex/YOURVFPAGENAME');
pdfPage.getParameters().put('id',recorded);
Now you have the Id of that record, now you can perform any action in the controller base on that ID.

VisualForce Page to render list of selected contacts

Here is a scenario , I am stuck in.
//edited post to elaborate more details.
Requirement: I need to email bulk of selected contacts. When there is no email to selected contacts, we are required to populate the name of contacts on UI who do not have the email. I am able to accomplish first part of requirement but stuck on displaying contact names on visual force page .
List button : BulkEmailTest which calls firstVF visual force page.
firstVF code:
<apex:page standardController="Contact" extensions="FirstController" recordSetVar="listRecs"
action="{!send}">
Emails are being sent!
window.history.back();
</apex:page>
FirstController code: for simplified code, I have edited snippet for contacts with email as our priority is only related to contacts with no email.
public with sharing class FirstController
{
public List noEmail {get;set;}
public Contact contact;
public List allcontact {get; set;}
Id test;
public Contact getAllContact() {
return contact;
}
ApexPages.StandardSetController setCon;
ApexPages.StandardController setCon1;
public static Boolean err{get;set;}
public FirstController(ApexPages.StandardController controller)
{
setCon1 = controller;
}
public FirstController(ApexPages.StandardSetController controller)
{
setCon = controller;
}
public PageReference cancel()
{
return null;
}
public FirstController()
{
}
public PageReference send()
{
noEmail = new List();
set ids = new set();
for(Integer i=0;i<setCon.getSelected().size();i++){
ids.add(setCon.getSelected()[i].id);
}
if(ids.size() == 0){
err = true;
return null;
}
List allcontact = [select Email, Name, firstName , LastName from Contact where Id IN :ids];
for(Contact current : allcontact)
{
system.debug(current);
if (current.Email!= null)
{
PageReference pdf = Page.pdfTest;
pdf.getParameters().put('id',(String)current.id);
system.debug('id is :'+current.id);
pdf.setRedirect(true);
return pdf;
}
else //No email
{
system.debug('in else current'+current );
noEmail.add(current);
// noEmail.add(current);
system.debug('in else noemail'+noEmail );
}//e
}
if(noEmail.size()>0 ) {
PageReference pdf1 = Page.NoEmailVF;
pdf1.getParameters().put('Name', String.valueOf(noEmail));
system.debug('pring noEmail' +noEmail);
pdf1.setRedirect(false);
return pdf1;
}
return null;
}
}
NoEmailVF visual force page code
<apex:page controller="FirstController">
Emails are not sent to below contacts :
Name
<apex:repeat var="cx" value="{!allcontact}" rendered="true">
{!cx.name}
Please note that emails are not sent to selected Donors only when
they did not make any donation for that year or if they do not have email address listed.
If you still wish to retrieve donations made in this year, then you may use "Hard Copy" button listed on the Donor record to have the data printed.
.
It doesn't look like your apex:commandButton has any action. If it doesn't have an action prop, no contact is being made with your Apex code. So no oncomplete should fire.
Also, when you want to refresh some data after completing some Apex call, you can use reRender prop. Put your dynamic data in an apex:outputPanel (docs), give the apex:outputPanel an id and pass that id to the reRender of your button.
See apex:commandButton docs

Check if user saved in SQLite database in Xamarin

I have Xamarin forms app ,the user will register and login for first time and save username in SQLite database ,then I want the app check if the database found and the username is have been inserted any time he open the app.
i used this code in registration page :
SqliteUser squser = new SqliteUser()
{
appUser = user
};
using (SQLiteConnection conn = new SQLiteConnection(App.DatabaseLocation))
{
conn.CreateTable<SqliteUser>();
int rows = conn.Insert(squser);
};
I checked the user is inserted successfully.
Now I made CheckUserpage to check if there is a user registered, if yes the alert me the name of first user inserted :-
public partial class CheckUserPage : ContentPage
{
public CheckUserPage()
{
InitializeComponent();
CheckSqlUser();
}
private void CheckSqlUser() {
using (SQLiteConnection conn = new SQLiteConnection(App.DatabaseLocation))
{
var userQuery = conn.Table<SqliteUser>().Where(a=>a.appUser!="");
if (userQuery != null) {
DisplayAlert("user found", "user found", "OK");
}
};
}
}
How can I get the first user name inserted ?
Best Regard
to get the first value in a list, use LINQ
using System.Linq;
var userQuery = conn.Table<SqliteUser>().Where(a=>a.appUser!="");
var results = userQuery.ToList();
var first = results.FirstOrDefault();

List has no rows for assignment to SObject error although query returns rows

I'm a bit new to apex and I am trying to display a selectList in a visualforce page using a custom controller i built.
I get a "List has no rows for assignment to SObject" error when trying to preview the visualforce page, but running the query in the developer console, returns the rows.
here is my page:
<apex:page Controller="BpmIcountPayment">
<apex:form >
<apex:selectList value="{!productsTitle}" multiselect="false">
<apex:selectOptions value="{!ProductsLov}"></apex:selectOptions>
</apex:selectList>
</apex:form>
</apex:page>
and my controller:
public class BpmIcountPayment{
private final Account account;
public String productsTitle {
get { return 'products for sale'; }
set;
}
public BpmIcountPayment() {
account = [SELECT Id, Name, Site FROM Account
WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
}
public Account getAccount() {
return account;
}
public List<SelectOption> getProductsLov() {
List<SelectOption> products = new List<SelectOption>();
List<Product2> productsList = [SELECT Id, Name, Family
FROM Product2
WHERE (Family = 'ShopProduct')
OR (Family = 'CourseParent')
OR (Family = 'SFCourseProgram')];
for (Product2 currProduct : productsList) {
products.add(new SelectOption(currProduct.Id, currProduct.Name));
}
return products;
}
}
Just to clarify the query i'm referring to is the query in getProductsLov().
My API version is 40 and i am working in a sandbox environment.
Impossible. If you're getting "list has no rows to assign to sObject" it means you're assigning to single object. This eliminates getProductsLov(unless you didn't post whole code) because there you assign to a list.
Humo(u)r me and System.debug(JSON.serializePretty(ApexPages.currentPage().getParameters())); in your constructor before firing that query...
You're viewing the page with valid Account Id passed in the URL? And that Account is visible for your current user? If the page is account-specific, try using <apex:page standardController="Account" extensions="BpmIcountPayment">... (you'll have to provide a different constructor in apex first). This could simplify your code a lot.
public BpmIcountPayment(ApexPages.StandardController sc){
if(String.isBlank(sc.getId()){
System.debug('you screwed up passing the valid acc id');
} else {
acc = (Account) sc.getRecord();
}
}

How to give rights for one user(profile) to delete only one other user(profile) in jhipster

I need to view the administration drop down to other authorities as well in jhipster. For that which file how should I change? I guess it should be navbar.html and the following line.
<li ng-class="{active: `vm.$state.includes('admin')`}" ng-switch-when="true" has-authority="ROLE_ADMIN" uib-dropdown class="dropdown pointer">
How to pass multiple authorities there. Other authority should be ROLE_MEMBER. In the JHI_AUTHORITY table it was added. I tried the above code as follows. But didn't make the change which I need.
vm.$state.includes('admin','member')
has-any-authority works as in the following answer. Then I need to give privileges for members to delete users which has the profile of ROLE_STUDENT. How should I do that?
.state('user-management.delete', {
url: '/{login}/delete',
data: {
authorities: ['ROLE_ADMIN','ROLE_MEMBER']
},
There members and admin both can delete all the users. But I need to give rights for members to delete only students(ROLE_STUDENT). How should I implement that logic? May be it should be implemented inside UserResource.java following method.
#DeleteMapping("/users/{login:" + Constants.LOGIN_REGEX + "}")
#Timed
#Secured({AuthoritiesConstants.ADMIN, AuthoritiesConstants.MEMBER})
public ResponseEntity<Void> deleteUser(#PathVariable String login) {
log.debug("REST request to delete User: {}", login);
boolean hasAuthorityAdmin = false;
boolean hasAuthorityMember = false;
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
hasAuthorityAdmin = authorities.contains(new SimpleGrantedAuthority(AuthoritiesConstants.ADMIN));
hasAuthorityMember = authorities.contains(new SimpleGrantedAuthority(AuthoritiesConstants.MEMBER));
if (hasAuthorityAdmin) {
// delete user
userService.deleteUser(login);
} else {
if (hasAuthorityMember) {
// delete user if it is a student
if (userService.getAuthorities().size() == 4 && userService.getAuthorities().contains(AuthoritiesConstants.STUDENT)) {
userService.deleteUser(login);
}
}
}
return ResponseEntity.ok().headers(HeaderUtil.createAlert("userManagement.deleted", login)).build();
}
There I guess mistake with this if (userService.getAuthorities().size() == 4 && userService.getAuthorities().contains(AuthoritiesConstants.STUDENT)) condition. There if I can get the user role of which is going to delete, then we can check whether it is a ROLE_STUDENT. I think deleteUser(#PathVariable String login) if we can pass another parameter to get the user profile where going to delete we can resolve the problem. Or if we can find another method in userService to get the authorities which is going to delete?
I think you can do it using has-any-authority :
<li ng-class="{active: `vm.$state.includes('admin')`}" ng-switch-when="true" has-any-authority="ROLE_MEMBER, ROLE_ADMIN" uib-dropdown class="dropdown pointer">
Following the question update, here is what you could do to be able to delete a user with ROLE STUDENT if the current user has the role MEMBER :
#DeleteMapping("/users/{login:" + Constants.LOGIN_REGEX + "}")
#Timed
#Secured({AuthoritiesConstants.ADMIN, AuthoritiesConstants.MEMBER})
public ResponseEntity<Void> deleteUser(#PathVariable String login) {
log.debug("REST request to delete User: {}", login);
boolean hasAuthorityMember = false;
boolean hasAuthorityAdmin = false;
hasAuthorityMember = SecurityContextHolder.getContext().getAuthentication().getAuthorities().contains(AuthoritiesConstants.MEMBER);
hasAuthorityAdmin = SecurityContextHolder.getContext().getAuthentication().getAuthorities().contains(AuthoritiesConstants.ADMIN);
if(hasAuthorityAdmin) {
// delete user
userService.deleteUser(login);
} else {
if(hasAuthorityMember) {
// delete user if it is a student
if(userService.getAuthorities().size()==1 && userService.getAuthorities().contains(AuthoritiesConstants.STUDENT)) {
userService.deleteUser(login);
}
}
}
return ResponseEntity.ok().headers(HeaderUtil.createAlert( "userManagement.deleted", login)).build();
}

Resources