• t j 5
  • NEWBIE
  • 0 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 3
    Replies
Hi 
Here is the complete solution. Hit like if this solution helps you.
User-added image

trigger MaintenanceRequest on Case ( after update) {
    
  //   List<case>ClosedCaseList = [SELECT Id, subject, Vehicle__c, vehicle__r.Name, equipment__c, type FROM Case WHERE status = 'closed'];
    
        List<case>ClosedCaseList = new List<case>();
     for(case c : Trigger.New){
        if((c.type == 'Repair' || c.type =='Routine Maintenance') && c.status =='closed'){
            ClosedCaseList.add(c);
           
        }
    }
     MaintenanceRequestHelper.updateWorkOrders(ClosedCaseList);
}


public class MaintenanceRequestHelper {
    
    public static void updateWorkOrders(List<case>ClosedCaseList){
        // update workorders
        
        list<case> insertCaseList = new list<case>();
        
        for(Case c : ClosedCaseList)
            
            {
                Case newCase = new Case();
                newCase.Type = 'Routine Maintenance';
                newCase.Status = 'New';
                newCase.Vehicle__c = c.Vehicle__c;
                newCase.Subject =  c.Subject;
                newCase.Date_Reported__c = Date.today();
                newCase.Date_Due__c = Date.today();
                newCase.Equipment__c = c.Equipment__c;
                insertCaseList.add(newCase);
            }
        
        if(insertCaseList.size()>0){
           Database.insert(insertCaseList,false); 
        }
    }
        
}

public with sharing class WarehouseCalloutService {
public static integer size;
    private static final String WAREHOUSE_URL = 'https://th-superbadge-apex.herokuapp.com/equipment';
    
    // complete this method to make the callout (using @future) to the
    // REST endpoint and update equipment on hand.
    @Future(callout=true)
    public static void runWarehouseEquipmentSync(){
        HttpRequest req = new HttpRequest();
        req.setMethod('GET');
        req.setEndpoint(WAREHOUSE_URL);
        req.setHeader('Content-Type', 'application/json');
        Http http= new Http();
        HTTPResponse res = http.send(req);
        
       list<object> equipments = (list<Object>) JSON.deserializeUntyped(res.getBody());
       
                   //List<Product2__c> p2 = new List<Product2__c>();
                   
                   List<Product2> p2 = new List<Product2>();
            for(Object obj :  equipments){
           map<String, Object> mapProduct = (Map<String, Object>)obj;
            Product2 product = new Product2();
            
          product.Name = (string)mapProduct.get('name');

          product.Cost__c = (integer)mapProduct.get('cost');

         product.Current_Inventory__c = (integer)mapProduct.get('quantity');
 product.ProductCode = (string)mapProduct.get('_id');
         product.Maintenance_Cycle__c = (Decimal)mapProduct.get('maintenanceperiod');

                product.Replacement_Part__c = true;

                product.Lifespan_Months__c= (integer)mapProduct.get('lifespan');

                product.Warehouse_SKU__c = (string)mapProduct.get('sku');

              //  product.ProductCode = (string)mapProduct.get('_id');

                p2.add(product);
                
                }
                size=p2.size();
                system.debug('p2'+p2.size());
      
      Database.upsert(p2,false);
     System.debug('upsert');


    }

}


@isTest
Public class WarehouseCalloutServiceTest {
  // implement your mock callout test here
  
   public static String CRON_EXP = '0 0 0 15 3 ? 2022';
  @isTest
  public static void test1(){
  Test.startTest();
  Vehicle__c vec = new Vehicle__c();
  vec.name= 't1';
  insert vec;
  
   product2 prod= new product2 ();
  prod.name= 't1';
  insert prod;
  
   Vehicle__c vecD= [SELECT Id FROM Vehicle__c WHERE name='t1'];
   product2 prodD= [SELECT Id FROM product2 WHERE name='t1'];
          list<case> l1 = new list<case>();
          for(integer i = 0; i<=300;i++)
              {
             
                        case newCase= new case();
                        newCase.Type = 'Routine Maintenance';
                        newCase.Status = 'New';
                        newCase.Vehicle__c = vecD.id;
                        newCase.Subject =  'testt'+i;
                        newCase.Date_Reported__c = Date.today();
                        newCase.Date_Due__c = Date.today();
                        newCase.Equipment__c = prodD.id;
                        l1.add(newCase);
                       
                  }
        insert l1;
        
        list<case> l2=[select id from case limit 200];
        list<case> l3= new list<case>();
        
        for(case ob : l2){
        ob.status='closed';        
        l3.add(ob);
        }
        update l3;
         list<case> l4=[select id from case where Type = 'Routine Maintenance' And status='closed'];
        //System.AssertEquals(10,l4.size());
                  Test.stopTest();
  }
  
  @isTest
  public static void test2(){
  Test.startTest();
  if(Test.isRunningTest())
   Test.setMock(HttpCalloutMock.class, new WarehouseCalloutServiceMock());        
  WarehouseCalloutService.runWarehouseEquipmentSync();
  

  Test.stopTest();
   System.AssertEquals(22,WarehouseCalloutService.size);
   }
}
  • September 06, 2017
  • Like
  • 0
Hi,

Please, help me for the test case.

Here the code how to automatically update app logo of the managed package but the issue in a test case can any one try to create a test case for the same code below:
global without sharing class PostInstallScript implements InstallHandler
{
global void onInstall(InstallContext context)
{
PostInstallScript.AppLogoUpdater('NewLogo.PNG');
}
@future (callout=true)
public static void AppLogoUpdater(Sting ImageName)
{
Http newHttp = new Http();
HttpRequest request = new HttpRequest();
string imageURL = 'http://yourRemoteSiteURL/'+ImageName;
imageURL = imageURL.replace(' ', '%20');
request.setEndpoint(imageURL);
request.setMethod('GET');
request.setHeader('Content-Type', 'image/png');
request.setCompressed(true);
request.setTimeout(50000);
HttpResponse result = null;
result = newHttp.send(request);
string responseValue = '';
responseValue = result.getBody();
blob image = result.getBodyAsBlob();
 
 
Document VarDocument = [SELECT Id, body FROM Document WHERE name='Your App Logo Document' limit 1];
VarDocument.body = image;
update VarDocument;
}
}

Thanks 
  • September 01, 2017
  • Like
  • 0
Hi,
please provide test case for class which having duplicate rule.

global class CopyAcc implements Database.Batchable<sObject> {
    Set<Id> setDuplicateIds = new Set<id>();
    global Database.QueryLocator start(Database.BatchableContext bc)
    {
        String query= 'select id,name,website,billingcity,phone,billingstreet from account';
        return Database.getQueryLocator(query); 
    }
    global void execute(Database.BatchableContext bc, List<Account> scope)
    {
     system.debug('account '+scope);
        for(Account a : scope)
        {
          
   Database.SaveResult sr = Database.insert(account, false);

    if (!sr.isSuccess()) {
        Datacloud.DuplicateResult duplicateResult;
        // Insertion failed due to duplicate detected
        for(Database.Error duplicateError : sr.getErrors()){

            duplicateResult = ((Database.DuplicateError)duplicateError).getDuplicateResult();
        }

    // Fetch the Ids of the existing duplicate records
        for(Datacloud.MatchResult duplicateMatchResult : duplicateResult.getMatchResults()) {

            for(Datacloud.MatchRecord duplicateMatchRecord : duplicateMatchResult.getMatchRecords()) {
customobj_c ob = new customobj_c (name_c=a.name);
  insert ob;
            }
        }
  
       // System.debug('Duplicate records ' + setDuplicateIds);
    }
else
{
}
        }
          
    }
    
    global void finish(Database.BatchableContext bc)
    {
        
    }
}

Thanks
  • October 21, 2016
  • Like
  • 0
Hello,
i have long running callout on my visualforce page implemented via Continuation class. It works well in most cases, but sometimes (1 of 10 000 calls) there are two types of unexpected behavior.

1) oncomplete is not called after return from controller (processMoveToStage5Response method has run properly and response has been logged).

or

2) oncomplete is called but rerender is not performed, so activatedOrderResponseJson element's html on page is not rendered (but processMoveToStage5Response  processed response properly and response is logged).

Timeout for Continuation and WS is set to the same value (120 s).

Do you have an idea what can be cause of this behavior?

Thanks
Honza

Visualforce page:
<apex:form >
	<apex:outputPanel id="errorMessage" styleClass="hidden errorMessage">{!errorMessage}</apex:outputPanel>
	<apex:outputPanel id="activatedOrderResponseJson" styleClass="hidden activatedOrderResponseJson">{!activatedOrderResponseJson}</apex:outputPanel>

	<apex:actionFunction action="{!moveToStage5}" onComplete="onMovedToStage5()" name="startMoveToStage5" reRender="activatedOrderResponseJson,errorMessage" />

</apex:form>

Controller:
public Continuation moveToStage5() {
    Continuation cont = new Continuation(timeout);
    cont.continuationMethod = 'processMoveToStage5Response';

    AsyncWS.Asyncy_sf_webservice service = new AsyncWS.Asyncy_sf_webservice();
    activationResponse = service.method(cont, ... params ..);

    return cont;
}

public void processMoveToStage5Response() {
    AsyncWS.YSfSaveorderResponse_element response;
    try {
        response = activationResponse.getValue();
        //logging of response
    } catch(Exception e) {
        //logging of error
        return;
    }

    activatedOrderResponseJson = JSON.serialize(response);
}






 

Is it possible to save a custom value from within the Live Agent chat to the Transcript object?

I can add a new field to the Transcript object. But how would I save a value to it during the chat so it is included when the Transcript record is created after the chat?

 
Hi all,
 
In my application i require to fetch data for the year entered by user using SOQL query,
In SOQL i tried the below code:
Code:
date sampleDate=Date.today();
//assume user have entered 2003
Integer diff=sampleDate.year()-2003;
Contact_log__c[] contactLog=[Select a.Id from Contact_log__c a where  a.Last_Import_ModifiedDate__c =(THIS_YEAR-:diff) order by a.Sequence_Number__c desc limit 1];

 

but i get syntax error , Please guide me.

Thanks in advance


 
  • January 20, 2009
  • Like
  • 0