+ Start a Discussion
SF Buzz VutukuriSF Buzz Vutukuri 

apex trigger

// the below sample code showing me error
 if(Trigger.isBefore && Trigger.isUpdate )
      for(Lead led:Trigger.new)
           string compnayname='company';
  Lead oldled= Trigger.oldMap.get(led.ID);

    if(oldled.compnayname!=led.compnayname)// ERROR : there is no such field company name :( (But there I am using the variable name instead of using field API directly, to acheive the requirement. Could plese tell me how to acheive this using the variable name)

No you can't use the lable name/variable name in the apex code, if you want to refer a field. You have to use the API name field to access the field programatically or by API. Normally API name of the custom field will use __c. 

If want to know the API name of the field, then go to that Object->Fields, it will disaply the API name of the field which you want to access
SF Buzz VutukuriSF Buzz Vutukuri

@karanraj - thanks for reply, But I need to use field API name dynamically and need to check old values and news values aren't same!!

Is there any way to use it dynamically >>>>>>>>>>>

Bhupendra chauhanBhupendra chauhan
You need use another for loop in side your for.....not best practice but if you need it you can go for it.
Map<String, Schema.SObjectField> fieldsMap = Schema.SObjectType.Lead.fields.getMap();
 for(String key : fieldsMap.keySet()){
    Schema.DescribeFieldResult result = fieldsMap.get(key).getDescribe();
    if(!result.isNillable() && result.isCreateable()) {
        System.debug(logginglevel.info, '+++++++++ result = '+result.getName());