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
Lena ChristyLena Christy 

Input values of a custom setting and use it in a trigger

Hi everyone,

It's the first time that I use custom setting and I would be so greatful if you help me with this issues.

What I did is that I created a list custom setting param__c with two number fields: num1__c and num2__c.

So I want to insert there values from a Visualforce page:

<apex:page controller="CustomController"> 
<apex:form >
<apex:inputField value="{!myValueFromPage}"/>
<apex:inputField value="{!secondValueFromPage}"/>

<apex:commandButton action="{!saveValues}" value="Save Input"/>
 </apex:form>
 </apex:page>
 

Its apex code:

public with sharing class CustomController {

public String myValueFromPage {get; set;}

public String secondValueFromPage {get; set;}
    
public CustomController() {

    }

public void saveValues() {
      param__c settings = param__c.getInstance();
    myValueFromPage= settings.num__c ;
    secondValueFromPage= settings.num2__c;
    upsert settings;
    }
}
 

I still don't know how to insert values of the list custom setting from vf page .....!

The second issue is that in the Account standard object I created two number custom fields: field1__c and field2__c.

the field2__c is calculated by num1__c (of the custom setting) divided by field1__c.

The trigger that I made is:

trigger Conversion on Account (before insert) {

 for (Account acc: Trigger.new){
   acc.field2__c= (param__c.num__c)/ (acc.field1__c);
   }
}

I have this error : Error: Compile Error: Arithmetic expressions must use numeric arguments at line 4 column 29

I really need your help.

Thanks.

Best Answer chosen by Lena Christy
Pankaj_GanwaniPankaj_Ganwani
Hi Lena,

I have updated your controller's code. Can you try with this one:
 
public with sharing class CustomController {

public String myValueFromPage {get; set;}

public String secondValueFromPage {get; set;}
    
public CustomController() {

    }

public void saveValues() {
      param__c settings = new param__c();
     if(String.isNotEmpty(myValueFromPage ))
             settings.num1__c = Decimal.valueOf(myValueFromPage );

    if(String.isNotEmpty(secondValueFromPage ))
             settings.num2__c = Decimal.valueOf(secondValueFromPage );
    insert settings;
    }
}

 

All Answers

Lena ChristyLena Christy

I just done the trigger and it worked, if anyone search for it, here is the answer:

trigger Conversion on Account (before insert) {

 for (Account acc: Trigger.new){
 List<param__c> mo = param__c.getall().values();
   acc.field2__c= (mo[0].num__c)/ (acc.field1__c);
   }
}

I really need your help for inpiyt values of the custom settings from a Visualforce page, thanks again!
Pankaj_GanwaniPankaj_Ganwani
Hi Lena,

I have updated your controller's code. Can you try with this one:
 
public with sharing class CustomController {

public String myValueFromPage {get; set;}

public String secondValueFromPage {get; set;}
    
public CustomController() {

    }

public void saveValues() {
      param__c settings = new param__c();
     if(String.isNotEmpty(myValueFromPage ))
             settings.num1__c = Decimal.valueOf(myValueFromPage );

    if(String.isNotEmpty(secondValueFromPage ))
             settings.num2__c = Decimal.valueOf(secondValueFromPage );
    insert settings;
    }
}

 
This was selected as the best answer
Lena ChristyLena Christy

Hi Pankaj, thank you for your reply, I tried your code and had this error:

Error: Could not resolve the entity from <apex:inputField> value binding '{!myValueFromPage}'. <apex:inputField> can only be used with SObjects, or objects that are Visualforce field component resolvable.

Pankaj_GanwaniPankaj_Ganwani
Hi Lina,

<apex:inputField> can only be used with Sobjects such as Account, Contact, Opportunity, Custom objects etc. I made following some minor tweaks in your Page code. Please refer the same:
 
<apex:page controller="CustomController"> 
<apex:form >
<apex:inputtext value="{!myValueFromPage}"/>
<apex:inputtext value="{!secondValueFromPage}"/>

<apex:commandButton action="{!saveValues}" value="Save Input"/>
 </apex:form>
 </apex:page>

 
Lena ChristyLena Christy
Thank you so much Pankaj, i didn't know this before. Thanks a lot!