2

I have created a text field SP_Login_Name__c on user in which i am saving the sharepoint login name which i have to use for some further api requests.

Sharepoint login name is saved in the following format: i:0#.f|membership|test@test.onmicrosoft.com

But when i query the same field in batch class strangely | character is automatically encoded to | and following system debug is shown

i:0#.f|membership|test@test.onmicrosoft.com

Following is the code snippet of my batch class:

global Database.QueryLocator start(Database.BatchableContext BC) {
        String query = 'Select Id,SP_Login_Name__c,Email,Sharepoint_Username__c,UserRole.Name,UserRoleId,Sharepoint_Group_Id__c from User where Sharepoint_Username__c!=\'\'';
        return Database.getQueryLocator(query);
    }

global void execute(Database.BatchableContext BC, List<User> scope) {
   for(User userObj : scope){

   system.debug('###userObj.SP_Login_Name__c::'+userObj.SP_Login_Name__c); 

   String addUserEndpoint = sPCredentials[0].Sharepoint_Base_URL__c+'sites/'+sPCredentials[0].Site_Name__c+'_api/Web/SiteGroups/GetById('+groupId+')/users';

   HttpRequest addUserRequest = new HttpRequest();
   addUserRequest.setEndpoint(addUserEndpoint); 
   addUserRequest.setMethod('POST'); 
   addUserRequest.setHeader('Accept','application/json; odata=verbose');
   addUserRequest.setHeader('Authorization','Bearer '+sPCredentials[0].spAccessToken__c);
   addUserRequest.setHeader('Content-Type','application/json; odata=verbose');
   addUserRequest.setBody('{ "__metadata": { "type": "SP.User" }, "LoginName":"'+userObj.SP_Login_Name__c+'"}');
   system.debug('###addUserRequest::'+addUserRequest.getBody()); 
   Http addUserhttp = new Http();
   HttpResponse addUserResponse = addUserhttp.send(addUserRequest);  
   }
}

I want the original value stored to make a valid api request. Also i wanted to know the reason why this is happening.

  • Share your code as well. – Tushar Sharma Apr 3 '18 at 11:51
  • I am making simple query in start method and debugging the field value in execute method. – Shubham Sharma Apr 3 '18 at 11:59
  • Can you clarify whether your code actually sees &#124; or if this is just what the System.debug() shows in the log? Is the field of type Text or Rich Text? – David Reed Apr 3 '18 at 13:03
  • The code when sending the request sees this &#124; instead of | vertical line due to which I am not able to send a valid request. I want the SP login name in the exact format in which i saved : i:0#.f|membership|test@test.onmicrosoft.com and the field is of type text. – Shubham Sharma Apr 3 '18 at 13:17
  • 1
    Have you tried posting the value as received in the query and pass it on to the request, does it work? I used the same value stored in a custom field and retrieved it from developer console and printed it in log. I could see the value having a "pipe" instead of &#124. It looks like it's just printing it that way in the logs, but the values are still retrieved appropriately. – Jayant Das Apr 3 '18 at 13:43
0

This has to do with how data is stored in Salesforce Internally and How its displayed to us on the UI screen.

Internally in SF databse all other than UTF-8 chars are stored with their ASCII value. Thus your | becomes &#124; .

You can check the same behaviour by using Debugging the same query in execute Anonomouys using Database.Query('Your Query'); or normal Select

You cannot change the way data is stored in SF but you can change the way it is sent as a request.

addUserRequest.setBody('{ "__metadata": { "type": "SP.User" }, "LoginName":"'+userObj.SP_Login_Name__c.replace('&#124;','|');+'"}');
| improve this answer | |
  • I have already tried to use Replace and ReplaceALL methods but salesforce is still sending &124; instead | in the request. addUserRequest.setBody('{ "__metadata": { "type": "SP.User" }, "LoginName":"'+userObj.SP_Login_Name__c.replaceAll('&#124;','|')+'"}'); – Shubham Sharma Apr 3 '18 at 14:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.