• Bhagya
  • NEWBIE
  • 40 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 1
    Likes Given
  • 5
    Questions
  • 1
    Replies
Hi can someone help me with this trigger?

*Avoid deletion of opportunities if its child Consultant status is active.(create consultant object with fields-name,DOB,active,oppid(lookup),accountid(lookup)).

I've tried this, but I'm getting error in different way.

trigger OppConsultantTrigger on Opportunity (before delete) {
     list<Consultant__c> cns = new list<Consultant__c>();
         cns = [select id,name,status__c from Consultant__c where Opportunityid__c IN : Trigger.OldMap.keySet()];
         for(opportunity o:trigger.old){
             for(Consultant__c c : cns)
             if(o.id== c.Opportunityid__c && c.status__c == 'Active')
             {
                 o.adderror('you annot delete this opportunity');
             }
         }
}
s
  • August 16, 2022
  • Like
  • 0
Can someone help me with this trigger?
Avoid deletion of related contact while deleting account which has rating as 'warm' and No_of_Employees__c is not empty(add error message).
I've tried this, but not working and not showing error message.
trigger AvoidContactDel on Account (before delete) {
    list<account> acc = new list<account>();
    list<contact> con = new list<contact>();
    set<id> ids = new set<id>();
    for(account a : trigger.old){
    ids.add(a.id);
    }
    con = [select id,lastname from contact where accountid IN : ids ];
    //acc = [select id,name,stage,No_of_Employees__c from account where id in : ids];
    for(account a : trigger.old){
        for(contact c : con){
        if(a.id == c.accountid && a.rating == 'Warm' && a.No_of_Employees__c != null){
            a.adderror('you are not allow to delete this contact for some reasons');     
        }
        }
    }
    
}
       
 
  • August 13, 2022
  • Like
  • 0
public class UpdateOpportunity {
    public static void descriptionUpdate() {
        List<Opportunity> opp = [SELECT  Id,Name,Description From Opportunity WHERE StageName='Closed Won'limit 5];
        System.debug(opp);
        for ( Opportunity opps :opp)
        { 
            if(opps.Description == Null){
                opps.Description = 'This Opportunity is converted into Account';
            }
            else if(opps.Description == ''){
                opps.Description = 'This Opportunity is converted into Account';
            }
        }
              Update opp;
    }
}
CLASS:
public class Currencyconverter {
    public static decimal CurrencyconverterfromEUROtoINR(decimal eur){
        decimal inr = 0.0;
        HTTP http = new HTTP();
        HTTPRequest req= new HTTPRequest();
        req.setEndpoint('https://api.exchangeratesapi.io/latest');
        req.setMethod('GET');
        HTTPResponse res = http.send(req);
        Map <string,object> Jsonbody=(Map <string,object>)Json.deserializeUntyped(res.getBody());
        system.debug(Jsonbody);
        Map <string,object> rates=(Map<string,object>)Jsonbody.get('rates');
        decimal conversionrate= (decimal)rates.get('INR');                    
        inr = eur * conversionrate;      
        return inr;
    
}
}

Testing at anonymous window:
decimal conversionrate = Currencyconverter.CurrencyconverterfromEUROtoINR(150);
system.debug('conversionrate'+conversionrate);

ERROR:
System.NullPointerException: Attempt to de-reference a null object
The error in test class is "Illegal assignment from System.PageReference to System.HttpResponse".  PLEASE SOMEONE HELP WITH THIS IN GETTI NG MAX CODE COVERAGE.
====================================================
My controller class code:


public class vfClass {
    public String InputLabel{get;set;}
    public decimal GeoPosition{get;set;}
    public decimal GeoPosition2{get;set;}
    public PageReference makeCallout(){
        if(InputLabel==NULL || InputLabel=='' ){
            //ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Please Enter a Location'));
        }
        else{
            Http http = new Http();
            HttpRequest request = new HttpRequest();
            String apikey = 'zrAR6DRKkoC4gRoku7QhnIiSjUxPNLMO';
            String endPoint = 'http://dataservice.accuweather.com/locations/v1/cities/search?q='+InputLabel+'&apikey='+apikey;
            if(Test.isRunningTest()) {
            endPoint = 'http://dataservice.accuweather.com/locations/v1/cities/search?q=Delhi&apikey=dummy';    
            }
            request.setEndpoint(endPoint);
            request.setMethod('GET');
            HttpResponse response = http.send(request);
            System.debug('response status-->'+response.getStatusCode());
            System.debug('response -->'+response);
            if (response.getStatusCode() == 200) {
                System.debug('body-->'+response.getBody());
                Object resp = JSON.deserializeUntyped(response.getBody());
                System.debug('resp-->'+resp);
                List<Object> theJsonList = (List<Object>) resp;
                System.debug('theJsonList-->'+theJsonList);
                List<Map<String, Object>> theJsonMapList = new List<Map<String, Object>>();
                
                for (Object obj : theJsonList) {
                    theJsonMapList.add((Map<String, Object>) obj);
                }
                System.debug('theJsonMapList-->'+theJsonMapList[0].get('GeoPosition'));
                Map<String,Object> GeoPositionMap = (Map<String,Object>)theJsonMapList[0].get('GeoPosition');
                System.debug('GeoPositionMap->'+GeoPositionMap.get('Latitude'));
               GeoPosition = (decimal)GeoPositionMap.get('Latitude');
               GeoPosition2 =(decimal)GeoPositionMap.get('Longitude');
                
            }
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Searching For GeoPositions'));
        }
        
        return null;
    }
}
=====================================================
Mock test code:
@isTest
global class vfnmock implements HttpCalloutMock {
    global HTTPResponse respond(HTTPRequest request) {
         HttpResponse response = new HttpResponse();
        response.setHeader('Content-Type', 'application/json');
        response.setBody('{GeoPosition": {"Latitude": 28.643,"Longitude": 77.118}}');
        response.setStatusCode(200);
        return response;
    }
}
====================================================
Test Class (that I tried):

@isTest
private class TestLocations {
     @isTest static void testCallout() {

        Test.setMock(HttpCalloutMock.class, new vfnmock());
        vfClass vfinst = new vfClass();
        HttpResponse res = vfinst.makeCallout();
        
        String contentType = res.getHeader('Content-Type');
        System.assert(contentType == 'application/json');
        String actualValue = res.getBody();
        String expectedValue = '{"example":"test"}';
        System.assertEquals(actualValue, expectedValue);
        System.assertEquals(200, res.getStatusCode());
    }
}

The error in test class is "Illegal assignment from System.PageReference to System.HttpResponse".  PLEASE SOMEONE HELP WITH THIS IN GETTI NG MAX CODE COVERAGE.
CLASS:
public class Currencyconverter {
    public static decimal CurrencyconverterfromEUROtoINR(decimal eur){
        decimal inr = 0.0;
        HTTP http = new HTTP();
        HTTPRequest req= new HTTPRequest();
        req.setEndpoint('https://api.exchangeratesapi.io/latest');
        req.setMethod('GET');
        HTTPResponse res = http.send(req);
        Map <string,object> Jsonbody=(Map <string,object>)Json.deserializeUntyped(res.getBody());
        system.debug(Jsonbody);
        Map <string,object> rates=(Map<string,object>)Jsonbody.get('rates');
        decimal conversionrate= (decimal)rates.get('INR');                    
        inr = eur * conversionrate;      
        return inr;
    
}
}

Testing at anonymous window:
decimal conversionrate = Currencyconverter.CurrencyconverterfromEUROtoINR(150);
system.debug('conversionrate'+conversionrate);

ERROR:
System.NullPointerException: Attempt to de-reference a null object