substring assignment to limit data to custom field
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
2
down vote
favorite
I have a custom object with a custom field Message__c designed to hold 4000 characters. However, the getBody in rare circumstances has exceeded the 4000 character limit and caused an error. I have made several attempts to limit the max value that can go to 4000 characters by way of substring but test class is failing and the only change I made is changing the assignment from logRecord.Message__c = response.getBody() to the code you see below.
My goal is to limit the size of characters to 4000 so I won't get this error.
string getBodyString = response.getBody();
getBodyString = getBodyString.substring(0,3999);
logRecord.Status_Code__c = response.getStatusCode();
logRecord.Status__c = response.getStatus();
logRecord.Message__c = getBodyString;
apex substring
add a comment |Â
up vote
2
down vote
favorite
I have a custom object with a custom field Message__c designed to hold 4000 characters. However, the getBody in rare circumstances has exceeded the 4000 character limit and caused an error. I have made several attempts to limit the max value that can go to 4000 characters by way of substring but test class is failing and the only change I made is changing the assignment from logRecord.Message__c = response.getBody() to the code you see below.
My goal is to limit the size of characters to 4000 so I won't get this error.
string getBodyString = response.getBody();
getBodyString = getBodyString.substring(0,3999);
logRecord.Status_Code__c = response.getStatusCode();
logRecord.Status__c = response.getStatus();
logRecord.Message__c = getBodyString;
apex substring
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I have a custom object with a custom field Message__c designed to hold 4000 characters. However, the getBody in rare circumstances has exceeded the 4000 character limit and caused an error. I have made several attempts to limit the max value that can go to 4000 characters by way of substring but test class is failing and the only change I made is changing the assignment from logRecord.Message__c = response.getBody() to the code you see below.
My goal is to limit the size of characters to 4000 so I won't get this error.
string getBodyString = response.getBody();
getBodyString = getBodyString.substring(0,3999);
logRecord.Status_Code__c = response.getStatusCode();
logRecord.Status__c = response.getStatus();
logRecord.Message__c = getBodyString;
apex substring
I have a custom object with a custom field Message__c designed to hold 4000 characters. However, the getBody in rare circumstances has exceeded the 4000 character limit and caused an error. I have made several attempts to limit the max value that can go to 4000 characters by way of substring but test class is failing and the only change I made is changing the assignment from logRecord.Message__c = response.getBody() to the code you see below.
My goal is to limit the size of characters to 4000 so I won't get this error.
string getBodyString = response.getBody();
getBodyString = getBodyString.substring(0,3999);
logRecord.Status_Code__c = response.getStatusCode();
logRecord.Status__c = response.getStatus();
logRecord.Message__c = getBodyString;
apex substring
asked Sep 7 at 13:46
Daryn
577
577
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
5
down vote
accepted
Instead of tuncating your self why not let salesforce truncate it for you using dml options?
string getBodyString = response.getBody();
logRecord.Status_Code__c = response.getStatusCode();
logRecord.Status__c = response.getStatus();
logRecord.Message__c = getBodyString;
Database.DMLOptions dmo = new Database.DMLOptions();
dmo.allowFieldTruncation = true;
logRecord.setOptions(dmo);
insert logRecord;
SRC: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_dml_database_dmloptions.htm
Edit: For adding it to List use Database.insert
Database.DMLOptions dmo = new Database.DMLOptions();
dmo.allowFieldTruncation = true;
Database.insert(myLoggerList,dmo);
By this way in future, if you increase the character limit of your field, you don't have to deploy your code again.
I was not aware of that option. This sounds like an ideal solution. I was looking into the getDescribe to replace the hardcoded value but this is better.
– Daryn
Sep 7 at 18:33
Well I tried it and ran into this: Method does not exist or incorrect signature: void setOptions(Database.DMLOptions) from the type List<MDM_Logger__c>
– Daryn
Sep 7 at 18:38
@Daryn are you using setOptions on list or on individual record. U can use it only on individual records. For using it on List you have to use database.insert method. Edited answer to reflect it.
– Pranay Jaiswal
Sep 7 at 18:44
Seems to be working... Great solution... Thank you!
– Daryn
yesterday
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
accepted
Instead of tuncating your self why not let salesforce truncate it for you using dml options?
string getBodyString = response.getBody();
logRecord.Status_Code__c = response.getStatusCode();
logRecord.Status__c = response.getStatus();
logRecord.Message__c = getBodyString;
Database.DMLOptions dmo = new Database.DMLOptions();
dmo.allowFieldTruncation = true;
logRecord.setOptions(dmo);
insert logRecord;
SRC: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_dml_database_dmloptions.htm
Edit: For adding it to List use Database.insert
Database.DMLOptions dmo = new Database.DMLOptions();
dmo.allowFieldTruncation = true;
Database.insert(myLoggerList,dmo);
By this way in future, if you increase the character limit of your field, you don't have to deploy your code again.
I was not aware of that option. This sounds like an ideal solution. I was looking into the getDescribe to replace the hardcoded value but this is better.
– Daryn
Sep 7 at 18:33
Well I tried it and ran into this: Method does not exist or incorrect signature: void setOptions(Database.DMLOptions) from the type List<MDM_Logger__c>
– Daryn
Sep 7 at 18:38
@Daryn are you using setOptions on list or on individual record. U can use it only on individual records. For using it on List you have to use database.insert method. Edited answer to reflect it.
– Pranay Jaiswal
Sep 7 at 18:44
Seems to be working... Great solution... Thank you!
– Daryn
yesterday
add a comment |Â
up vote
5
down vote
accepted
Instead of tuncating your self why not let salesforce truncate it for you using dml options?
string getBodyString = response.getBody();
logRecord.Status_Code__c = response.getStatusCode();
logRecord.Status__c = response.getStatus();
logRecord.Message__c = getBodyString;
Database.DMLOptions dmo = new Database.DMLOptions();
dmo.allowFieldTruncation = true;
logRecord.setOptions(dmo);
insert logRecord;
SRC: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_dml_database_dmloptions.htm
Edit: For adding it to List use Database.insert
Database.DMLOptions dmo = new Database.DMLOptions();
dmo.allowFieldTruncation = true;
Database.insert(myLoggerList,dmo);
By this way in future, if you increase the character limit of your field, you don't have to deploy your code again.
I was not aware of that option. This sounds like an ideal solution. I was looking into the getDescribe to replace the hardcoded value but this is better.
– Daryn
Sep 7 at 18:33
Well I tried it and ran into this: Method does not exist or incorrect signature: void setOptions(Database.DMLOptions) from the type List<MDM_Logger__c>
– Daryn
Sep 7 at 18:38
@Daryn are you using setOptions on list or on individual record. U can use it only on individual records. For using it on List you have to use database.insert method. Edited answer to reflect it.
– Pranay Jaiswal
Sep 7 at 18:44
Seems to be working... Great solution... Thank you!
– Daryn
yesterday
add a comment |Â
up vote
5
down vote
accepted
up vote
5
down vote
accepted
Instead of tuncating your self why not let salesforce truncate it for you using dml options?
string getBodyString = response.getBody();
logRecord.Status_Code__c = response.getStatusCode();
logRecord.Status__c = response.getStatus();
logRecord.Message__c = getBodyString;
Database.DMLOptions dmo = new Database.DMLOptions();
dmo.allowFieldTruncation = true;
logRecord.setOptions(dmo);
insert logRecord;
SRC: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_dml_database_dmloptions.htm
Edit: For adding it to List use Database.insert
Database.DMLOptions dmo = new Database.DMLOptions();
dmo.allowFieldTruncation = true;
Database.insert(myLoggerList,dmo);
By this way in future, if you increase the character limit of your field, you don't have to deploy your code again.
Instead of tuncating your self why not let salesforce truncate it for you using dml options?
string getBodyString = response.getBody();
logRecord.Status_Code__c = response.getStatusCode();
logRecord.Status__c = response.getStatus();
logRecord.Message__c = getBodyString;
Database.DMLOptions dmo = new Database.DMLOptions();
dmo.allowFieldTruncation = true;
logRecord.setOptions(dmo);
insert logRecord;
SRC: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_dml_database_dmloptions.htm
Edit: For adding it to List use Database.insert
Database.DMLOptions dmo = new Database.DMLOptions();
dmo.allowFieldTruncation = true;
Database.insert(myLoggerList,dmo);
By this way in future, if you increase the character limit of your field, you don't have to deploy your code again.
edited yesterday
Daryn
577
577
answered Sep 7 at 13:57


Pranay Jaiswal
8,73431848
8,73431848
I was not aware of that option. This sounds like an ideal solution. I was looking into the getDescribe to replace the hardcoded value but this is better.
– Daryn
Sep 7 at 18:33
Well I tried it and ran into this: Method does not exist or incorrect signature: void setOptions(Database.DMLOptions) from the type List<MDM_Logger__c>
– Daryn
Sep 7 at 18:38
@Daryn are you using setOptions on list or on individual record. U can use it only on individual records. For using it on List you have to use database.insert method. Edited answer to reflect it.
– Pranay Jaiswal
Sep 7 at 18:44
Seems to be working... Great solution... Thank you!
– Daryn
yesterday
add a comment |Â
I was not aware of that option. This sounds like an ideal solution. I was looking into the getDescribe to replace the hardcoded value but this is better.
– Daryn
Sep 7 at 18:33
Well I tried it and ran into this: Method does not exist or incorrect signature: void setOptions(Database.DMLOptions) from the type List<MDM_Logger__c>
– Daryn
Sep 7 at 18:38
@Daryn are you using setOptions on list or on individual record. U can use it only on individual records. For using it on List you have to use database.insert method. Edited answer to reflect it.
– Pranay Jaiswal
Sep 7 at 18:44
Seems to be working... Great solution... Thank you!
– Daryn
yesterday
I was not aware of that option. This sounds like an ideal solution. I was looking into the getDescribe to replace the hardcoded value but this is better.
– Daryn
Sep 7 at 18:33
I was not aware of that option. This sounds like an ideal solution. I was looking into the getDescribe to replace the hardcoded value but this is better.
– Daryn
Sep 7 at 18:33
Well I tried it and ran into this: Method does not exist or incorrect signature: void setOptions(Database.DMLOptions) from the type List<MDM_Logger__c>
– Daryn
Sep 7 at 18:38
Well I tried it and ran into this: Method does not exist or incorrect signature: void setOptions(Database.DMLOptions) from the type List<MDM_Logger__c>
– Daryn
Sep 7 at 18:38
@Daryn are you using setOptions on list or on individual record. U can use it only on individual records. For using it on List you have to use database.insert method. Edited answer to reflect it.
– Pranay Jaiswal
Sep 7 at 18:44
@Daryn are you using setOptions on list or on individual record. U can use it only on individual records. For using it on List you have to use database.insert method. Edited answer to reflect it.
– Pranay Jaiswal
Sep 7 at 18:44
Seems to be working... Great solution... Thank you!
– Daryn
yesterday
Seems to be working... Great solution... Thank you!
– Daryn
yesterday
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f231629%2fsubstring-assignment-to-limit-data-to-custom-field%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password