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
Mohan Chaitanya PalaparthiMohan Chaitanya Palaparthi 

how to update a custom object field through a apex class

i have an custom object called Drink_order
in that i have flavor,size and price fields
 i want to autoupdate price field depending upon the user selecting flavor and size
eg: flavor is apple
and size is large

can anyone please help me.
Best Answer chosen by Mohan Chaitanya Palaparthi
Khan AnasKhan Anas (Salesforce Developers) 
Hi Mohan,

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Note: You can change the Object name and fields name as per your requirement.

Visualforce:
<apex:page controller="AutofillBasedOnPicklist">
    <apex:form >
        <apex:pageBlock >
            Rating: <apex:selectList size="1" value="{!selectedRating}" multiselect="false">    
            <apex:actionSupport event="onchange" action="{!pageLoad}" reRender="t"/>    
            <apex:selectOptions value="{!RList}"/>         
            </apex:selectList>
            <apex:pageBlockSection>
                <apex:inputField value="{!acc.Amount__c}" id="t"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Apex:
public class AutofillBasedOnPicklist {
    
    public string string1 {get;set;}
    public List<User> UserTemp = new List<User>();
    public string selectedRating {get;set;} 
    
    public Account acc {get;set;}
    
    public AutofillBasedOnPicklist(){
        acc = new Account();
    }
    
    public List<SelectOption> getRList() {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('--None--', ''));
        Schema.DescribeFieldResult fieldResult = Account.rating.getDescribe();
        List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
        for( Schema.PicklistEntry f : ple) {
            options.add(new SelectOption(f.getLabel(), f.getValue()));
        }       
        return options;
    }
    
    public PageReference pageLoad() {
        if(selectedRating == 'Hot'){
        	acc.Amount__c=300;
        }
        if(selectedRating == 'Warm'){
        	acc.Amount__c=200;
        }
        if(selectedRating == 'Cold'){
        	acc.Amount__c=100;
        }
        return null;
    }
}

I hope it helps you.

Kindly mark this as solved if the information was helpful. It will help to keep this community clean.

Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Mohan,

Greetings to you!

You can update the price field based on flavor and size using a trigger. 
Try this:
 
trigger updatePrice on Drink_Order__c (before insert,before update) {
    for(Drink_Order__c do : Trigger.new) {
        if(do.Flavor__c == 'Apple' && do.Size__c == 'Large') {
            do.Price__c = 300;
        }
        if(do.Flavor__c == 'Apple' && do.Size__c == 'Medium') {
            do.Price__c = 200;
        }
        //Rest of the code
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Mohan Chaitanya PalaparthiMohan Chaitanya Palaparthi
Hi Khan Anas
             thanks for your answer,but the field will get updated only after we click on save button.What i want is those fields are picklists once the
user selects flavor and size,a value should be display in price before we click on save button.
Khan AnasKhan Anas (Salesforce Developers) 
Hi Mohan,

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Note: You can change the Object name and fields name as per your requirement.

Visualforce:
<apex:page controller="AutofillBasedOnPicklist">
    <apex:form >
        <apex:pageBlock >
            Rating: <apex:selectList size="1" value="{!selectedRating}" multiselect="false">    
            <apex:actionSupport event="onchange" action="{!pageLoad}" reRender="t"/>    
            <apex:selectOptions value="{!RList}"/>         
            </apex:selectList>
            <apex:pageBlockSection>
                <apex:inputField value="{!acc.Amount__c}" id="t"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Apex:
public class AutofillBasedOnPicklist {
    
    public string string1 {get;set;}
    public List<User> UserTemp = new List<User>();
    public string selectedRating {get;set;} 
    
    public Account acc {get;set;}
    
    public AutofillBasedOnPicklist(){
        acc = new Account();
    }
    
    public List<SelectOption> getRList() {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('--None--', ''));
        Schema.DescribeFieldResult fieldResult = Account.rating.getDescribe();
        List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
        for( Schema.PicklistEntry f : ple) {
            options.add(new SelectOption(f.getLabel(), f.getValue()));
        }       
        return options;
    }
    
    public PageReference pageLoad() {
        if(selectedRating == 'Hot'){
        	acc.Amount__c=300;
        }
        if(selectedRating == 'Warm'){
        	acc.Amount__c=200;
        }
        if(selectedRating == 'Cold'){
        	acc.Amount__c=100;
        }
        return null;
    }
}

I hope it helps you.

Kindly mark this as solved if the information was helpful. It will help to keep this community clean.

Regards,
Khan Anas
This was selected as the best answer