+ Start a Discussion
Frank JordanFrank Jordan 

Help with Apex Class Error: Expecting right curly bracket, found 'try'

I have the below class that updates the status on an agreement request and it's associated agreeemnt (parent record). It is giving me the below error and I can't figure out why. This is my first time using 'try' so im not sure if i am using it correctly or not. Thanks in advance for the help. 
 
public class SubmitUpdateToLegal {
 
try{
    // Query for the agreement request, which has been associated with an agreement.
    Agreement Request queriedAR = [SELECT Agreement_Request__c.Master_Agreement__c 
                              FROM Agreement_Request__c 
                              WHERE Master_Agreement__c != ''
                              ];

    // Update the agreement requests's status
    queriedAR.Status__c = 'Update from Opportunity';

    // Update the related agreement's status
    queriedAR.APTS_Agreement.Apttus__Status__c = 'Update From Opportunity	';

    // Make two separate calls 
    // 1. This call is to update the agreement request's status.
    update queriedAR;
    // 2. This call is to update the related agreement's status field.
    update queriedAR.APTS_Agreement; 
} catch(Exception e) {
    System.debug('An unexpected error has occurred: ' + e.getMessage());
}
}

 
Best Answer chosen by Frank Jordan
Abhishek BansalAbhishek Bansal
Hi Frank,

Since there is no method or constructor defined in your class that is why you are getting the error.
Please update your code as mentioned below :
1. If you want that this code is called everytime when your class is referenced than move this code in constructor :
public class SubmitUpdateToLegal {
 
	public SubmitUpdateToLegal(){
		try{
		// Query for the agreement request, which has been associated with an agreement.
		Agreement Request queriedAR = [SELECT Agreement_Request__c.Master_Agreement__c 
								  FROM Agreement_Request__c 
								  WHERE Master_Agreement__c != ''
								  ];

		// Update the agreement requests's status
		queriedAR.Status__c = 'Update from Opportunity';

		// Update the related agreement's status
		queriedAR.APTS_Agreement.Apttus__Status__c = 'Update From Opportunity	';

		// Make two separate calls 
		// 1. This call is to update the agreement request's status.
		update queriedAR;
		// 2. This call is to update the related agreement's status field.
		update queriedAR.APTS_Agreement; 
		} catch(Exception e) {
			System.debug('An unexpected error has occurred: ' + e.getMessage());
		}
	}
}
2. If you want that this code should be run when some action is performed than move this code inside a method as below :
public class SubmitUpdateToLegal {
 
	public void executeCodeWithMethod(){
		try{
		// Query for the agreement request, which has been associated with an agreement.
		Agreement Request queriedAR = [SELECT Agreement_Request__c.Master_Agreement__c 
								  FROM Agreement_Request__c 
								  WHERE Master_Agreement__c != ''
								  ];

		// Update the agreement requests's status
		queriedAR.Status__c = 'Update from Opportunity';

		// Update the related agreement's status
		queriedAR.APTS_Agreement.Apttus__Status__c = 'Update From Opportunity	';

		// Make two separate calls 
		// 1. This call is to update the agreement request's status.
		update queriedAR;
		// 2. This call is to update the related agreement's status field.
		update queriedAR.APTS_Agreement; 
		} catch(Exception e) {
			System.debug('An unexpected error has occurred: ' + e.getMessage());
		}
	}
}


Please update your class as code by chossing one of the option mentioned above as per your requirement and let me know if you have any issue or if you want more information on this.

Thanks,
Abhishek Bansal.

All Answers

Dan1126Dan1126
I think try-catch needs to be in a method. 
@Karanraj@Karanraj
Where is the method in your class? Try-catch must be inside the method
Abhishek BansalAbhishek Bansal
Hi Frank,

Since there is no method or constructor defined in your class that is why you are getting the error.
Please update your code as mentioned below :
1. If you want that this code is called everytime when your class is referenced than move this code in constructor :
public class SubmitUpdateToLegal {
 
	public SubmitUpdateToLegal(){
		try{
		// Query for the agreement request, which has been associated with an agreement.
		Agreement Request queriedAR = [SELECT Agreement_Request__c.Master_Agreement__c 
								  FROM Agreement_Request__c 
								  WHERE Master_Agreement__c != ''
								  ];

		// Update the agreement requests's status
		queriedAR.Status__c = 'Update from Opportunity';

		// Update the related agreement's status
		queriedAR.APTS_Agreement.Apttus__Status__c = 'Update From Opportunity	';

		// Make two separate calls 
		// 1. This call is to update the agreement request's status.
		update queriedAR;
		// 2. This call is to update the related agreement's status field.
		update queriedAR.APTS_Agreement; 
		} catch(Exception e) {
			System.debug('An unexpected error has occurred: ' + e.getMessage());
		}
	}
}
2. If you want that this code should be run when some action is performed than move this code inside a method as below :
public class SubmitUpdateToLegal {
 
	public void executeCodeWithMethod(){
		try{
		// Query for the agreement request, which has been associated with an agreement.
		Agreement Request queriedAR = [SELECT Agreement_Request__c.Master_Agreement__c 
								  FROM Agreement_Request__c 
								  WHERE Master_Agreement__c != ''
								  ];

		// Update the agreement requests's status
		queriedAR.Status__c = 'Update from Opportunity';

		// Update the related agreement's status
		queriedAR.APTS_Agreement.Apttus__Status__c = 'Update From Opportunity	';

		// Make two separate calls 
		// 1. This call is to update the agreement request's status.
		update queriedAR;
		// 2. This call is to update the related agreement's status field.
		update queriedAR.APTS_Agreement; 
		} catch(Exception e) {
			System.debug('An unexpected error has occurred: ' + e.getMessage());
		}
	}
}


Please update your class as code by chossing one of the option mentioned above as per your requirement and let me know if you have any issue or if you want more information on this.

Thanks,
Abhishek Bansal.

This was selected as the best answer