• Harshwardhan Singh Karki
  • NEWBIE
  • 0 Points
  • Member since 2023

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 7
    Questions
  • 2
    Replies
Ex:

@isTest
public with sharing class Test_UserTriggerHandler {
    
    @testSetup
    static void createUser(){
        try{
            Id pId = [Select Id,Name From Profile Where Name = 'PE Standard User'].Id;
            User userRec = new User(
                FirstName = 'TestNew',
                LastName = 'User', 
                Email = 'testuser123@gmail.com',
                ProfileId = pId,
                IsActive = True,
                UserName = 'testUser123@gmail.com',
                TimeZoneSidKey = 'America/Los_Angeles',
                EmailEncodingKey = 'UTF-8',
                LanguageLocaleKey = 'en_US',
                LocaleSidKey = 'en_US'
            );
            insert userRec;
            System.runAs(userRec){
                system.debug(userRec);
            }
            
        }
        catch(Exception e){
            system.debug('The error is :'+e.getMessage()+ 'at '+e.getLineNumber());
        }
    }
    
   private static testmethod void testInsertEvent(){
            Id pId = [Select Id,Name From Profile Where Name = 'PE Admin'].Id;
            User userRec = new User(
                FirstName = 'TestNew',
                LastName = 'User', 
                Email = 'testnewuser@gmail.com',
                ProfileId = pId,
                IsActive = True,
                UserName = 'testnewUser123@gmail.com',
                TimeZoneSidKey = 'America/Los_Angeles',
                EmailEncodingKey = 'UTF-8',
                LanguageLocaleKey = 'en_US',
                LocaleSidKey = 'en_US'
            );
        Test.startTest();
            insert userRec;
        Test.stopTest();
        System.runAs(userRec){
        }
    }
    
    
    private static testmethod void testUpdateEvent(){
        list<User> userListToUpdate = new list<User>();
        For(User userRec : [Select Id,FirstName,LastName, Name From User Where IsActive = True]){
            userRec.FirstName = 'Test'+ userRec.Id;
            userListToUpdate.add(userRec);
        }
        
        if(!userListToUpdate.isEmpty()){
            Test.startTest();
            update userListToUpdate;
            Test.stopTest();
        }

}


This code is not running since i am unable to get profileid and also no record of user inserted 
Ex: Let Custom Object - A__c  have Picklist Field (Custom Field) - P__c , so how to get Id of P__c in apex ?
I am trying to fetch the content from file (VersionData) which is Blob type and i want to display it as a string in LWC .I am unable to get the normal string instead getting the string in other format after using Encoding class .Is there any solution regarding this ?

Ex:-
ID is argument passed to function

ContentVersion newCV = [Select Id, ContentDocumentId, VersionData,Notes__c From ContentVersion Where Title = :ID];
                    Blob fileBody = newCV.VersionData;
                    String textString = fileBody.toString();
                   String textString = EncodingUtil.base64Encode(fileBody);
                   system.debug(textString);
It is not displaying the actual string so is there way to convert it to string
I have a lwc component in which "imp notes" button is clicked then we can choose a pdf or word file form our system which is being done in .net page and text inside that  file has to be displayed in a field (long text area) in LWC . So What are the ways in which we can do this?
I have A custom metadata type named "ImportNotes_Setting__mdt" with fields , DeveloperName , MasterLabel as standard and custom fields are , ImportId__c,Notes__c, IsDelete__c ,so  i need to insert records from user for this custom metadata type using tooling api (Http callout) .

Code executed to insert record of custom metadata type which is  returning error {"message":"entity type cannot be inserted: ImportNotes Setting","errorCode":"CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY","fields":[]} , So i need the solution regarding this if it is possible to do so with this approach


global class TestRecordInsertUsingToolingApi {
    @future (callout = true)
    public static void connectOrg(){
        Http h = new Http();
        Httprequest req = new Httprequest();
        string json ='{"Masterlabel":"Import Note","ImportID__c":"Imp36862577","Notes__c":"Test ToolinApi"}';
        String autho ='Bearer '+ UserInfo.getUserId();
        req.setHeader('Authorization', autho);            
        string endpointRemainingUrl = '/services/data/v56.0/sobjects/ImportNotes_Setting__mdt';
        string endpoint = Url.getSalesforceBaseUrl().toExternalForm() + endpointRemainingUrl;
        req.setEndpoint(endpoint); 
        req.setHeader('Content-Type','application/json;  charset=utf-8');
        req.setBody(json);
        req.setMethod('POST');
        Httpresponse res = h.send(req);
        system.debug(res.getBody()+ res.getStatusCode());
        if(res.getStatusCode() == 400){
            system.debug(res.getBody());
            Map<String, Object> m = (Map<String, Object>)System.JSON.deserializeUntyped(res.getBody());
            system.debug(m);
        }
    }
Does contentDocumentLink and contentDocument return no record when used in apex as in my query it returns no records.

Query -
list<ContentDocumentLink> cdList = [Select Id, ContentDocumentId, LinkedEntityId From ContentDocumentLink Where LinkedEntityId = :taskId ];
taskId is the triggered record

Note* - How can i fetch file id which is attached while task is saved ?(For this i did go through Content Document and Content Document Link but unable to fetch the records, So is there any limitation to this.)
I am trying to update and remove the picklist value set using tooling api on the basis of active users in the org , i am able to update the picklist value set using json but when it comes to removing the value from ValueSet , i am unable to find any approach .
Sample Code Used While updating the picklist valueset 
 Http h = new Http();
        Httprequest req = new Httprequest();
        string json ='{"Metadata" : {"businessOwnerGroup" : null,"businessOwnerUser" : null,"businessStatus" : null,"caseSensitive" : null,"complianceGroup" : null,'+
    '"customDataType" : null,"defaultValue" : null,"deleteConstraint" : null,"deprecated" : null,"description" : null,"displayFormat" : null,"displayLocationInDecimal" : null,'+
    '"encryptionScheme" : null,"escapeMarkup" : null,"externalDeveloperName" : null,"externalId" : false,"formula" : null,"formulaTreatBlanksAs" : null,"inlineHelpText" : null,"isAIPredictionField" : null,'+
    '"isConvertLeadDisabled" : null,"isFilteringDisabled" : null,"isNameField" : null,"isSortingDisabled" : null,"label" : "PickAUser","length" : null,"lookupFilter" : null,"maskChar" : null,"maskType" : null,'+
    '"metadataRelationshipControllingField" : null,"populateExistingRows" : null,"precision" : null,"readOnlyProxy" : null,"referenceTargetField" : null,"referenceTo" : null,"relationshipLabel" : null,"relationshipName" : null,'+
    '"relationshipOrder" : null,"reparentableMasterDetail" : null,"required" : false,"restrictedAdminField" : null,"scale" : null,"securityClassification" : null,"startingNumber" : null,"stripMarkup" : null,"summarizedField" : null,'+
    '"summaryFilterItems" : null,"summaryForeignKey" : null,"summaryOperation" : null,"trackFeedHistory" : null,"trackHistory" : false,"trackTrending" : false,"translateData" : null,"type" : "Picklist","unique" : null,"urls" : null,'+
    '"valueSet" : {"controllingField" : null,"restricted" : true,"valueSetDefinition" : {"sorted" : false,"value" : [ {"color" : null,"default" : false,"description" : null,"isActive" : null,"label" : "Amanda Deeks","urls" : null,'+
    '"valueName" : "005Dn0000043JTTIA2"}, {"color" : null,"default" : false,"description" : null,"isActive" : null,"label" : "Ben Snowden","urls" : null,"valueName" : "005Dn0000043JTUIA2"},{"color" : null,"default" : false,"description" : null,'+
    '"isActive" : null,"label" : "Caroline Hawkins","urls" : null,"valueName" : "005Dn000003LM7hIAG"}, {"color" : null,"default" : false,"description" : null,"isActive" : null,"label" : "Daniel Hartage","urls" : null,"valueName" : "005Dn0000043JTVIA2"'+
    '} ]},"valueSetName" : null,"valueSettings" : []},"visibleLines" : null,"writeRequiresMasterRead" : null},"FullName" : "Theme_Relation__c.PickAUser__c"}';
    
        req.setHeader('Authorization', 'Bearer' + UserInfo.getUserId());            
        string endpointRemainingUrl = '/services/data/v48.0/tooling/sobjects/CustomField/00NDn00000HiexxMAB';
        string endpoint = Url.getSalesforceBaseUrl().toExternalForm() + endpointRemainingUrl;
        req.setEndpoint(endpoint); 
        req.setHeader('Content-Type','application/json;  charset=utf-8');
        req.setBody(json);
        req.setMethod('PATCH');
        Httpresponse res = h.send(req); 
Does contentDocumentLink and contentDocument return no record when used in apex as in my query it returns no records.

Query -
list<ContentDocumentLink> cdList = [Select Id, ContentDocumentId, LinkedEntityId From ContentDocumentLink Where LinkedEntityId = :taskId ];
taskId is the triggered record

Note* - How can i fetch file id which is attached while task is saved ?(For this i did go through Content Document and Content Document Link but unable to fetch the records, So is there any limitation to this.)