• Markus Koch 7
  • NEWBIE
  • 5 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
Hey guys,

I have an error that I don't get. Here is my code:
pattern contractNumberPattern = pattern.compile('[0-9]{7}'); 
Matcher contractNumberMatcher = contractNumberPattern.matcher('Test1234567Test');   
String contractNumber;
system.debug(contractNumberMatcher.find());
system.debug(contractNumberMatcher.matches());
system.debug(contractNumberMatcher.groupCount());
contractNumber = contractNumberMatcher.group(1);
system.debug(contractNumber);
It should find a 7 digit number inside a string and return this, But even though find() returns true the groupcount is 0 and I get an error when accessing group(1).
What am I doing wrong here? Thanks in advance!
 

I'm trying to write out JSON from multiple Salesforce objects using the JSONGenerator class, with the below lines as a small example. 

 JSONGenerator gen = JSON.createGenerator(true);   
 gen.writeStartObject(); 
 gen.writeStringField('companyname', account.name); 

 gen.writeStringField('permalink', opp.permalink__c); 

gen.writeEndObject()

 

When writing the string fields, I can't guarantee the string won't be null (for example, that opp.permalink__c is null), which means that the generator throws an error when I try when I attempt to write a null string. To get around it, I could do a null check on every field, and set it to the empty string instead of null, but I'm retrieving a lot of fields and don't want to make the code too unreadable... Is there a cleaner fix for this?

 

Thanks in advance!