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
dujsudujsu 

Updating an Object field with a New Value

Hello,

 

Im new to APEX and could use a nudge here. Im trying to update a custom field with the Opportunity object and am getting the following error below. 

 

Visual Force Error: System.NullPointerException: Attempt to de-reference a null object 

 


Code

 

public with sharing class myTestClass{
	
	public MyCustomBuildObject__c build {get;set;}
	public String myWSXMLResult {get;set;}  
	public String getWSXMLResult() {
		

		build = ip_shared_updateMethods.getBuild('a1DV00000000jK4MAI');
		if(build.Opportunity__r.LiveLink_URL__c == null && build.Product_Group__r.Engineering_Engagement__c == 'Required')
		{
			String tmpFolderName = build.Opportunity__r.Account.Name + ' - ' + build.Opportunity__r.Custom_Contract_Number__c;
			String wordCharPattern = '[^_A-Za-z0-9-\\s]';
			String LLWSResultPattern = '[0-9]{2,}';
			Pattern compiledWordCharPattern = pattern.compile(wordCharPattern);
			Matcher wordCharMatcher = compiledWordCharPattern.matcher(tmpFolderName);
			tmpFolderName = wordCharMatcher.replaceAll('');
			tmpFolderName = tmpFolderName.replaceAll('  ',' ');
			
			DSPA__c dsp = DSPA__c.getInstance();
			String tmpObjectID = dsp.DSPID__c;
			
			String myWSXMLResult = another_class.invokeWs(tmpFolderName,tmpObjectID);
			
			Boolean LLWSPatternMatched = pattern.matches(LLWSResultPattern,myWSXMLResult);
			
			if(LLWSPatternMatched)
			{
				/******Error occurs on line below******/
				build.Opportunity__r.LLURL__c = 'https://internalService/' + myWSXMLResult;
				update build;
			} 
			
		} 
		return myWSXMLResult;
	}
}

 

This is where the error is pointing to..

 

/******Error occurs on line below******/
build.Opportunity__r.LLURL__c = 'https://internalService/' + myWSXMLResult;

Im sure this is an easy one, thanks ahead of time for the advice.

Best Answer chosen by Admin (Salesforce Developers) 
SFRajSFRaj

Hi,

 

Normally (System.LimitException: DML currently not allowed) this error occurs if you call the method in constructor.

 

You can't execute DML in the Constructor (or call a method that executes DML from a Constructor)

 

If this is the controller for a VF page, you can use the action parameter on the page to directly execute addcomment, and that is allowed I believe.

Calling this method in VF Page or Trigger?


All Answers

SFRajSFRaj

Hi,

 

I think you can't assign value to build.Opportunity__r.LLURL__c. Only you can get the value.

dujsudujsu

Thanks @SFRaj - this is the solution that I came up with..

 

UPDATE: See next post

 

if(LLWSPatternMatched)
{
	Opportunity o = [SELECT Id FROM Opportunity 
	                 WHERE Id = :build.Opportunity__c 
             		 LIMIT 1];	
	o.LiveLink_URL__c = 'https://internalService/' + myWSXMLResult;
	update o;

} 
dujsudujsu

Ok - so the solution above turned out to not work. I had a data related issue, no opportunity was associated to the 'build'. Ive resolved that issue now, but am still unable to update the Opportunity record.

 

Now I am getting the following error:

 

System.LimitException: DML currently not allowed 

 

The error occurs on the "update o;" statement.

 

 

Anyone have a suggestion?

VKrishVKrish

I am new to Apex too. I haven't worked with Opportunity object.

But why can't you try this?

if(LLWSPatternMatched)
{
	Opportunity o =  new Opportunity(Id = build.Opportunity__c);
	o.LiveLink_URL__c = 'https://internalService/' + myWSXMLResult;
	update o;
}

 This works with me in other standard objects like Contact, Account for me.

dujsudujsu

Hey @VKrish - thanks for the response. This gives me the same error, just FYI.

VKrishVKrish

Is this class/method is called from a Oppertunity trigger? If so update is not allowed. Just changing the variable with desired value should do.

SFRajSFRaj

Hi,

 

Normally (System.LimitException: DML currently not allowed) this error occurs if you call the method in constructor.

 

You can't execute DML in the Constructor (or call a method that executes DML from a Constructor)

 

If this is the controller for a VF page, you can use the action parameter on the page to directly execute addcomment, and that is allowed I believe.

Calling this method in VF Page or Trigger?


This was selected as the best answer
dujsudujsu

I ended up calling it from the action in a VF Page.  Thanks again @SFRaj!