+ Start a Discussion
tmbarrytmbarry 

Error Error: Compile Error: Variable does not exist:

I have a trigger that works fine.  This trigger pulls weekly updates from a child object and updates fields on the Opportunity record.  There are three items on the Weekly Update object:  Status Update, Risks, & Next Steps.

 

trigger UpdateOpportunityStatusFromWeeklyUpdate on Weekly_Update__c (after insert, after Update) {
    for (Weekly_Update__c WU : Trigger.new) {
    
    Date dt=wu.date__c;
    String strOppId = wu.Opportunity_Name__c;
         
    List<RecordType> rtypes = [Select Name, Id From RecordType where sObjectType='Opportunity' and isActive=true];        
     //Create a map between the Record Type Name and Id for easy retrieval
     Map<String,String> opptyRecordTypes = new Map<String,String>{};
     for(RecordType rt: rtypes)
        opptyRecordTypes.put(rt.Name,rt.Id);

    
    
for (Opportunity opp : [SELECT id, name, RecordTypeId  FROM Opportunity WHERE id =: strOppId]){
   If(opp.RecordTypeId ==opptyRecordTypes.get('MMA') || opp.RecordTypeId ==opptyRecordTypes.get('MMA - Market Monitoring')) {
        String strA = dT.month() + '/' + dt.day() + '/' + dt.year()+' - ' + wu.rptOwnerName__c + ':  '+ wu.Status_Update__c ;
        String strB = dT.month() + '/' + dt.day() + '/' + dt.year()+' - ' + wu.rptOwnerName__c + ':  '+ wu.Risks__c ;
        String strC = dT.month() + '/' + dt.day() + '/' + dt.year()+' - ' + wu.rptOwnerName__c + ':  '+ wu.Next_Steps__c ;
        Opportunity O = new Opportunity(Id=wu.Opportunity_Name__c, 
                                        Opportunity_Update__c=stra,
                                        Opportunity_Next_Steps_v2__c = strc,
                                        Opportunity_Risks__c=strb,
                                        rptStatusUpateDate__c=dt);
        update O;}        
    Else {
        String strA = dT.month() + '/' + dt.day() + '/' + dt.year()+ ' - '+ wu.Status_Update__c ;
        String strB = dT.month() + '/' + dt.day() + '/' + dt.year()+ ' - '+ wu.Risks__c ;
        String strC = dT.month() + '/' + dt.day() + '/' + dt.year()+ ' - '+ Next1 ;
        Opportunity O = new Opportunity(Id=wu.Opportunity_Name__c, 
                                        Opportunity_Update__c=stra,
                                        Opportunity_Next_Steps_v2__c = strc,
                                        Opportunity_Risks__c=strb,
                                        rptStatusUpateDate__c=dt);
        update O;}
}}
}

The only draw back to this code is, if one of the three fields (Status Update, Risks, & Next Steps) is Null it updates the Opportunty reocrd with the word 'Null'

 

Opportunity Next Steps
8/22/2013 - null

 

So I wrote an if statement to check to see if the value was null and replace the value with 'No Report'

 

trigger UpdateOpportunityStatusFromWeeklyUpdate on Weekly_Update__c (after insert, after Update) {
    for (Weekly_Update__c WU : Trigger.new) {
    
    Date dt=wu.date__c;
    String strOppId = wu.Opportunity_Name__c;
         
    List<RecordType> rtypes = [Select Name, Id From RecordType where sObjectType='Opportunity' and isActive=true];        
     //Create a map between the Record Type Name and Id for easy retrieval
     Map<String,String> opptyRecordTypes = new Map<String,String>{};
     for(RecordType rt: rtypes)
        opptyRecordTypes.put(rt.Name,rt.Id);

/* ****Here is my New Code*** */
If(wu.Status_Update__c == ''){
    String Update1 = 'No Report';}
Else {
    String Update1 = wu.Status_Update__c;}
    
If(wu.Risks__c == ''){
    String Risk1 = 'No Report';}
Else {
    String Risk1 = wu.Risks__c;}
    
If(wu.Next_Steps__c == ''){
    String Next1 = 'No Report';}
Else {
    String Next1 = wu.Next_Steps__c;}        
    
for (Opportunity opp : [SELECT id, name, RecordTypeId  FROM Opportunity WHERE id =: strOppId]){
   If(opp.RecordTypeId ==opptyRecordTypes.get('MMA') || opp.RecordTypeId ==opptyRecordTypes.get('MMA - Market Monitoring')) {
        String strA = dT.month() + '/' + dt.day() + '/' + dt.year()+' - ' + wu.rptOwnerName__c + ':  '+ wu.Status_Update__c ;
        String strB = dT.month() + '/' + dt.day() + '/' + dt.year()+' - ' + wu.rptOwnerName__c + ':  '+ wu.Risks__c ;
        String strC = dT.month() + '/' + dt.day() + '/' + dt.year()+' - ' + wu.rptOwnerName__c + ':  '+ wu.Next_Steps__c ;
        Opportunity O = new Opportunity(Id=wu.Opportunity_Name__c, 
                                        Opportunity_Update__c=stra,
                                        Opportunity_Next_Steps_v2__c = strc,
                                        Opportunity_Risks__c=strb,
                                        rptStatusUpateDate__c=dt);
        update O;}        
    Else {
        String strA = dT.month() + '/' + dt.day() + '/' + dt.year()+ ' - '+ Update1 ;
        String strB = dT.month() + '/' + dt.day() + '/' + dt.year()+ ' - '+ Risk1 ;
        String strC = dT.month() + '/' + dt.day() + '/' + dt.year()+ ' - '+ Next1 ;
        Opportunity O = new Opportunity(Id=wu.Opportunity_Name__c, 
                                        Opportunity_Update__c=stra,
                                        Opportunity_Next_Steps_v2__c = strc,
                                        Opportunity_Risks__c=strb,
                                        rptStatusUpateDate__c=dt);
        update O;}
}}
}

 But I am getting an error message stating: Error: Compile Error: Variable does not exist: Update1 at line 41 column 77

 

Which is this line:

String strA = dT.month() + '/' + dt.day() + '/' + dt.year()+ ' - '+ Update1 ;

 I am sure my error is because I put my new If statements in the wrong spot:

/* ****Here is my New Code*** */
If(wu.Status_Update__c == ''){
    String Update1 = 'No Report';}
Else {
    String Update1 = wu.Status_Update__c;}
    
If(wu.Risks__c == ''){
    String Risk1 = 'No Report';}
Else {
    String Risk1 = wu.Risks__c;}
    
If(wu.Next_Steps__c == ''){
    String Next1 = 'No Report';}
Else {
    String Next1 = wu.Next_Steps__c;} 

 Any thoughts on how to fix this.  The multiple IF statements are screwing me up!

Best Answer chosen by Admin (Salesforce Developers) 
Dhaval PanchalDhaval Panchal

You are declaring string variables in "IF" block, so that are not accessible to outside of "IF" block.

 

try this

String Update1 = '';
If(wu.Status_Update__c == ''){ Update1 = 'No Report';} Else { Update1 = wu.Status_Update__c;}

 

instead of

 

If(wu.Status_Update__c == ''){
    String Update1 = 'No Report';}
Else {
    String Update1 = wu.Status_Update__c;}

All Answers

Dhaval PanchalDhaval Panchal

You are declaring string variables in "IF" block, so that are not accessible to outside of "IF" block.

 

try this

String Update1 = '';
If(wu.Status_Update__c == ''){ Update1 = 'No Report';} Else { Update1 = wu.Status_Update__c;}

 

instead of

 

If(wu.Status_Update__c == ''){
    String Update1 = 'No Report';}
Else {
    String Update1 = wu.Status_Update__c;}
This was selected as the best answer
tmbarrytmbarry

Thanks Dhaval, I never thought of that!