+ Start a Discussion
srikanth j 24srikanth j 24 

Salesforce Workflow and Trigger


Hi guys,
I have 1 scenario using workflow i need to update 1 field based on some criteria and using Trigger i need to update same object and same field how can i achieve this can anyone help me out.if possible send me code.
Thanks in Advance
Best Answer chosen by srikanth j 24
EldonEldon
If that is your req then try the below code (assuming object name: student__c)
trigger TestTrigger on Student__c (before insert,before update) {
    if(trigger.new.size()>0){
        for(Student__c stu : trigger.new){
            if(stu.City__c=='Bangalore'){
                stu.state__c='AP';
            }
        }
    }
}

Let me know if you have any issues.

Close the thread if it works.


Regards

All Answers

EldonEldon
Hi srikanth,

Please elaborate your requirements like what is the criteria on the workflow and what is the criteria on the trigger for the field updation and when do you want those things to execute

Regards
srikanth j 24srikanth j 24
Workflow:
Evaluation Criteria:created and every time it's edited.
Rule Criteria is:Student City=Bangalore i want to update field called State=AP  This is my Workflow Rule
The Above scenario i want to achieve through Trigger Same Object and Same field.
Plz help me out if possible send me code.  Because i am new to coding





 
EldonEldon
So you need to write a trigger equivalent to your workflow which replaces that workflow right?
EldonEldon
If that is your req then try the below code (assuming object name: student__c)
trigger TestTrigger on Student__c (before insert,before update) {
    if(trigger.new.size()>0){
        for(Student__c stu : trigger.new){
            if(stu.City__c=='Bangalore'){
                stu.state__c='AP';
            }
        }
    }
}

Let me know if you have any issues.

Close the thread if it works.


Regards
This was selected as the best answer
srikanth j 24srikanth j 24
Thanks Bro